您现在的位置是:网站首页> 编程资料编程资料
PHP实现将上传图片自动缩放到指定分辨率,并保持清晰度封装类示例_php技巧_
2023-05-25
430人已围观
简介 PHP实现将上传图片自动缩放到指定分辨率,并保持清晰度封装类示例_php技巧_
本文实例讲述了PHP实现将上传图片自动缩放到指定分辨率,并保持清晰度封装类。分享给大家供大家参考,具体如下:
class AutoImage{ private $image; public function resize($src, $width, $height){ //$src 就是 $_FILES['upload_image_file']['tmp_name'] //$width和$height是指定的分辨率 //如果想按指定比例放缩,可以将$width和$height改为$src的指定比例 $this->image = $src; $info = getimagesize($src);//获取图片的真实宽、高、类型 if($info[0] == $width && $info[1] == $height){ //如果分辨率一样,直接返回原图 return $src; } switch ($info['mime']){ case 'image/jpeg': header('Content-Type:image/jpeg'); $image_wp = imagecreatetruecolor($width, $height); $image_src = imagecreatefromjpeg($src); imagecopyresampled($image_wp, $image_src, 0, 0, 0, 0, $width, $height, $info[0], $info[1]); imagedestroy($image_src); imagejpeg($image_wp,$this->image); break; case 'image/png': header('Content-Type:image/png'); $image_wp = imagecreatetruecolor($width, $height); $image_src = imagecreatefrompng($src); imagecopyresampled($image_wp, $image_src, 0, 0, 0, 0, $width, $height, $info[0], $info[1]); imagedestroy($image_src); imagejpeg($image_wp,$this->image); break; case 'image/gif': header('Content-Type:image/gif'); $image_wp = imagecreatetruecolor($width, $height); $image_src = imagecreatefromgif($src); imagecopyresampled($image_wp, $image_src, 0, 0, 0, 0, $width, $height, $info[0], $info[1]); imagedestroy($image_src); imagejpeg($image_wp,$this->image); break; } return $this->image; } } 更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP图形与图片操作技巧汇总》、《PHP数组(Array)操作技巧大全》、《PHP数据结构与算法教程》、《php程序设计算法总结》、《PHP数学运算技巧总结》、《php字符串(string)用法总结》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
您可能感兴趣的文章:
相关内容
- PHP 并发场景的几种解决方案_php实例_
- PHP 实现文件压缩解压操作的方法_php实例_
- php反射学习之依赖注入示例_php技巧_
- php反射学习之不用new方法实例化类操作示例_php技巧_
- PHP反射学习入门示例_php技巧_
- PHP如何实现阿里云短信sdk灵活应用在项目中的方法_php实例_
- PHP中常用的三种设计模式详解【单例模式、工厂模式、观察者模式】_php技巧_
- PHP面向对象程序设计子类扩展父类(子类重新载入父类)操作详解_php技巧_
- PHP面向对象程序设计模拟一般面向对象语言中的方法重载(overload)示例_php技巧_
- PHP面向对象程序设计重载(overloading)操作详解_php技巧_
