如何在 PHP 中调整 png 的透明度并使其透明?
我正在尝试在 PHP 中调整具有透明背景的 png 的大小,但我在网上找到的代码示例对我不起作用。 这是我正在使用的代码,非常感谢您的建议!
$this->image = imagecreatefrompng($filename);
imagesavealpha($this->image, true);
$newImage = imagecreatetruecolor($width, $height);
// Make a new transparent image and turn off alpha blending to keep the alpha channel
$background = imagecolorallocatealpha($newImage, 255, 255, 255, 127);
imagecolortransparent($newImage, $background);
imagealphablending($newImage, false);
imagesavealpha($newImage, true);
imagecopyresampled($newImage, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $newImage;
imagepng($this->image,$filename);
更新 我所说的“不起作用”是指当我调整 png 大小时背景颜色会变为黑色。
I'm attempting to resize pngs with transparent backgrounds in PHP and the code samples I've found online don't work for me. Here's the code I'm using, advice will be much appreciated!
$this->image = imagecreatefrompng($filename);
imagesavealpha($this->image, true);
$newImage = imagecreatetruecolor($width, $height);
// Make a new transparent image and turn off alpha blending to keep the alpha channel
$background = imagecolorallocatealpha($newImage, 255, 255, 255, 127);
imagecolortransparent($newImage, $background);
imagealphablending($newImage, false);
imagesavealpha($newImage, true);
imagecopyresampled($newImage, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $newImage;
imagepng($this->image,$filename);
Update
By 'not working' I meant to say the background color changes to black when I resize pngs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
据我所知,您需要在执行 imagecolorallocatealpha 之前将混合模式设置为 false,并将保存 alpha 通道标志设置为 true ()
更新:此代码仅适用于不透明度 = 0 的透明背景。如果您的图像有 0 < 不透明度< 100 它将是黑色背景。
From what I can tell, you need to set the blending mode to
false
, and the save alpha channel flag totrue
before you do the imagecolorallocatealpha()UPDATE : This code is working only on background transparent with opacity = 0. If your image have 0 < opacity < 100 it'll be black background.
这是对我来说效果很好的最终解决方案。
Here is a final solution that is working fine for me.
与 imagecopyresampled 相比,使用 imagescale 更好。 调整大小的图像不需要空图像资源,与 imagecopyresampled 所需的十个参数相比,仅需要两个参数。 还可以以更小的尺寸生产更好的质量。 如果使用 PHP 5.5.18 或更早版本,或者 PHP 5.6.2 或更早版本,您应该提供第三个参数的高度,因为长宽比计算不正确。
using imagescale is better compared to imagecopyresampled. No empty image resource required for the resized image, requires only two arguments compared to the ten required by imagecopyresampled. Also produces better quality with smaller sizes. If using PHP 5.5.18 or earlier, or PHP 5.6.2 or earlier, you should provide the height which is the 3rd argument as the aspect ratio calculation was incorrect.
还需要用透明颜色填充新图像(如 Dycey 编码,但我猜忘记提及:)),而不仅仅是“战略”保存本身。
IIRC,您还需要确保 PNG 是 24 位,即真彩色,而不是 8 位,以避免错误行为。
The filling of the new image with a transparent colour is also required (as Dycey coded but I'm guessing forgot to mention :)), not just the 'strategic' saving by itself.
IIRC, you also need to be sure PNGs are 24bit, ie truecolor, and not 8bit to avoid buggy behaviour.
旧线程,但以防万一 - 如果您正确命名,戴西的示例应该可以工作。 这是我的图像调整大小类中使用的修改版本。 请注意检查以确保 imagecolorallocatealpha() 已定义,如果您使用 GD <2.0.8,则不会定义该检查
old thread, but just in case - Dycey's example should work, if you name things correctly. Here is a modified version used in my image resizing class. Notice the check to make sure imagecolorallocatealpha() is defined, which it won't be if you are using GD <2.0.8
它可能与较新版本的 PHP 有关(我使用 PHP 5.6 进行了测试),但现在不需要用透明背景填充图像:
It's probably related to the newer versions of PHP (I tested with PHP 5.6) but this now works without the need to fill the image with a transparent background:
这对我来说也不起作用:(
这是我的解决方案..但我也得到黑色背景并且图像不透明
this is also not working for me :(
thisis my solution.. but i also get a black background and the image is not transparent
这是适用于 png 文件并保留其图像透明度的完整代码。
Here is full code working for png files with preserving their images transparency..
您可以简单地使用 imagescale() 调整图像大小
这段代码对我来说效果很好:
You can simply resize the image with imagescale()
This code works fine for me:
完整的例子。 请注意,对于在互联网上找到的一些 png 图像,它工作不正确,但对于我自己用 Photoshop 创建的图像,它工作正常。
Full example. Notice that for some png images found on internet it works incorrect, but for my own created with photoshop it works fine.
上述解决方案也不适合我。 这就是我发现解决问题的方法。
Nor the above solution worked for me. This is the way what i found out to solve the issue.