使用PHP通过压缩将PNG转换为JPG? [英] Use PHP to convert PNG to JPG with compression?
本文介绍了使用PHP通过压缩将PNG转换为JPG?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一堆高质量的PNG文件.我想使用PHP将它们转换为JPG,因为它在保持质量的同时较小的文件大小.我想在网上显示JPG文件.
I have a bunch of high quality PNG files. I want to use PHP to convert them to JPG because of it's smaller file sizes while maintaining quality. I want to display the JPG files on the web.
PHP是否具有执行此功能的函数/库?质量/压缩效果好吗?
Does PHP have functions/libraries to do this? Is the quality/compression any good?
推荐答案
执行此操作可将PNG安全地转换为具有白色透明性的JPG.
Do this to convert safely a PNG to JPG with the transparency in white.
$image = imagecreatefrompng($filePath);
$bg = imagecreatetruecolor(imagesx($image), imagesy($image));
imagefill($bg, 0, 0, imagecolorallocate($bg, 255, 255, 255));
imagealphablending($bg, TRUE);
imagecopy($bg, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
imagedestroy($image);
$quality = 50; // 0 = worst / smaller file, 100 = better / bigger file
imagejpeg($bg, $filePath . ".jpg", $quality);
imagedestroy($bg);
这篇关于使用PHP通过压缩将PNG转换为JPG?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文