加快图像大小调整

时间:2010-11-22 22:50:15

标签: php image performance gd

问题:脚本似乎运行缓慢。此脚本位于针对不同图像大小运行四次的函数内。有没有办法加快下面的代码?

$outputFile = "../data/assets/temp.jpg";
$maxTempWidth  = 45;
$maxTempHeight = 45;
$image_info = getimagesize($setXsmallNewName);

if($image_info['mime'] == 'image/jpeg'){
$image = imagecreatefromjpeg($setXsmallNewName);
}elseif($image_info['mime'] == 'image/gif'){
$image = imagecreatefromgif($setXsmallNewName);
}elseif($image_info['mime'] == 'image/png'||$image_info['mime'] == 'image/x-png'){
    $image = imagecreatefrompng($setXsmallNewName);
}

$width = imagesx( $image );
$height = imagesy( $image );

if ($width > $maxTempWidth || $height > $maxTempHeight){   
    if ( $width > $height ){
        $newwidth = $maxTempWidth;
        $ratio = $maxTempWidth / $width;
        $newheight = floor($height * $ratio);

        if ($newheight > $maxTempHeight){
            $newheight = $maxTempHeight;
            $ratio = $maxTempHeight / $height;
            $newWidth = floor($width * $ratio);
        }
    }else{
        $newheight = $maxTempHeight;
        $ratio = $maxTempHeight / $height;
        $newwidth = floor($width * $ratio);

        if ($newwidth > $maxTempWidth){
            $newwidth = $maxTempWidth;
            $ratio = $maxTempWidth / $width;
            $newheight = floor($height * $ratio);
        }
    }
}else{
    $newwidth = $width;
    $newheight = $height;
}   
$final_image = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($final_image, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

1 个答案:

答案 0 :(得分:0)

使用ImageMagick,这是php系列的核心,速度非常快。