复制和调整图像大小会改变颜色

时间:2013-06-29 19:52:44

标签: php image-processing scale

原始

Original

缩放

Scaled

我有一个脚本可以复制服务器上的图像并调整其大小。它的扩展非常好,但输出文件的颜色不同。

质量设置为最大,是否有其他设置我可以更改以获得更好的副本?

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;
    }
}

下面是一个输出有关这两个图像的一些信息的脚本

$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);

输出

原始

Array ( [sig] => ÿØÿàJF [chunksize] => 1229324289 [chunktype] => , [width] => 19660800 [height] => 4292942560 [bit-depth] => 69 [color] => 120 [compression] => 105 [filter] => 102 [interface] => 0 )

缩放

Array ( [sig] => ÿØÿàJF [chunksize] => 1229324289 [chunktype] =>  [width] => 65536 [height] => 4294836284 [bit-depth] => 67 [color] => 82 [compression] => 69 [filter] => 65 [interface] => 84 )

原始文件(1080p)为580K,缩放文件(720p)为638K

0 个答案:

没有答案