0

Original

Original

Scaled

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

Oskar Persson
  • 6,605
  • 15
  • 63
  • 124
  • take a look at this post: http://stackoverflow.com/questions/2611852/imagecreatefrompng-makes-a-black-background-instead-of-transparent – Casimir et Hippolyte Jun 29 '13 at 19:57
  • I don't think that it's a transparecy problem since my image is a jpeg. – Oskar Persson Jun 29 '13 at 21:06
  • What about but depht? Is it possible you are reducing the bit depth of the image? – jnovacho Jun 30 '13 at 09:14
  • @jnovacho Edited by question with included bit-depth – Oskar Persson Jun 30 '13 at 11:20
  • I don't know what library you are using, but from the $info it's one can tell, that the those two images are clearly different. I would suggest to dig into the docs, and look into saving options. I would guess, the problem is writing images to disk, rather than resizing. – jnovacho Jun 30 '13 at 12:46

0 Answers0