简体   繁体   中英

Resizing images with `imagecopyresampled` from PHP GD

Using the PHP GD library, I generate image resource that is opaque white, with some "holes" here and there, which are fully transparent disk shapes. I've attached it bellow, although you'll need to first save it and open it (in Windows) with the Image Preview application in order to see the holes in it, due to the blue-ish background. Otherwise you'll only see white.

The original image: 原始图像资源

From that image resource, which is 2550 x 3000px, I need to create a smaller version. I do so using imagecopyresampled() . All is fine with the resulting image, with one exception: here and there, it contains grey pixels (RGB: 254,254,254):

The resized image: 调整大小的图片

Part of the code I use is bellow:

$previewPxWidth = $this->viewportWidth_;
$previewPxHeight = round($this->viewportWidth_ / $schematic['paper.aspect.ratio']);

$preview = imagecreatetruecolor($previewPxWidth, $previewPxHeight);
$noColor = imagecolorallocatealpha($preview, 255, 255, 255, 127);
imagesavealpha($preview,true);
imagefill($preview, 0, 0, $noColor);

imagecopyresampled($preview, $sheet, 0, 0, 0, 0, $previewPxWidth, $previewPxHeight, $sheetPxWidth, $sheetPxHeight);
imagedestroy($sheet);

header("Content-type: image/png");
header("Content-disposition: inline; filename=image.png");
imagepng($preview);

Where are those very light and (apparently) randomly positioned grey pixels coming from and how can I get rid of them?

尝试imageAlphaBlending(),但是ImageMagic是最好的方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM