note 54404 deleted from function.imagecopy by cmb

From: Date: Fri, 09 Oct 2020 12:09:29 +0000
Subject: note 54404 deleted from function.imagecopy by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-215556@lists.php.net to get a copy of this message
Note Submitter: sawaz ---- Just a little function that allows you to change the foreground color of a transparent gif or png. It's supposed to use only two-color images, becouse it convert all the colors to the specified one. It works with .gif or .png as source, but writes only .png function ColorReplace( $url ) { # The new color and its channels $NEW_COLOR = "#FF66FF"; $r = 0+("0x".substr( $NEW_COLOR, 1, 2)); $g = 0+("0x".substr( $NEW_COLOR, 3, 2)); $b = 0+("0x".substr( $NEW_COLOR, 5, 2)); # I read the kind of file from the extension $tmp = pathinfo($url); $extension =$tmp['extension']; if( $extension == 'png' ) $oldIm = @imagecreatefrompng( $url ); elseif( $extension == 'gif' ) $oldIm = @imagecreatefromgif( $url ); # I replace EACH color in the palette with the new one for( $i=0; $i < imagecolorstotal( $oldIm ); $i++ ) imagecolorset( $oldIm, $i, $r,$g,$b ); # Output the file in png format : each color is now NEW COLOR # but transparency is preserved !! header("Content-type: image/png"); imagepng($oldIm); imagedestroy($oldIm); }

« previous php.notes (#215556) next »