图像颜色不能使用 imagefilter 函数正确改变
Image color not change proper using imagefilter function
我正在使用 imagefilter
函数来更改图像颜色。但问题是功能没有改变我想要的颜色,结果图像颜色是旧图像颜色和新图像颜色的相加。这是我的原图
并且我想在
中更改此图像颜色
但他们是这样转换的
这是我的代码
header('Content-Type: image/png');
$image = imagecreatefrompng('a400-d58.png');
imagefilter($image, IMG_FILTER_COLORIZE, 0, 128, 64);
imagealphablending( $image, true );
imagesavealpha( $image, true );
imagepng($image,'a400-d585.png');
当我检查图像功能时,实际上添加了旧图像值和给定值
103,55,32 + 0,128,64 =103,183,96 这是我的新图像颜色值。
谁能帮我看看这是怎么回事?
我尝试了这个并且成功了 - 主要是 :) 我将你的棕色图像保存为 img1.png。
<?php
function colorizeKeepAplhaChannnel( $inputFilePathIn, $targetRedIn, $targetGreenIn, $targetBlueIn, $outputFilePathIn ) {
$im_src = imagecreatefrompng( $inputFilePathIn );
$im_dst = imagecreatefrompng( $inputFilePathIn );
$width = imagesx($im_src);
$height = imagesy($im_src);
// Note this: FILL IMAGE WITH TRANSPARENT BG
imagefill($im_dst, 0, 0, IMG_COLOR_TRANSPARENT);
imagesavealpha($im_dst,true);
imagealphablending($im_dst, true);
$flagOK = 1;
for( $x=0; $x<$width; $x++ ) {
for( $y=0; $y<$height; $y++ ) {
$rgb = imagecolorat( $im_src, $x, $y );
$colorOldRGB = imagecolorsforindex($im_src, $rgb);
$alpha = $colorOldRGB["alpha"];
//echo "$colorOldRGB[red], $colorOldRGB[green],$colorOldRGB[blue] <br/>\n";
if($colorOldRGB["red"] >150 && $colorOldRGB["green"] >150 && $colorOldRGB["blue"] >150) {
$newRed = $colorOldRGB["red"];
$newGreen = $colorOldRGB["green"];
$newBlue = $colorOldRGB["blue"];
}
else if($colorOldRGB["red"] === 0 && $colorOldRGB["green"] === 0 && $colorOldRGB["blue"] === 0) {
$newRed = 0;
$newGreen = 0;
$newBlue = 0;
}
else {
// $newRed = 0 - $colorOldRGB["red"] + $targetRedIn;
// $newGreen =0 - $colorOldRGB["green"] + $targetGreenIn;
// $newBlue = 0 - $colorOldRGB["blue"] + $targetBlueIn;
$newRed = $targetRedIn;
$newGreen = $targetGreenIn;
$newBlue = $targetBlueIn;
}
if($newRed <0 ) {
$newRed = 0;
}
if($newRed >255) {
$newRed = 255;
}
if($newGreen <0){
$newGreen = 0;
}
if($newGreen>255) {
$newGreen = 255;
}
if($newBlue<0) {
$newBlue = 0;
}
if($newBlue>255) {
$newBlue = 255;
}
//$colorNew = imagecolorallocatealpha($im_src, $targetRedIn, $targetGreenIn, $targetBlueIn, $alpha);
$colorNew = imagecolorallocatealpha($im_src, $newRed, $newGreen, $newBlue, $alpha);
$flagFoundColor = true;
// uncomment next 3 lines to substitute only 1 color (in this case, BLACK/greys)
/*
$colorOld = imagecolorallocatealpha($im_src, $colorOldRGB["red"], $colorOldRGB["green"], $colorOldRGB["blue"], 0); // original color WITHOUT alpha channel
$color2Change = imagecolorallocatealpha($im_src, 0, 0, 0, 0); // opaque BLACK - change to desired color
$flagFoundColor = ($color2Change == $colorOld);
*/
if ( false === $colorNew ) {
//echo( "FALSE COLOR:$colorNew alpha:$alpha<br/>" );
$flagOK = 0;
} else if ($flagFoundColor) {
imagesetpixel( $im_dst, $x, $y, $colorNew );
//echo "x:$x y:$y col=$colorNew alpha:$alpha<br/>";
}
}
}
$flagOK2 = imagepng($im_dst, $outputFilePathIn);
if ($flagOK && $flagOK2) {
echo ("<strong>Congratulations, your conversion was successful </strong><br/>new file $outputFilePathIn<br/>");
} else if ($flagOK2 && !$flagOK) {
echo ("<strong>ERROR, your conversion was UNsuccessful</strong><br/>Please verify if your PNG is truecolor<br/>input file $inputFilePathIn<br/>");
} else if (!$flagOK2 && $flagOK) {
$dirNameOutput = dirname($outputFilePathIn)."/";
echo ("<strong>ERROR, your conversion was successful, but could not save file</strong><br/>Please verify that you have PERMISSION to save to directory $dirName <br/>input file $inputFilePathIn<br/>");
} else {
$dirNameOutput = dirname($outputFilePathIn)."/";
echo ("<strong>ERROR, your conversion was UNsuccessful AND could not save file</strong><br/>Please verify if your PNG is truecolor<br/>Please verify that you have PERMISSION to save to directory $dirName <br/>input file $inputFilePathIn<br/>");
}
echo ("TargetName:$outputFilePathIn wid:$width height:$height CONVERTED:|$flagOK| SAVED:|$flagOK2|<br/>");
imagedestroy($im_dst);
imagedestroy($im_src);
}
$targetRed = 0;
$targetGreen = 128;
$targetBlue = 64;
//$inputFileName = 'frameSquareBlack_88x110.png';
$inputFileName = 'img1.png';
$dirName = "./images/";
$nameTemp = basename($inputFileName, ".png");
$outputFileName = $nameTemp."_$targetRed"."_$targetGreen"."_$targetBlue.png";
$inputFilePath = $dirName.$inputFileName;
$outputFilePath = $dirName.$outputFileName;
//echo "inputFileName:$inputFilePath<br>outputName:$outputFilePath<br>";
colorizeKeepAplhaChannnel( $inputFilePath, $targetRed, $targetGreen, $targetBlue, $outputFilePath);
?>
<br/><br/>
Original <br/>
<img src="<?php echo $inputFilePath; ?>">
<br /><br />Colorized<br/>
<img src="<?php echo $outputFilePath; ?>">
<br />
我正在使用 imagefilter
函数来更改图像颜色。但问题是功能没有改变我想要的颜色,结果图像颜色是旧图像颜色和新图像颜色的相加。这是我的原图
并且我想在
但他们是这样转换的
这是我的代码
header('Content-Type: image/png');
$image = imagecreatefrompng('a400-d58.png');
imagefilter($image, IMG_FILTER_COLORIZE, 0, 128, 64);
imagealphablending( $image, true );
imagesavealpha( $image, true );
imagepng($image,'a400-d585.png');
当我检查图像功能时,实际上添加了旧图像值和给定值 103,55,32 + 0,128,64 =103,183,96 这是我的新图像颜色值。
谁能帮我看看这是怎么回事?
我尝试了这个并且成功了 - 主要是 :) 我将你的棕色图像保存为 img1.png。
<?php
function colorizeKeepAplhaChannnel( $inputFilePathIn, $targetRedIn, $targetGreenIn, $targetBlueIn, $outputFilePathIn ) {
$im_src = imagecreatefrompng( $inputFilePathIn );
$im_dst = imagecreatefrompng( $inputFilePathIn );
$width = imagesx($im_src);
$height = imagesy($im_src);
// Note this: FILL IMAGE WITH TRANSPARENT BG
imagefill($im_dst, 0, 0, IMG_COLOR_TRANSPARENT);
imagesavealpha($im_dst,true);
imagealphablending($im_dst, true);
$flagOK = 1;
for( $x=0; $x<$width; $x++ ) {
for( $y=0; $y<$height; $y++ ) {
$rgb = imagecolorat( $im_src, $x, $y );
$colorOldRGB = imagecolorsforindex($im_src, $rgb);
$alpha = $colorOldRGB["alpha"];
//echo "$colorOldRGB[red], $colorOldRGB[green],$colorOldRGB[blue] <br/>\n";
if($colorOldRGB["red"] >150 && $colorOldRGB["green"] >150 && $colorOldRGB["blue"] >150) {
$newRed = $colorOldRGB["red"];
$newGreen = $colorOldRGB["green"];
$newBlue = $colorOldRGB["blue"];
}
else if($colorOldRGB["red"] === 0 && $colorOldRGB["green"] === 0 && $colorOldRGB["blue"] === 0) {
$newRed = 0;
$newGreen = 0;
$newBlue = 0;
}
else {
// $newRed = 0 - $colorOldRGB["red"] + $targetRedIn;
// $newGreen =0 - $colorOldRGB["green"] + $targetGreenIn;
// $newBlue = 0 - $colorOldRGB["blue"] + $targetBlueIn;
$newRed = $targetRedIn;
$newGreen = $targetGreenIn;
$newBlue = $targetBlueIn;
}
if($newRed <0 ) {
$newRed = 0;
}
if($newRed >255) {
$newRed = 255;
}
if($newGreen <0){
$newGreen = 0;
}
if($newGreen>255) {
$newGreen = 255;
}
if($newBlue<0) {
$newBlue = 0;
}
if($newBlue>255) {
$newBlue = 255;
}
//$colorNew = imagecolorallocatealpha($im_src, $targetRedIn, $targetGreenIn, $targetBlueIn, $alpha);
$colorNew = imagecolorallocatealpha($im_src, $newRed, $newGreen, $newBlue, $alpha);
$flagFoundColor = true;
// uncomment next 3 lines to substitute only 1 color (in this case, BLACK/greys)
/*
$colorOld = imagecolorallocatealpha($im_src, $colorOldRGB["red"], $colorOldRGB["green"], $colorOldRGB["blue"], 0); // original color WITHOUT alpha channel
$color2Change = imagecolorallocatealpha($im_src, 0, 0, 0, 0); // opaque BLACK - change to desired color
$flagFoundColor = ($color2Change == $colorOld);
*/
if ( false === $colorNew ) {
//echo( "FALSE COLOR:$colorNew alpha:$alpha<br/>" );
$flagOK = 0;
} else if ($flagFoundColor) {
imagesetpixel( $im_dst, $x, $y, $colorNew );
//echo "x:$x y:$y col=$colorNew alpha:$alpha<br/>";
}
}
}
$flagOK2 = imagepng($im_dst, $outputFilePathIn);
if ($flagOK && $flagOK2) {
echo ("<strong>Congratulations, your conversion was successful </strong><br/>new file $outputFilePathIn<br/>");
} else if ($flagOK2 && !$flagOK) {
echo ("<strong>ERROR, your conversion was UNsuccessful</strong><br/>Please verify if your PNG is truecolor<br/>input file $inputFilePathIn<br/>");
} else if (!$flagOK2 && $flagOK) {
$dirNameOutput = dirname($outputFilePathIn)."/";
echo ("<strong>ERROR, your conversion was successful, but could not save file</strong><br/>Please verify that you have PERMISSION to save to directory $dirName <br/>input file $inputFilePathIn<br/>");
} else {
$dirNameOutput = dirname($outputFilePathIn)."/";
echo ("<strong>ERROR, your conversion was UNsuccessful AND could not save file</strong><br/>Please verify if your PNG is truecolor<br/>Please verify that you have PERMISSION to save to directory $dirName <br/>input file $inputFilePathIn<br/>");
}
echo ("TargetName:$outputFilePathIn wid:$width height:$height CONVERTED:|$flagOK| SAVED:|$flagOK2|<br/>");
imagedestroy($im_dst);
imagedestroy($im_src);
}
$targetRed = 0;
$targetGreen = 128;
$targetBlue = 64;
//$inputFileName = 'frameSquareBlack_88x110.png';
$inputFileName = 'img1.png';
$dirName = "./images/";
$nameTemp = basename($inputFileName, ".png");
$outputFileName = $nameTemp."_$targetRed"."_$targetGreen"."_$targetBlue.png";
$inputFilePath = $dirName.$inputFileName;
$outputFilePath = $dirName.$outputFileName;
//echo "inputFileName:$inputFilePath<br>outputName:$outputFilePath<br>";
colorizeKeepAplhaChannnel( $inputFilePath, $targetRed, $targetGreen, $targetBlue, $outputFilePath);
?>
<br/><br/>
Original <br/>
<img src="<?php echo $inputFilePath; ?>">
<br /><br />Colorized<br/>
<img src="<?php echo $outputFilePath; ?>">
<br />