使用 PHP 将 PNG 转换为 JPG 并进行压缩?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1201798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use PHP to convert PNG to JPG with compression?
提问by John
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.
我有一堆高质量的 PNG 文件。我想使用 PHP 将它们转换为 JPG,因为它在保持质量的同时较小的文件大小。我想在网络上显示 JPG 文件。
Does PHP have functions/libraries to do this? Is the quality/compression any good?
PHP 有函数/库来做到这一点吗?质量/压缩有什么好处吗?
回答by Daniel De León
Do this to convert safely a PNG to JPG with the transparency in white.
这样做可以安全地将 PNG 转换为具有白色透明度的 JPG。
$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);
回答by Andrew Moore
Be careful of what you want to convert. JPG doesn't support alpha-transparency while PNG does. You will lose that information.
请注意您要转换的内容。JPG 不支持 alpha 透明度,而 PNG 支持。您将丢失该信息。
To convert, you may use the following function:
要转换,您可以使用以下函数:
// Quality is a number between 0 (best compression) and 100 (best quality)
function png2jpg($originalFile, $outputFile, $quality) {
$image = imagecreatefrompng($originalFile);
imagejpeg($image, $outputFile, $quality);
imagedestroy($image);
}
This function uses the imagecreatefrompng()and the imagejpeg()functions from the GD library.
此函数使用imagecreatefrompng()和imagejpeg()GD 库中的函数。
回答by Al.
This is a small example that will convert 'image.png' to 'image.jpg' at 70% image quality:
这是一个小示例,它将以 70% 的图像质量将“image.png”转换为“image.jpg”:
<?php
$image = imagecreatefrompng('image.png');
imagejpeg($image, 'image.jpg', 70);
imagedestroy($image);
?>
Hope that helps
希望有帮助
回答by Patrik
<?php
function createThumbnail($imageDirectory, $imageName, $thumbDirectory, $thumbWidth) {
$explode = explode(".", $imageName);
$filetype = $explode[1];
if ($filetype == 'jpg') {
$srcImg = imagecreatefromjpeg("$imageDirectory/$imageName");
} else
if ($filetype == 'jpeg') {
$srcImg = imagecreatefromjpeg("$imageDirectory/$imageName");
} else
if ($filetype == 'png') {
$srcImg = imagecreatefrompng("$imageDirectory/$imageName");
} else
if ($filetype == 'gif') {
$srcImg = imagecreatefromgif("$imageDirectory/$imageName");
}
$origWidth = imagesx($srcImg);
$origHeight = imagesy($srcImg);
$ratio = $origWidth / $thumbWidth;
$thumbHeight = $origHeight / $ratio;
$thumbImg = imagecreatetruecolor($thumbWidth, $thumbHeight);
imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $origWidth, $origHeight);
if ($filetype == 'jpg') {
imagejpeg($thumbImg, "$thumbDirectory/$imageName");
} else
if ($filetype == 'jpeg') {
imagejpeg($thumbImg, "$thumbDirectory/$imageName");
} else
if ($filetype == 'png') {
imagepng($thumbImg, "$thumbDirectory/$imageName");
} else
if ($filetype == 'gif') {
imagegif($thumbImg, "$thumbDirectory/$imageName");
}
}
?>
This is a very good thumbnail script =) Here's an example:
这是一个非常好的缩略图脚本 =) 这是一个例子:
$path = The path to the folder where the original picture is. $name = The filename of the file you want to make a thumbnail of. $thumbpath = The path to the directory where you want the thumbnail to be saved into. $maxwidth = the maximum width of the thumbnail in PX eg. 100 (wich will be 100px).
$path = 原始图片所在文件夹的路径。$name = 要制作缩略图的文件的文件名。$thumbpath = 要保存缩略图的目录的路径。$maxwidth = PX 中缩略图的最大宽度,例如。100(将是 100 像素)。
createThumbnail($path, $name, $thumbpath, $maxwidth);
createThumbnail($path, $name, $thumbpath, $maxwidth);
回答by falstro
You might want to look into Image Magick, usually considered the de facto standard library for image processing. Does require an extra php module to be installed though, not sure if any/which are available in a default installation.
您可能想查看Image Magick,它通常被认为是图像处理的事实上的标准库。不过,确实需要安装额外的 php 模块,不确定默认安装中是否有任何/哪些可用。
HTH.
哈。
回答by Gumbo
PHP has some image processing functionsalong with the imagecreatefrompngand imagejpegfunction. The first will create an internal representation of a PNG image file while the second is used to save that representation as JPEG image file.
PHP 有一些图像处理函数以及imagecreatefrompng和imagejpeg函数。第一个将创建 PNG 图像文件的内部表示,而第二个用于将该表示保存为 JPEG 图像文件。
回答by Draemon
See this list of php image libraries. Basically it's GD or Imagemagick.
请参阅此 php 图像库列表。基本上它是GD或Imagemagick。
回答by Pascal MARTIN
I know it's not an exact answer to the OP, but as answers have already be given...
我知道这不是 OP 的确切答案,但由于已经给出了答案......
Do you really need to do this in PHP ?
What I mean is : if you need to convert a lot of images, doing it in PHP might not be the best way : you'll be confronted to memory_limit, max_execution_time, ...
您真的需要在 PHP 中执行此操作吗?
我的意思是:如果您需要转换大量图像,在 PHP 中进行转换可能不是最好的方法:您将面临memory_limit, max_execution_time, ...
I would also say GD might not get you the best quality/size ratio ; but not sure about that (if you do a comparison between GD and other solutions, I am very interested by the results ;-) )
我还要说 GD 可能无法为您提供最佳的质量/尺寸比;但不确定(如果您对 GD 和其他解决方案进行比较,我对结果非常感兴趣;-))
Another approach, not using PHP, would be to use Image Magickvia the command line (and not as a PHP extension like other people suggested)
另一种不使用 PHP 的方法是通过命令行使用Image Magick (而不是像其他人建议的那样作为 PHP 扩展)
You'd have to write a shell-script that goes through all .pngfiles, and gives them to either
您必须编写一个遍历所有.png文件的 shell 脚本,并将它们提供给
convertto create a new.jpgfile for each.pngfile- or
mogrifyto directly work on the original file and override it.
As a sidenote : if you are doing this directly on your production server, you could put some sleep time between bunches of conversions, to let it cool down a bit sometimes ^^
作为旁注:如果您直接在生产服务器上执行此操作,则可以在一系列转换之间放置一些睡眠时间,以便有时让它冷却一下^^
I've use the shell script + convert/mogrifya few times (having them run for something like 10 hours one time), and they do the job really well :-)
我已经使用过 shell 脚本 + convert/mogrify几次(一次让它们运行 10 个小时左右),而且它们的工作做得非常好:-)

