我一直试图让透明度与我的应用程序工作(其中存储在他们面前动态调整大小的图像),我想我终于缩小约太多误导后问题imagealphablending和imagesavealpha.源图像永远不会加载适当的透明度!
// 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);
Run Code Online (Sandbox Code Playgroud)
从文件加载图像会有一些严重的架构难度; 此代码与从iPhone应用程序查询的JSON API一起使用,在这种情况下(并且更加一致)在POST数据中将图像作为base64编码的字符串上载更容易.我是否绝对需要以某种方式将图像存储为文件(以便PHP可以将其再次加载到内存中)?有没有办法创建一个$fileData可以传递给它的流imagecreatefrompng?
Blech,这最终归结为完全独立的GD调用,它验证了图像上传.我忘了添加imagealphablending和imagesavealpha代码,它创建了一个新的图像,然后传递给调整大小代码.无论如何应该改变哪个.非常感谢goldenparrot将字符串转换为文件名的绝佳方法.
小智 5
您可以使用此代码:
$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);
Run Code Online (Sandbox Code Playgroud)
它将为您制作透明图像。祝你好运 !
| 归档时间: |
|
| 查看次数: |
4529 次 |
| 最近记录: |