2010-11-22 60 views
0

问题:脚本似乎运行缓慢。此脚本位于一个函数中,该函数针对不同的图像大小运行四次。有什么方法可以加快下面的代码?加速图像调整大小

$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

这真的很难相信。你100%确定吗?如果这是瓶颈,那么一定是错误的 – 2010-11-22 22:52:07

+0

getimagesize似乎是缓慢的部分,但我也认为这可能是我调整图像大小的方式。用更多信息更新了帖子。 – stwhite 2010-11-22 22:54:57

回答

0

使用ImageMagick,这是php家族的核心,非常快速。