PHP создает функцию миниатюр, уменьшает размер файла, но увеличивает размер файла?

вот мой код создания эскиза. ширина/высота эскиза меньше (400). но размер файла больше (111 КБ), старый размер файла (95 КБ)

Почему? как я могу сжать и уменьшить размер файла?

create_thumbnail('http://upload.wikimedia.org/wikipedia/en/2/2e/Doraemon_first_appearance.jpg', './test.jpg', 400,'w','jpg' );

function create_thumbnail( $source_file, $destination_file, $max_dimension, $one_dimension=false,$source_file_type=false)
{
    list($img_width,$img_height) = getimagesize($source_file); // Get the original dimentions
    $aspect_ratio = $img_width / $img_height;

    // If either dimension is too big...
    if ( ($img_width > $max_dimension) || ($img_height > $max_dimension) ) 
    {
        if ( $img_width > $img_height ) // For wide images...
        {
            if($one_dimension=='h')
            {
                $new_width = $img_width * $max_dimension / $img_height;
                $new_height = $max_dimension;
            }
            else
            {
                $new_width = $max_dimension;
                $new_height = $new_width / $aspect_ratio;
            }
        }
        elseif ( $img_width < $img_height ) // For tall images...
        {
            if($one_dimension=='w')
            {
                $new_width = $max_dimension;
                $new_height = $img_height * $max_dimension / $img_width;
            }
            else
            {
                $new_height = $max_dimension;
                $new_width = $new_height * $aspect_ratio;
            }
        }
        elseif ( $img_width == $img_height ) // For square images...
        {
           $new_width = $max_dimension;
           $new_height = $max_dimension;
        }
        else { echo "Error reading image size."; return FALSE; }

        // Make sure these are integers.
        $new_width = intval($new_width);
        $new_height = intval($new_height);

        $thumbnail = imagecreatetruecolor($new_width,$new_height); // Creates a new image in memory.

        // The following block retrieves the source file.  It assumes the filename extensions match the file's format.
        if(!$source_file_type) $source_file_type = get_file_ext($source_file,true);

        if ( $source_file_type=="gif" ) { $img_source = ImageCreateFromGIF($source_file); }
        elseif ( ($source_file_type=="jpg") || ($source_file_type=="jpeg") || ($source_file_type=="pjpeg") ) { $img_source = imagecreatefromjpeg($source_file); }
        elseif ( $source_file_type=="bmp" ) { $img_source = imagecreatefromwbmp($source_file); }
        elseif ( $source_file_type=="png" ) { $img_source = imagecreatefrompng($source_file); }

        // Here we resample and create the new jpeg.
        imagecopyresampled($thumbnail, $img_source, 0, 0, 0, 0, $new_width, $new_height, $img_width, $img_height);
        imagejpeg( $thumbnail, $destination_file, 100 );

        // Finally, we destroy the two images in memory.
        imagedestroy($img_source);
        imagedestroy($thumbnail);
    }
    // If it's already smaller, don't change the size.
    else { 
        copy($source_file, $destination_file);
    } 
}

person TomSawyer    schedule 01.09.2012    source источник


Ответы (2)


Снизьте качество выходного изображения, настроив третий параметр imagejpeg (диапазон от 0 до 100, когда 100 — наилучшее качество). Например:

imagejpeg($thumbnail, $destination_file, 80);
person Kuba Wyrostek    schedule 01.09.2012

Лучший способ сделать то же самое — использовать библиотеку ImageMagick и множество других манипуляций с изображениями.

http://www.imagemagick.org/script/index.php

person Brighton Vino    schedule 01.09.2012
comment
Не совсем отвечает на вопрос. - person Michael; 01.09.2012