PHP/GD: How to handle jpg transparency?

点点圈 提交于 2021-01-29 07:16:25

问题


JPG doesn't support transparency.

Yet you can export an image with transparent areas from for example GIMP. In that case the transparent areas will show either black or white in your browser, Windows explorer or other software.

I regularly receive such files that I need to process and provide in several smaller formats. I do that with some PHP script using the GD library. The problem is that it is unpredictable whether those areas will turn out black or white in the processed versions. Even white backgrounds can turn into black.

So my question is whether it is in some way possible to detect this "transparent" area in GD and assign it a specific color.

See for example those two images: http://www.bilbil.com/9156-thickbox_default/bus.jpg http://bilbil.com/img/p/9/1/5/6/9156.jpg

At the moment I use this code:

$srcfile = "9158.jpg";
$dir ="/test/";
list($width, $height, $imgtype, $attr) = @getimagesize( $dir.$srcfile );
$src = imagecreatefromjpeg($dir.$srcfile);
$newheight=$newwidth = 800;
$img = imagecreatetruecolor ( $newwidth , $newheight );
$white = imagecolorallocate($img, 255, 255, 255);
imagefill($img, 0, 0, $white);
imagecopyresampled($img, $src, ($newwidth-$width)/2, ($newheight-$height)/2, 0, 0, $width, $height, $width, $height);
if(!imagejpeg($img, $dir.'test.jpg', 97))
  echo("Error creating testimage");

回答1:


In almost all the cases, you can use the top-left pixel color as the transparent color.




回答2:


You can give a chance to ImageMagick. The following command will convert .jpeg file into .png with transparency.

convert bus.jpg \
    -alpha set -fill none -fuzz 1% \
    -bordercolor white -border 1 -draw 'color 0,0 floodfill' -shave 1x1 \
    -bordercolor black -border 1 -draw 'color 0,0 floodfill' -shave 1x1 \
    a.png

Here is how it works:

  1. Add white border around whole image
  2. Floodfill white color (and all colors similar to it by 1%) by transparent starting at top-left corner
  3. Remove extra border around image
  4. Add black border around whole image
  5. Floodfill black color (and all colors similar to it by 1%) by transparent starting at top-left corner
  6. Remove extra border around image
  7. Write result image to file a.png

And this is a snippet for PHP:

<?php

function jpg_to_png($jpg, $png)
{
$templ = trim("
convert {{jpg}} \
    -alpha set -fill none -fuzz 1% \
    -bordercolor white -border 1 -draw 'color 0,0 floodfill' -shave 1x1 \
    -bordercolor black -border 1 -draw 'color 0,0 floodfill' -shave 1x1 \
    {{png}}
");
    $cmd = strtr($templ, [
        '{{jpg}}' => escapeshellarg($jpg),
        '{{png}}' => escapeshellarg($png)
    ]);
    exec($cmd, $stdout, $exitcode);
    if ($exitcode) {
        throw new Error("Command failed with code $exitcode");
    }
}

jpg_to_png('bus.jpg', 'a.png');

The following command (ref) might also be helpful in your case:

convert bus.jpg -fuzz 1% -trim +repage a.jpg

It will remove all "white space" from input image.




回答3:


I am not sure what you are asking. Are you starting with a non-JPG image with transparency and converting to JPG? Or do you already have the JPG.

If you are starting with a transparent image such as PNG or TIFF and want to convert to JPG and always have a black (or any other color) background, then in Imagemagick, you can do:

convert image.png -background black -alpha background -alpha off image.jpg


or

convert image.png -background black -flatten image.jpg


Replace black with any other color that you want for where it was transparent, such as white.

If you already have a JPG with some color and want to change it to black, then do:

convert image.jpg -fuzz XX% -fill black -opaque somecolor image.jpg


where XX% is the percent you want to consider close to "somecolor" for which you want to change "somecolor" and nearby colors to black.

Sorry, I do not know GD. But I expect that it has similar functionality. Or you can use PHP Imagick, which is built upon Imagemagick.



来源:https://stackoverflow.com/questions/54044844/php-gd-how-to-handle-jpg-transparency

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!