php 如何将 Base64 PNG 转换为 JPG 图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14514587/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:25:35  来源:igfitidea点击:

How to convert a Base64 PNG to a JPG image?

phpimagepngbase64jpeg

提问by Nader Khan

I have this Base64 PNG, which I want to decode to JPG. If I convert to PNG it works fine, using:

我有这个 Base64 PNG,我想将其解码为 JPG。如果我转换为 PNG 它工作正常,使用:

list($type, $data) = explode(';', $data);
list(, $data)      = explode(',', $data);
$data = base64_decode($data);
file_put_contents('myDirectory/filename.png', $data);

But if I try to save it as JPG, it comes out in black and white using (MyDirectory/filename.jpg).

但是,如果我尝试将其另存为 JPG,则会使用 ( MyDirectory/filename.jpg)以黑白形式显示出来。

How do I convert it to a JPG? here is an example of my Base64 PNG:

如何将其转换为 JPG?这是我的 Base64 PNG 的示例:

data:image/png;base64,iVBORw0KGgoAAAANSUhE...

回答by JakeGould

Base64 is an encoding format that is strictly used to convert data into a text transportable format. Whatever is in that encoding format needs to be converted further if you want another format. So if you want the PNG to be a JPEG, after the Base64 decode it needs to be converted by another tool into a JPEG. This threadhas some good suggestions. @Andrew Moore who answers the thread recommends using a function like this. Be sure to have the GD library installed as part of your PHP setup:

Base64 是一种严格用于将数据转换为文本可传输格式的编码格式。如果您想要另一种格式,则需要进一步转换该编码格式中的任何内容。所以如果你想让PNG变成JPEG,Base64解码后需要通过其他工具转成JPEG。 这个线程有一些很好的建议。回答该线程的@Andrew Moore 建议使用这样的函数。确保将 GD 库安装为 PHP 设置的一部分:

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

So using your code as an example, you would then use this function to do the following:

因此,以您的代码为例,您将使用此函数执行以下操作:

png2jpg('myDirectory/filename.png','myDirectory/filename.jpg', 100);

Or you can deconstruct the functions of that png2jpgfunction and use them in your code like this:

或者您可以解构该函数的png2jpg函数并在您的代码中使用它们,如下所示:

list($type, $data) = explode(';', $data);
list(, $data)      = explode(',', $data);
$data = base64_decode($data);
file_put_contents('myDirectory/filename.png', $data);
$image = imagecreatefrompng('myDirectory/filename.png');
imagejpeg($image, 'myDirectory/filename.jpg', 100);
imagedestroy($image);

回答by mcuadros

The easiest way to to this since PHP 5.2.0, is using the data://wrapper, you can use it like a file in many of functions.

自 PHP 5.2.0 以来,最简单的方法是使用data://包装器,您可以在许多函数中将其用作文件。

$image = 'data:image/png;base64,iVBORw0KGgoAAAANSUhE...';
$image = imagecreatefrompng($image);
imagejpeg($image, 'myDirectory/filename.jpg', 100);
imagedestroy($image);