PHP创建缩略图功能,使文件尺寸更小,但文件大小更大?

时间:2012-09-01 08:08:49

标签: php file thumbnails

这是我创建的缩略图代码。 缩略图宽度/高度较小(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);
    } 
}

2 个答案:

答案 0 :(得分:2)

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

imagejpeg($thumbnail, $destination_file, 80);

答案 1 :(得分:-1)

更好的方法是使用ImageMagick Library和其他一系列图像处理

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