簡體   English   中英

加快圖像調整大小

[英]Speeding up Image Resizing

問題:腳本似乎運行緩慢。 該腳本位於一個函數內,該函數針對不同的圖像大小運行四次。 有什么辦法可以加快下面的代碼?

$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