0

これが私のサムネイル作成コードです。サムネイルの幅/高さが小さくなっています (400)。ただし、ファイルサイズは大きく (111KB)、古いファイルサイズ (95KB)

なぜ?圧縮してファイルサイズを小さくするにはどうすればよいですか?

create_thumbnail( 'http://upload.wikimedia.org/wikipedia/en/2/2e/ドラえもん_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);
    } 
}
4

2 に答える 2

2

の 3 番目のパラメーターを微調整して、出力イメージの品質を下げますimagejpeg(100 が最高の品質の場合、0 から 100 の範囲)。例えば:

imagejpeg($thumbnail, $destination_file, 80);
于 2012-09-01T08:11:21.600 に答える
-1

同じことを行うより良い方法は、ImageMagick Library を使用して、他の多くの画像操作を行うことです。

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

于 2012-09-01T09:19:04.983 に答える