Original
Scaled
I have a script that copies and resizes an image on the server. It scales really good but the color differs on the outputted file.
The quality is set to max, are there any other settings I can change to get a better copy?
Example with original on top and scaled below
$directory = 'path/to/directory';
$image_file = 'filename.extension';
$image = $directory.$image;
$destination = 'path/to/new/directory';
$source_size = getimagesize($image);
if ($source_size !== false) {
switch($source_size['mime']) {
case 'image/jpeg':
$source = imagecreatefromjpeg($image);
break;
case 'image/png':
$source = imagecreatefrompng($image);
break;
}
// Set a maximum height and width
$width = 1280;
$height = 2000;
// Get old dimensions
list($width_orig, $height_orig) = getimagesize($image);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
// Resample
$thumb = imagecreatetruecolor($width, $height);
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
switch($source_size['mime']) {
case 'image/jpeg':
imagejpeg($thumb, $destination.$image_file, 100);
break;
case 'image/png':
imagepng($thumb, $destination.$image_file, 9);
break;
}
}
Below is a script that outputs some information about the two images
$file = 'original.jpg';
$info = unpack('A8sig/Nchunksize/A4chunktype/Nwidth/Nheight/Cbit-depth/'.
'Ccolor/Ccompression/Cfilter/Cinterface',
file_get_contents($file,0,null,0,29))
;
print_r($info);
$file = 'scaled.jpg';
$info = unpack('A8sig/Nchunksize/A4chunktype/Nwidth/Nheight/Cbit-depth/'.
'Ccolor/Ccompression/Cfilter/Cinterface',
file_get_contents($file,0,null,0,29))
;
print_r($info);
Output
Original
Array ( [sig] => ÿØÿàJF [chunksize] => 1229324289 [chunktype] => , [width] => 19660800 [height] => 4292942560 [bit-depth] => 69 [color] => 120 [compression] => 105 [filter] => 102 [interface] => 0 )
Scaled
Array ( [sig] => ÿØÿàJF [chunksize] => 1229324289 [chunktype] => [width] => 65536 [height] => 4294836284 [bit-depth] => 67 [color] => 82 [compression] => 69 [filter] => 65 [interface] => 84 )
The original file (1080p) is 580K and the scaled file (720p) is 638K