简体   繁体   English

如何在PHP中使用透明性最小化和调整PNG图像的大小

[英]How to minimize and resize PNG images with transparency in php

I have 512 * 512 big size PNG images with transparency. 我有透明的512 * 512大尺寸 PNG图片。

How to create 256 * 256 PNG images with smaller file size, which also support transparency and maintaining quality. 如何创建具有较小文件大小的256 * 256 PNG图像,该图像还支持透明度和保持质量。

EDIT: I'm using this code but the output image is cropped and not supporting transparency. 编辑:我正在使用此代码,但输出图像被裁剪并且不支持透明度。

   $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);

You can use ImageMagick : 您可以使用ImageMagick

Simple example: 简单的例子:

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

More info 更多信息

try this, 尝试这个,

$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(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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