问题
I'll be using these variables throughout:
$ROOTDIR = $_SERVER["DOCUMENT_ROOT"];
$ROOTFILE = "http://www.scottandjessiecooper.com/webtutorials/images/smiley.png";
$NEWFILE = "$ROOTDIR/images/tmp/new_smiley.png";
When I use this function, I have NO problems with transparency
function save_image($root, $saveto){
copy($root, $saveto);
}
save_image( $ROOTFILE, $NEWFILE ); // root can be file or url
However I NEED to use an IMAGE_RESOURCE so i cam manipulate the ROOTFILE if needed
So i treid this:
if ( file_exists( $NEWFILE ) ) unlink ($NEWFILE);
$image = imagecreatefrompng( $ROOTFILE );
imagepng( $image, $NEWFILE );
imagedestroy( $image );
Now when I use this:
<img src="<?=$NEWFILE?>" />
I lose the transparency. The background goes BLACK!
So I tried outputting the image to make sure it wasn't the saving that caused the problem:
if ( file_exists( $NEWFILE ) ) unlink ($NEWFILE);
$image = imagecreatefrompng( $ROOTFILE );
header('Content-Type: image/png');
imagepng( $image );
imagedestroy( $image );
Still to no avail...
Help?
回答1:
You need to enabled alpha blending and save alpha. I found this after a 10 sec google search: http://www.php.net/manual/en/function.imagecreatefrompng.php#43024
回答2:
The problem here is not in the GD
or the PHP
.
The problem is in the Photoshop.
If you save PNG
file while you are on RGB mode
and not on Indexed mode
.
回答3:
this helps?
$info = getimagesize("smiley.png");
$image = imagecreatefrompng("smiley.png");
$image_new = imagecreatetruecolor($info[0],$info[1]);
if ( ($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG) ) {
$trnprt_indx = imagecolortransparent($image);
if ($trnprt_indx >= 0 ) {
$trnprt_color = imagecolorsforindex($image, $trnprt_indx);
$trnprt_indx = imagecolorallocate($image_new, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
imagefill($image_new, 0, 0, $trnprt_indx);
imagecolortransparent($image_new, $trnprt_indx);
}
elseif ($info[2] == IMAGETYPE_PNG) {
imagealphablending($image_new, false);
$color = imagecolorallocatealpha($image_new, 0, 0, 0, 127);
imagefill($image_new, 0, 0, $color);
imagesavealpha($image_new, true);
}
}
imagecopy($image_new,$image,0,0,0,0,$info[0],$info[1]);
imagepng($image_new,"smiley2.png");
回答4:
I had this problem and found prehfeldt's answer has the right idea but didn't actually help me solve this. The practical way to enable saving alpha channel information is by calling imagesavealpha on your image resource before you output it to file:
imagesavealpha($image, true);
imagepng( $image, $NEWFILE );
When you don't do this, by default GD will discard the transparency information when you save or output the image. The reason copy
didn't cause this problem for you is that its a simple byte by byte copy at the file level without going through any image processing at all.
回答5:
If the background is black, try the following:
$black = imagecolorallocate($im, 0, 0, 0);
// Make the background transparent
imagecolortransparent($im, $black);
(Transparency in PNGs is never perfect thorugh PHP)
来源:https://stackoverflow.com/questions/10718617/png-file-is-not-keeping-transparency