当前位置:Gxlcms > PHP教程 > 关于php图片等比例缩放函数的.谁能帮我看看我这个函数怎么用啊?

关于php图片等比例缩放函数的.谁能帮我看看我这个函数怎么用啊?

时间:2021-07-01 10:21:17 帮助过:24人阅读

function resize($srcImage,$toFile,$maxWidth = 100,$maxHeight = 100,$imgQuality=100) 
{ 
   
    list($width, $height, $type, $attr) = getimagesize($srcImage); 
    if($width < $maxWidth  || $height < $maxHeight) return ; 
    switch ($type) { 
    case 1: $img = imagecreatefromgif($srcImage); break; 
    case 2: $img = imagecreatefromjpeg($srcImage); break; 
    case 3: $img = imagecreatefrompng($srcImage); break; 
    } 
    $scale = min($maxWidth/$width, $maxHeight/$height); //求出绽放比例 
     
    if($scale < 1) { 
    $newWidth = floor($scale*$width); 
    $newHeight = floor($scale*$height); 
    $newImg = imagecreatetruecolor($newWidth, $newHeight); 
    imagecopyresampled($newImg, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); 
    $newName = ""; 
    $toFile = preg_replace("/(.gif|.jpg|.jpeg|.png)/i","",$toFile); 
 
    switch($type) { 
        case 1: if(imagegif($newImg, "$toFile$newName.gif", $imgQuality)) 
        return "$newName.gif"; break; 
        case 2: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality)) 
        return "$newName.jpg"; break; 
        case 3: if(imagepng($newImg, "$toFile$newName.png", $imgQuality)) 
        return "$newName.png"; break; 
        default: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality))
        return "$newName.jpg"; break; 
    } 
    imagedestroy($newImg); 
    } 
    imagedestroy($img); 
    return false; 
}
其中 $toFile = preg_replace("/(.gif|.jpg|.jpeg|.png)/i","",$toFile);具体目的是什么?

回复内容:

function resize($srcImage,$toFile,$maxWidth = 100,$maxHeight = 100,$imgQuality=100) 
{ 
   
    list($width, $height, $type, $attr) = getimagesize($srcImage); 
    if($width < $maxWidth  || $height < $maxHeight) return ; 
    switch ($type) { 
    case 1: $img = imagecreatefromgif($srcImage); break; 
    case 2: $img = imagecreatefromjpeg($srcImage); break; 
    case 3: $img = imagecreatefrompng($srcImage); break; 
    } 
    $scale = min($maxWidth/$width, $maxHeight/$height); //求出绽放比例 
     
    if($scale < 1) { 
    $newWidth = floor($scale*$width); 
    $newHeight = floor($scale*$height); 
    $newImg = imagecreatetruecolor($newWidth, $newHeight); 
    imagecopyresampled($newImg, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); 
    $newName = ""; 
    $toFile = preg_replace("/(.gif|.jpg|.jpeg|.png)/i","",$toFile); 
 
    switch($type) { 
        case 1: if(imagegif($newImg, "$toFile$newName.gif", $imgQuality)) 
        return "$newName.gif"; break; 
        case 2: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality)) 
        return "$newName.jpg"; break; 
        case 3: if(imagepng($newImg, "$toFile$newName.png", $imgQuality)) 
        return "$newName.png"; break; 
        default: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality))
        return "$newName.jpg"; break; 
    } 
    imagedestroy($newImg); 
    } 
    imagedestroy($img); 
    return false; 
}
其中 $toFile = preg_replace("/(.gif|.jpg|.jpeg|.png)/i","",$toFile);具体目的是什么?

因为tofile($srcImage,$toFile)第二个参数$toFile自己指定的话存在格式问题,比如srcImage是jpg,但是如果你填写的toFile是png,那格式就对不上了,因此如果你制定了toFile的文件后缀,那就直接干掉,然后在直接将原图片的格式追加在你指定的toFile后面。

--

简单来说:
tofile('a.jpg','b.png');
最终生成的是b.jpg而不是b.png

人气教程排行