Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Image Processing GD JPEG Quality

I started using the WideImage image processing library and I'm having a problem with the quality of the JPEG images it's generating. WideImage actually uses GD, so I'm testing just using the GD PHP image functions.

My aim is ultimately to resize images, but here's my test code with no resizing taking place:

$srcImage = "path/to/image.jpg";
list($width, $height) = getimagesize($srcImage);
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($srcImage);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);
imagejpg($image_p, "path/to/image_resized.jpg", 100);

This works, but outputs a lower quality more washed out version of the original image. Here is an example next to the original split down the center:

Example Image

This happens when I do perform a resize also, but I want to maintain the same colours/quality of the original image.

Has anyone got any ideas as to how I can achieve this? Is there perhaps a setting in my php.ini that I am missing or something? I also tried using imagepng() but with much the same results.

I'm using PHP Version 5.3.29, here is my GD info from phpinfo():

GD Support        : enabled
GD Version        : bundled (2.1.0 compatible)
FreeType Support  : enabled
FreeType Linkage  : with freetype
FreeType Version  : 2.3.11
T1Lib Support     : enabled
GIF Read Support  : enabled
GIF Create Support: enabled
JPEG Support      : enabled
libJPEG Version   : 6b
PNG Support       : enabled
libPNG Version    : 1.2.49
WBMP Support      : enabled
XPM Support       : enabled
libXpm Version    : 30411
XBM Support       : enabled

Thanks!

At squeamish ossifrage's request, here is the original file and the converted file.

EDIT - as per the squeamish ossifrage's answer marked correct below, I took the following steps to solve the issue:

Installed exiftool on the server

Generated the following command with PHP:

exiftool -TagsFromFile "/var/www/vhosts/path/to/image/the file name.jpg" -icc_profile "/var/www/vhosts/path/to/image-processed/the file name.jpg"

Ran the command with PHP's exec() method:

$arrOutput = array();
$return_var = "";
$directOutput = exec($exiftoolCommand, $arrOutput, $return_var);

Worked like a charm!

like image 924
bbeckford Avatar asked Oct 29 '25 10:10

bbeckford


1 Answers

The original image contains an ICC colour profile. GD doesn't know anything about colour profiles, so this information is missing from the converted file. To fix this problem, you basically have two options:

  1. Save the original file without a colour profile, or

  2. Copy the colour profile from the original file to the converted file.

I'd go with option 1 if you're planning to publish this file on the web, as colour profiles and other EXIF information use up bandwidth for no good reason.

Alternatively, you can use a free tool like exiftool to copy the colour profile from one file to the other. This should work on the command line:

exiftool -TagsFromFile so_original.jpg -icc_profile so_converted.jpg
like image 99
r3mainer Avatar answered Oct 31 '25 02:10

r3mainer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!