imagepng() and transparency in GD library with PHP

丶灬走出姿态 提交于 2019-12-18 02:19:43

问题


When using the function imagepng() in PHP, how can I make sure the images that I save are saved with a transparent background?


回答1:


Here is an example of the imagecolortransparent function (if it helps):

<?php
// Create a 55x30 image
$im = imagecreatetruecolor(55, 30);
$red = imagecolorallocate($im, 255, 0, 0);
$black = imagecolorallocate($im, 0, 0, 0);

// Make the background transparent
imagecolortransparent($im, $black);

// Draw a red rectangle
imagefilledrectangle($im, 4, 4, 50, 25, $red);

// Save the image
imagepng($im, './imagecolortransparent.png');
imagedestroy($im);
?>



回答2:


Simply do this:

imagealphablending($img, false);
imagesavealpha($img, true);

Before outputting. Make sure that all source files (if you used any) are set to PNG 32-bit with transparency - if not the output may differ with black background or transparency does not comply.




回答3:


Here is the example

     $newimage = imagecreatetruecolor($dst_w, $dst_h);
     imagealphablending($newimage, false);
     imagesavealpha($newimage, true);
     $transparentindex = imagecolorallocatealpha($newimage, 255, 255, 255, 127);
     imagefill($newimage, 0, 0, $transparentindex);



回答4:


There's a function called imagecolortransparent that allows you to set which color is made transparent. I don't know if this answers your question.



来源:https://stackoverflow.com/questions/1705098/imagepng-and-transparency-in-gd-library-with-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!