0

这是我的创建缩略图代码。缩略图宽度/高度较小 (400)。但文件大小更大(111KB),旧文件大小(95KB)

为什么?如何压缩和缩小文件大小?

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

2 回答 2

2

通过调整的第三个参数来降低输出图像的质量imagejpeg(范围从 0 到 100,当 100 是最佳质量时)。例如:

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

更好的方法是使用 ImageMagick 库和许多其他图像操作

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

于 2012-09-01T09:19:04.983 回答