Php 使用gd libarary更改单色透明png中的颜色
我正在尝试使用gd库重新绘制单色png图像。我在另一篇文章中发现了一个代码,它将重新生成gif。我已经修改了该代码以使用透明png。这是我的密码。我可以用这段代码创建一个新的png文件,但是颜色没有改变。请有人帮我改变一个透明的png颜色Php 使用gd libarary更改单色透明png中的颜色,php,gd,Php,Gd,我正在尝试使用gd库重新绘制单色png图像。我在另一篇文章中发现了一个代码,它将重新生成gif。我已经修改了该代码以使用透明png。这是我的密码。我可以用这段代码创建一个新的png文件,但是颜色没有改变。请有人帮我改变一个透明的png颜色 <?php // first we will create a transparent image. an image that has no color. $width = 300; $height=340; $image = imagecreatet
<?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=imagecolorallocatealpha($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);
?>
我自己刚刚发现了这一点,使用了一些人的代码并在网站上进行了组合,我不记得在哪里,因为我一直在抓取和合并代码
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));
函数updateThumb($image,$newColor){
$img=imagecreatefrompng($image);
$w=图像X($img);
$h=imagesy($img);
//通过像素工作
对于($y=0;$y255)$green\U set=255;
如果($blue\u set>255)$blue\u set=255;
$pixelColor=imagecolorallocatealpha($img、$red\U set、$green\U set、$blue\U set、$rgb['alpha']);
imagesetpixel($img,$x,$y,$pixelColor);
}
}
//还原阿尔法
imageAlphaBlending($img,true);
imageSaveAlpha($img,true);
返回$img;
}
函数makeThumb($path、$top、$bottom=FALSE){
$width=imagesx($top);
$height=imagesy($top);
$thumbHeight=$bottom!=FALSE?$height*2:$height;
//创建透明PNG
$thumb=ImageCreateTureColor($width,$thumbHeight);
$transparent=imagecolorallocatealpha($thumb,0,0,0127);
imagefill($thumb,0,0,$transparent);
//复制顶部图像
imagecopy($thumb、$top、0、0、0、$width、$height);
//复制底部图像
如果($bottom!=FALSE){
imagecopy($thumb、$bottom、0、$height、0、0、$width、$height);
}
//使用Alpha保存图像
ImageAlphabling($thumb,true);
imageSaveAlpha($thumb,true);
标题('Content-Type:image/png');
imagepng($thumb,$path);//将图像另存为png
}
//该阵列为ur rgb recolor。数组(红色、绿色、蓝色)值0-255
$thumbTop=updateThumb('input/path',数组(240105,15));
我自己刚刚发现了这一点,使用了一些人的代码并在网站上进行了组合,我不记得在哪里,因为我一直在抓取和合并代码
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));
函数updateThumb($image,$newColor){
$img=imagecreatefrompng($image);
$w=图像X($img);
$h=imagesy($img);
//通过像素工作
对于($y=0;$y255)$green\U set=255;
如果($blue\u set>255)$blue\u set=255;
$pixelColor=imagecolorallocatealpha($img、$red\U set、$green\U set、$blue\U set、$rgb['alpha']);
imagesetpixel($img,$x,$y,$pixelColor);
}
}
//还原阿尔法
imageAlphaBlending($img,true);
imageSaveAlpha($img,true);
返回$img;
}
函数makeThumb($path、$top、$bottom=FALSE){
$width=imagesx($top);
$height=imagesy($top);
$thumbHeight=$bottom!=FALSE?$height*2:$height;
//创建透明PNG
$thumb=ImageCreateTureColor($width,$thumbHeight);
$transparent=imagecolorallocatealpha($thumb,0,0,0127);
imagefill($thumb,0,0,$transparent);
//复制顶部图像
imagecopy($thumb、$top、0、0、0、$width、$height);
//复制底部图像
如果($bottom!=FALSE){
imagecopy($thumb、$bottom、0、$height、0、0、$width、$height);
}
//使用Alpha保存图像
ImageAlphabling($thumb,true);
imageSaveAlpha($thumb,true);
标题('Content-Type:image/png');
imagepng($thumb,$path);//将图像另存为png
}
//该阵列为ur rgb recolor。数组(红色、绿色、蓝色)值0-255
$thumbTop=updateThumb('input/path',数组(240105,15));