简体   繁体   English

加快图像调整大小

[英]Speeding up Image Resizing

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

使用ImageMagick ,它是php家族的核心,而且速度很快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM