PHP GD imagecreatefromstring discards transparency

感情迁移 提交于 2019-12-05 03:41:35

Blech, this turned out to ultimately be due to a totally separate GD call which was validating the image uploads. I forgot to add imagealphablending and imagesavealpha to THAT code, and it was creating a new image that then got passed to the resizing code. Which should probably be changed anyway. Thanks very much to goldenparrot for the excellent method of converting a string into a filename.

papyrut

you can use this code :

$new = imagecreatetruecolor($width, $height);

// preserve transparency

imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));

imagealphablending($new, false);

imagesavealpha($new, true);

imagecopyresampled($new, $img, 0, 0, $x, 0, $width, $height, $w, $h);

imagepng($new);

imagedestroy($new);

It will make a transparent image for you. Good Luck !

Do I absolutely need to somehow store the image as a file (just so that PHP can load it into memory again)?

No.

Documentation says:

You can use data:// protocol from php v5.2.0

Example:

// prints "I love PHP"
echo file_get_contents('data://text/plain;base64,SSBsb3ZlIFBIUAo=');
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!