【发布时间】:2016-07-08 20:46:33
【问题描述】:
我们得到一个 jpg,其中包含由黑线组成的绘图。我们确实裁剪它,旋转它,使白色透明,然后最后将其调整为标准宽度。
在我调整它的大小之前,图像(在 $src 中)正是我希望它在正确位置具有透明度的样子。重新采样后,图像 ($out) 恢复为白色背景。 (注释掉的行是我尝试过的一些东西。)在我找到 an answer to a similar problem, 之前,我没有更改 alpha 混合和 alpha 保存的设置,并且至少有一些非常嘈杂的透明度。
如何让重新采样的图像将白色变为透明?
编辑: 在 $out 中,我看到大多数像素是 255、255、255。有些是 252、252、252。少数是 245、245、245。这些是仅有的 3 个值我在 $out 中看到了。我不明白为什么 $out 会出现这种情况,而 $src 则不会。
<?php
$imgname = "../assets/Sample.jpg";
$src = imagecreatefromjpeg($imgname);
$src = imagecropauto($src, IMG_CROP_WHITE);
$white = imagecolorallocate($src, 255, 255, 255);
imagecolortransparent($src, $white);
$src = imagerotate($src, -90, 0);
// Resample
$width = imagesx($src);
$height = imagesy($src);
$percent = 270/$width;
$new_width = $width * $percent;
$new_height = $height * $percent;
$out = imagecreatetruecolor($new_width, $new_height);
//imagefill($out, 0,0, imagecolorallocate($out, 255, 255, 255));
imagealphablending( $out, false );
imagesavealpha( $out, true );
imagecopyresampled($out, $src, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
$white2 = imagecolorallocate($out, 255, 255, 255);
imagecolortransparent($out, $white2);
header("Content-type: image/png");
// imagepng($src);
imagepng($out);
?>
【问题讨论】: