使用 PHP 将 PNG 转换为 JPG 并进行压缩?

发布于 2024-07-29 08:47:27 字数 122 浏览 2 评论 0 原文

我有一堆高质量的 PNG 文件。 我想使用 PHP 将它们转换为 JPG,因为它的文件大小较小,同时保持质量。 我想在网络上显示 JPG 文件。

PHP 有函数/库来执行此操作吗? 质量/压缩好吗?

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.

Does PHP have functions/libraries to do this? Is the quality/compression any good?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

暗恋未遂 2024-08-05 08:47:27

执行此操作可以将 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);

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);
戈亓 2024-08-05 08:47:27

请小心您要转换的内容。 JPG 不支持 alpha 透明度,而 PNG 支持。 您将丢失该信息。

要进行转换,您可以使用以下函数:

// 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);
}

该函数使用 imagecreatefrompng() 和 GD 库中的 imagejpeg() 函数。

Be careful of what you want to convert. JPG doesn't support alpha-transparency while PNG does. You will lose that information.

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.

猥︴琐丶欲为 2024-08-05 08:47:27

这是一个小例子,它将以 70% 图像质量将“image.png”转换为“image.jpg”:

<?php
$image = imagecreatefrompng('image.png');
imagejpeg($image, 'image.jpg', 70);
imagedestroy($image);
?>

希望有帮助

This is a small example that will convert 'image.png' to 'image.jpg' at 70% image quality:

<?php
$image = imagecreatefrompng('image.png');
imagejpeg($image, 'image.jpg', 70);
imagedestroy($image);
?>

Hope that helps

征﹌骨岁月お 2024-08-05 08:47:27
<?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");
    }
}
    ?>

这是一个非常好的缩略图脚本 =)
下面是一个示例:

$path = 原始图片所在文件夹的路径。
$name = 您要制作缩略图的文件的文件名。
$thumbpath = 您想要保存缩略图的目录的路径。
$maxwidth = PX 中缩略图的最大宽度,例如。 100(即 100px)。

createThumbnail($path, $name, $thumbpath, $maxwidth);

<?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).

createThumbnail($path, $name, $thumbpath, $maxwidth);

抠脚大汉 2024-08-05 08:47:27

您可能需要研究 Image Magick,通常被认为是事实上的图像处理标准库。 确实需要安装额外的 php 模块,但不确定默认安装中是否有可用的模块。

HTH。

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.

HTH.

甜警司 2024-08-05 08:47:27

PHP 有一些图像处理函数以及imagecreatefrompngimagejpeg 函数。 第一个将创建 PNG 图像文件的内部表示,而第二个用于将该表示另存为 JPEG 图像文件。

PHP has some image processing functions along with the imagecreatefrompng and imagejpeg function. 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.

携余温的黄昏 2024-08-05 08:47:27

请参阅此 PHP 图像库列表。 基本上就是 GD 或 Imagemagick。

See this list of php image libraries. Basically it's GD or Imagemagick.

寒尘 2024-08-05 08:47:27

我知道这不是OP的确切答案,但由于答案已经给出......

你真的需要在PHP中执行此操作吗?

我的意思是:如果您需要转换大量图像,用 PHP 进行可能不是最好的方法:您将面临 memory_limitmax_execution_time, ...

我还想说,GD 可能无法为您提供最佳的质量/尺寸比; 但不确定(如果你对 GD 和其他解决方案进行比较,我对结果非常感兴趣;-))

另一种不使用 PHP 的方法是使用 Image Magick 通过命令行(而不是像其他人建议的那样作为 PHP 扩展)

您必须编写一个 shell 脚本来遍历所有 .png 文件,并将它们提供给

作为旁注:如果您直接在生产服务器上执行此操作,您可以在一堆转换之间放置一些睡眠时间,以使其有时冷却一些 ^^

我已经使用了 shell 脚本 + convert/mogrify 几次(让它们一次运行大约 10 个小时),并且它们把工作做得很好:-)

I know it's not an exact answer to the OP, but as answers have already be given...

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, ...

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

Another approach, not using PHP, would be to use Image Magick via the command line (and not as a PHP extension like other people suggested)

You'd have to write a shell-script that goes through all .png files, and gives them to either

  • convert to create a new .jpg file for each .png file
  • or mogrify to 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/mogrify a few times (having them run for something like 10 hours one time), and they do the job really well :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文