在php中使用透明度调整图像大小

在php中使用透明度调整图像大小,php,resize,png,gd,alpha,Php,Resize,Png,Gd,Alpha,当我调整png的大小时,我已经到处寻找如何正确地管理alpha。我已经设法让它保持透明度,但只适用于完全透明的像素。这是我的密码: $src_image = imagecreatefrompng($file_dir.$this->file_name); $dst_image = imagecreatetruecolor($this->new_image_width, $this->new_image_height); imagealphablending($dst_image,

当我调整png的大小时,我已经到处寻找如何正确地管理alpha。我已经设法让它保持透明度,但只适用于完全透明的像素。这是我的密码:

$src_image = imagecreatefrompng($file_dir.$this->file_name);
$dst_image = imagecreatetruecolor($this->new_image_width, $this->new_image_height);
imagealphablending($dst_image, true);
imagesavealpha($dst_image, true);
$black = imagecolorallocate($dst_image, 0, 0, 0);
imagecolortransparent($dst_image, $black);
imagecopyresampled($dst_image, $src_image, 0, 0, 0, 0, $this->new_image_width, 
                 $this->new_image_height, $this->image_width, $this->image_height);
imagepng($dst_image, $file_dir.$this->file_name);
从这个源映像开始:

调整大小的图像如下所示:

imagealphablending($dst_image, false);
$transparent = imagecolorallocatealpha($dst_image, 0, 0, 0, 127);
imagefill($dst_image, 0, 0, $transparent);

关于这个问题,我看过的几乎所有论坛帖子的解决方案都是这样的:

imagealphablending($dst_image, false);
$transparent = imagecolorallocatealpha($dst_image, 0, 0, 0, 127);
imagefill($dst_image, 0, 0, $transparent);
此代码的结果在保存任何alpha时失败:


还有其他解决办法吗?我是不是错过了阿尔法混合?为什么这对其他人都有效,而对我却完全失败?我正在使用MAMP 2.1.3和PHP5.3.15。

我使用simpleImage类来调整图像大小。 您可以通过保持纵横比来重新调整图像大小。 该类使用imagecreatetruecolorimagecopyresampled核心Php函数来重新调整图像大小

  $new_image = imagecreatetruecolor($width, $height);
  imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
  $this->image = $new_image;
找到完整的代码在

你一定是做错了什么。链接副本中的代码添加了两行以加载和保存图像:

$im = imagecreatefrompng(PATH_TO_ROOT."var/tmp/7Nsft.png");

$srcWidth = imagesx($im);
$srcHeight = imagesy($im);

$nWidth = intval($srcWidth / 4);
$nHeight = intval($srcHeight /4);

$newImg = imagecreatetruecolor($nWidth, $nHeight);
imagealphablending($newImg, false);
imagesavealpha($newImg,true);
$transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
imagefilledrectangle($newImg, 0, 0, $nWidth, $nHeight, $transparent);
imagecopyresampled($newImg, $im, 0, 0, 0, 0, $nWidth, $nHeight,
    $srcWidth, $srcHeight);

imagepng($newImg, PATH_TO_ROOT."var/tmp/newTest.png");
生成图像:

$im = imagecreatefrompng(PATH_TO_ROOT."var/tmp/7Nsft.png");

$srcWidth = imagesx($im);
$srcHeight = imagesy($im);

$nWidth = intval($srcWidth / 4);
$nHeight = intval($srcHeight /4);

$newImg = imagecreatetruecolor($nWidth, $nHeight);
imagealphablending($newImg, false);
imagesavealpha($newImg,true);
$transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
imagefilledrectangle($newImg, 0, 0, $nWidth, $nHeight, $transparent);
imagecopyresampled($newImg, $im, 0, 0, 0, 0, $nWidth, $nHeight,
    $srcWidth, $srcHeight);

imagepng($newImg, PATH_TO_ROOT."var/tmp/newTest.png");


i、 e.这个问题(和答案)是完全重复的。

可能重复的问题。我已经尝试了那篇文章(和其他许多文章)中给出的解决方案。它们根本不起作用,我也不知道为什么。这不是一个问题,但它是离题的,因为asker专门针对PHP的内置GD库。。。(这应该是评论,而不是回答)你们都有什么平台,什么版本的PHP/GD/Imagemagick?这可能就是原因。或者,堆栈溢出可能会重新处理源映像,因此您不会测试同一映像。