2012-03-09 10:32
浏览 116

PHP PNG到JPG转换问题,图像失真

I have a script that uploads and resizes images using the GD library and PHP. I now want to upload a PNG image and for it to be stored as PNG and JPEG, I am then going to resize it but that's fine.

The problem I'm having is that sometimes, the converted version of the image (jpg) is distorted. Other times it is fine.

My code is taken from another answer here on StackOverflow:

function png2jpg($originalFile, $outputFile, $quality){
    $image = imagecreatefrompng($originalFile);
    imagejpeg($image, $outputFile, $quality);

An example of the distorted result is shown below, I am fully aware that I won't get the transparency on the JPG, I just want a white background. Any ideas?

I can't post images, so link to original: and JPG:

图片转代码服务由CSDN问答提供 功能建议

我有一个脚本,可以使用GD库和PHP上传和调整图像大小。 我现在要上传PNG 图像,并将其存储为PNG和JPEG,然后我会调整它的大小但是没关系。

我遇到的问题是有时,图像的转换版本 (jpg)被扭曲了。 其他时间没问题。


  function png2jpg($ originalFile,$ outputFile  ,$ quality){
 $ image = imagecreatefrompng($ originalFile); 
 imagejpeg($ image,$ outputFile,$ quality); 
 imagedestroy($ image); 

下面显示了扭曲结果的一个例子,我完全清楚我不会获得JPG的透明度,我只想要一个白色背景。 有什么想法吗?

我无法发布图片,因此链接到原文: ) 和JPG:

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • dousuohe5882 2012-03-09 10:48

    You need to create a fresh image with a white (or whatever you want) background and copy the none-transparent pixels from the png to that image:

    function png2jpg($originalFile, $outputFile, $quality) {
        $source = imagecreatefrompng($originalFile);
        $image = imagecreatetruecolor(imagesx($source), imagesy($source));
        $white = imagecolorallocate($image, 255, 255, 255);
        imagefill($image, 0, 0, $white);
        imagecopy($image, $source, 0, 0, 0, 0, imagesx($image), imagesy($image));
        imagejpeg($image, $outputFile, $quality);
    点赞 打赏 评论
  • doujia1871 2012-03-09 10:36

    PHP will only copy pixels from PNG files, and if your png has transparency this will not be handled by PHP resulting in what you see after conversion.

    点赞 打赏 评论
  • douguanyun2169 2012-03-09 10:38

    JPG format does not support transparent pixels.

    Instead you can replace transparent pixels with white/black and then make the conversion:

    function png2jpg($originalFile, $outputFile, $quality){
        $size = getimagesize($originalFile);
        $blank = imagecreate($size[0], $size[1]);
        $newImage = imagecopymerge($blank, $originalFile, 0, 0, 0, 0, $size[0], $size[1], $quality);
        png2jpg($newImage, $outputFile, );
        $image = imagecreatefrompng($newImage);
        imagejpeg($image, $outputFile, $quality);
    点赞 打赏 评论
  • dongxing5525 2012-03-09 10:40

    Maybe if you try to make an image from png on top of a white image you could repair this problem.

    点赞 打赏 评论
  • douyi1966 2012-03-23 22:05

    There was a library I came across in which is basically a wrapper over the GD library of PHP. It was called GDImageManipulation or something. Its a simple one file class, and it handles most of the preliminary image operations that you are talking about.

    Take a look at it. It might help.

    点赞 打赏 评论

相关推荐 更多相似问题