gpt4 book ai didi

PHP GD imagecreatefromstring 丢弃透明度

转载 作者:可可西里 更新时间:2023-10-31 23:10:57 26 4
gpt4 key购买 nike

我一直在努力让我的应用程序变得透明(它在存储图像之前动态调整图像大小),我认为在对 imagealphablendingimagesavealpha。源图像从未以适当的透明度加载!

// With this line, the output image has no transparency (where it should be
// transparent, colors bleed out randomly or it's completely black, depending
// on the image)
$img = imagecreatefromstring($fileData);
// With this line, it works as expected.
$img = imagecreatefrompng($fileName);

// Blah blah blah, lots of image resize code into $img2 goes here; I finally
// tried just outputting $img instead.

header('Content-Type: image/png');
imagealphablending($img, FALSE);
imagesavealpha($img, TRUE);
imagepng($img);

imagedestroy($img);

从文件中加载图像在架构上会遇到一些严重的困难;此代码与从 iPhone 应用程序查询的 JSON API 一起使用,在这种情况下更容易(并且更一致)将图像上传为 POST 数据中的 base64 编码字符串。我是否绝对需要以某种方式将图像存储为文件(以便 PHP 可以再次将其加载到内存中)?有没有办法从 $fileData 创建一个 Stream 并传递给 imagecreatefrompng

最佳答案

你可以使用这段代码:

$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);

它将为您制作透明图像。祝你好运!

关于PHP GD imagecreatefromstring 丢弃透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12181573/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com