Problem: Script seems to be running slow. This script is inside a function that is run four times for different image sizes. Is there any way to speed up the code below?
$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);
Use ImageMagick, which is core to the php family and very speedy.
精彩评论