Change color in a single color transparent png using gd libarary
i m trying to recolor a one color png image using gd library. I found a code in another post that will recolor a gif. I have modified that code to work with transparent png. Here is my code. I can create a new png file with this code but color is not changed. Please someone help me to change color in a transparent png.
<?php
// first we will create a transparent image. an image that has no color.
$width = 300; $height=340;
$image = imagecreatetruecolor($width,$height); //black image of the specified width x height.
imagealphablending($image, false); // set blend mode to false.
$col=i开发者_开发知识库magecolorallocatealpha($image,255,255,255,127); // fill color
imagefilledrectangle($image,0,0,$width,$height,$col);
imagealphablending($image,true);
$shirt = imagecreatefrompng("shirt.png");
$color = imagecolorclosest ( $shirt, 255,0,0 );
imagecolorset($shirt,$color,92,92,92); // SET NEW COLOR
imagecopy($image, $shirt, 0, 0, 0, 0, $width, $height);
imagealphablending($image,true);
imagealphablending($image,false);
imagesavealpha($image,true);
if(imagepng($image, "hello.png", 1)){
echo "hello.png";
}
imagedestroy($image);
imagedestroy($shirt);
?>
Just figured this out myself, used a few people's code and combined them on the site, i don't remember where because I just kept grabbing and merging code.
function updateThumb($image, $newColor) {
$img = imagecreatefrompng($image);
$w = imagesx($img);
$h = imagesy($img);
// Work through pixels
for($y=0;$y<$h;$y++) {
for($x=0;$x<$w;$x++) {
// Apply new color + Alpha
$rgb = imagecolorsforindex($img, imagecolorat($img, $x, $y));
$transparent = imagecolorallocatealpha($img, 0, 0, 0, 127);
imagesetpixel($img, $x, $y, $transparent);
// Here, you would make your color transformation.
$red_set=$newColor[0]/100*$rgb['red'];
$green_set=$newColor[1]/100*$rgb['green'];
$blue_set=$newColor[2]/100*$rgb['blue'];
if($red_set>255)$red_set=255;
if($green_set>255)$green_set=255;
if($blue_set>255)$blue_set=255;
$pixelColor = imagecolorallocatealpha($img, $red_set, $green_set, $blue_set, $rgb['alpha']);
imagesetpixel ($img, $x, $y, $pixelColor);
}
}
// Restore Alpha
imageAlphaBlending($img, true);
imageSaveAlpha($img, true);
return $img;
}
function makeThumb($path, $top, $bottom=FALSE) {
$width = imagesx($top);
$height = imagesy($top);
$thumbHeight = $bottom != FALSE ? $height * 2 : $height;
// Create Transparent PNG
$thumb = imagecreatetruecolor($width, $thumbHeight);
$transparent = imagecolorallocatealpha($thumb, 0, 0, 0, 127);
imagefill($thumb, 0, 0, $transparent);
// Copy Top Image
imagecopy($thumb, $top, 0, 0, 0, 0, $width, $height);
// Copy Bottom Image
if ($bottom != FALSE) {
imagecopy($thumb, $bottom, 0, $height, 0, 0, $width, $height);
}
// Save Image with Alpha
imageAlphaBlending($thumb, true);
imageSaveAlpha($thumb, true);
header('Content-Type: image/png');
imagepng($thumb, $path); // save image as png
}
//the array is ur rgb recolor. array(red,green,blue) values 0-255
$thumbTop = updateThumb('input/path', array(240,105,15));
精彩评论