我有一个调整上传图片大小的脚本。它适用于PNG和JPG,但使用GIF可以在调整大小的GIF黑色中呈现透明度。
$src = imagecreatefromgif($file);
$dst = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
imagegif($dst, $file);
答案 0 :(得分:3)
从手册页http://us3.php.net/manual/en/function.imagecopyresampled.php#104028
imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
imagealphablending($new, false);
imagesavealpha($new, true);
一张海报使用来保持透明度。
这里有一个提示...总是在php.net上查看用户评论,因为它们通常非常有助于理解函数的细微差别并提供处理常见任务的提示。
答案 1 :(得分:0)
我假设这是一个网页,如果它可以输出具有正确属性的图像标记?
echo "<img src='$file' width='$newWidth' height='$newHeight' />";
答案 2 :(得分:0)
您仍然需要在新图像中设置透明度。以下摘自php docs:
<?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);
// ^^ This is the command you are missing.
// Draw a red rectangle
imagefilledrectangle($im, 4, 4, 50, 25, $red);
// Save the image
imagepng($im, './imagecolortransparent.png');
imagedestroy($im);
?>
在你的情况下,你想要使透明度与原始的transaprency颜色相同 - 我总是制作一个可怕的紫色或某种东西,它会像我的图像处理软件中的拇指一样伸出,其次,有一个RGB键几乎不可能错误地包含在图像中。