如何在 php 中最小化和调整具有透明度的 PNG 图像

How to minimize and resize PNG images with transparency in php

我有 512 * 512 大尺寸 透明的 PNG 图片。

如何创建文件大小较小的 256 * 256 PNG 图像,同时支持透明度和保持质量。

编辑:我正在使用此代码,但输出图像被裁剪且不支持透明度。

   $image = imagecreatefrompng("C:\Users\HP\htdocs\icon_hd.png");  // 512 * 512
    $bg = imagecreatetruecolor(256, 256);
    imagefill($bg, 0, 0, 0);
    imagealphablending($bg, TRUE);
    imagecopy($bg, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
    imagedestroy($image);
    $quality = 100;
    imagepng($bg, "C:\Users\HP\out_icon.png", 9);
    imagedestroy($bg);

您可以使用 ImageMagick :

简单示例:

$inFile = "big_img.png";
$outFile = "small_img.png";
$image = new Imagick($inFile);
$image->thumbnailImage(256, 256);
$image->writeImage($outFile);

More info

试试这个,

$im = new Imagick();
$im->readImage('myimage.gif');  
$im->resizeImage(256,256,Imagick::FILTER_LANCZOS,1);
$im->setImageBackgroundColor('white');
$im->setImageAlphaChannel(Imagick::ALPHACHANNEL_REMOVE);
$im->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);
$im->writeImage('mythumb.gif');
$im->clear();
$im->destroy();