首页 > 代码库 > 【PHP】图片操作类

【PHP】图片操作类

刚学php不久,在做一个项目一步步的积累和学习,希望分享的东西能其他人带来帮助,少走弯路。

<?php/** *    图片操作类 * @author Harlan Song *//** * 压缩图片,以宽度为基准,高度等比例压缩。 * @param string $srcPath 源图片路径 * @param string $newPath 压缩后图片路径 *  @param string $newWidth 最大宽度 */function commpressImage($srcPath,$newPath,$newWidth){    $info = getimagesize($srcPath);    $width = $info[0];    $height = $info[1];    $mime = $info[‘mime‘];//得到图片类型    if($width > $newWidth){        $newHeight = $height / ($width / $newWidth);        $newImg = imagecreatetruecolor($newWidth,$newHeight);        switch($mime){            case "image/jpeg":                $img = @imagecreatefromjpeg($srcPath);                imagecopyresampled($newImg,$img,0,0,0,0,$newWidth,$newHeight,$width,$height);                imagejpeg($newImg,$newPath);                break;            case "image/png":                $img = @imagecreatefrompng($srcPath);                imagecopyresampled($newImg,$img,0,0,0,0,$width,$height,$newWidth,$newHeight);                imagepng($newImg,$newPath);                break;            case "image/gif":                $img = @imagecreatefromgif($srcPath);                imagecopyresampled($newImg,$img,0,0,0,0,$width,$height,$newWidth,$newHeight);                imagegif($newImg,$newPath);                break;            case "image/wbmp":                $img = @imagecreatefromwbmp($srcPath);                imagecopyresampled($newImg,$img,0,0,0,0,$width,$height,$newWidth,$newHeight);                imagewbmp($newImg,$newPath);                break;        }    }else{        //源图片宽度比指定值小,不压缩直接复制。        copy($srcPath,$newPath);    }}?>