首页 > 开发 > PHP > 正文

PHP图片裁剪函数(保持图像不变形)

2024-05-04 22:21:22
字体:
来源:转载
供稿:网友
为了完成图片上传之后自动的裁剪,然后在前台显示出裁剪出的图片。
需求如上,源码如下:
代码如下:
<?
 *exif_imagetype -- 判断一个图像的类型
 *说明:函数功能是把一个图像裁剪为任意大小的图像,图像不变形
 * 参数说明:输入 需要处理图片的 文件名,生成新图片的保存文件名,生成新图片的宽,生成新图片的高
 */
 // 获得任意大小图像,不足地方拉伸,不产生变形,不留下空白
         function my_image_resize($src_file, $dst_file , $new_width , $new_height) {
        $new_width= intval($new_width);
        $new_height=intval($new_width);
         if($new_width <1 || $new_height <1) {
         echo "params width or height error !";
         exit();
         }
         if(!file_exists($src_file)) {
         echo $src_file . " is not exists !";
         exit();
         }
         // 图像类型
         $type=exif_imagetype($src_file);
         $support_type=array(IMAGETYPE_JPEG , IMAGETYPE_PNG , IMAGETYPE_GIF);
         if(!in_array($type, $support_type,true)) {
         echo "this type of image does not support! only support jpg , gif or png";
         exit();
         }
         //Load image
         switch($type) {
         case IMAGETYPE_JPEG :
         $src_img=imagecreatefromjpeg($src_file);
         break;
         case IMAGETYPE_PNG :
         $src_img=imagecreatefrompng($src_file);
         break;
         case IMAGETYPE_GIF :
         $src_img=imagecreatefromgif($src_file);
         break;
         default:
         echo "Load image error!";
         exit();
         }
         $w=imagesx($src_img);
         $h=imagesy($src_img);
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表