PHP compression crop a picture example

/ * * 
 * @Param $ srcImage original image path 
 * @param $ toFile after clipping path picture 
 * @param int $ maxWidth maximum cutting width 
 * @param int $ maxHeight after cutting the maximum height 
 * @param int $ imgQuality picture quality 
 @return BOOL * | String | void 
 * / 
function a resize ( $ srcImage , $ of toFile , $ maxWidth = 640, $ the maxHeight = 480, $ 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;
}

From the micro-channel public number: Programming Society

Advanced programmers daily book, please pay attention!

Guess you like

Origin www.cnblogs.com/ai10999/p/11449444.html