Home php教程 php手册 [分享]一个强大的图片缩放类,支持填充缩放模式

[分享]一个强大的图片缩放类,支持填充缩放模式

Jun 06, 2016 pm 08:07 PM
share picture filling us screenshot support model Zoom

我们在截图的时候,如果给定的尺寸比例与原图比例不等,那么情况就是缩略出来的图片总有一边是不理想的, 可能高度不够,也可能宽度不够,最近项目用到图片缩放,发现了这个问题,于是重写了框架里的图片缩放方法,大家可能都知道电脑设置壁纸模式的时候有一

我们在截图的时候,如果给定的尺寸比例与原图比例不等,那么情况就是缩略出来的图片总有一边是不理想的, 可能高度不够,也可能宽度不够,最近项目用到图片缩放,发现了这个问题,于是重写了框架里的图片缩放方法,大家可能都知道电脑设置壁纸模式的时候有一项为“填充”,这种模式保证了你的屏幕不会出现空白,图片会自动调整到填满屏幕大小,那么下面这个方法加入了这一种模式,保证你需要的尺寸一定会有图,而不是出现黑边或者尺寸不理想等情况。

启用填充模式需要设置:$scaleMode=false;

以下方法只是框架图片类中的一部分:

???????? /**
???????? * 生成缩略图
???????? * @author Joychao
???????? * @static
???????? * @param string  $image?????????原图
???????? * @param string  $type??????????图像格式
???????? * @param string  $maxWidth????? 目标最大宽度
???????? * @param string  $maxHeight?????目标最大高度
???????? * @param string  $prefix????????目标后缀
???????? * @param boolean $scaleMode??   按比例切割模式
???????? * @param boolean $saveImg?????  生成后是否保存文件
???????? * @param boolean $interlace???? 启用隔行扫描
???????? * @return void 
???????? */
??????? static function thumbnail($image,$type='',$maxWidth=200,$maxHeight=50,$prefix='_thumb',$scaleMode=false,$saveImg=true,$interlace=true)
??????? {
??????????? // 获取原图信息
??????????? $info? = self::getInfo($image);
??????????? if($info !== false) {
??????????????? $srcWidth? = $info['width'];
??????????????? $srcHeight = $info['height'];
??????????????? $type = empty($type)?$info['type']:$type;
??????????????? $type = strtolower($type);
??????????????? $interlace? =? $interlace? 1:0;
??????????????? unset($info);
??????????????? // 载入原图
??????????????? $createFun = 'Imagecreatefrom'.($type=='jpg'?'jpeg':$type);
??????????????? $srcImg???? = $createFun($image);
??????????????? if($scaleMode){//按比例切图
??????????????????? $scale = min($maxWidth/$srcWidth, $maxHeight/$srcHeight); // 计算缩放比例
??????????????????? if($scale>=1) {
??????????????????????? // 超过原图大小不再缩略
??????????????????????? $width?? =? $srcWidth;
??????????????????????? $height? =? $srcHeight;
??????????????????? }else{
??????????????????????? // 缩略图尺寸
??????????????????????? $width? = (int)($srcWidth*$scale);
??????????????????????? $height = (int)($srcHeight*$scale);
??????????????????? }
??????????????? }else{//不按比例切图
??????????????????? $newScale=$maxWidth/$maxHeight;
??????????????????? $srcScale=$srcWidth/$srcHeight;
??????????????????? if($srcScale>=$newScale){
??????????????????????? $height=$maxHeight;
??????????????????????? $width=intval($height*($srcWidth/$srcHeight));
??????????????????? }else{
??????????????????????? $width=$maxWidth;
??????????????????????? $height=intval($width/($srcWidth/$srcHeight));
??????????????????? }
??????????????????? $thumbImgFirst = imagecreatetruecolor($width, $height);
??????????????????? imagecopyresampled($thumbImgFirst, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth,$srcHeight);
??????????????????? if($srcScale>=$newScale){
??????????????????????? //原图起点
??????????????????????? $startX=intval(($width-$maxWidth)*0.5);
??????????????????????? $startY=0;
??????????????????????? $width=$maxWidth;
??????????????????? }else{
??????????????????????? //原图起点
??????????????????????? $startX=0;
??????????????????????? $startY=intval(($height-$maxHeight)*0.5);
??????????????????????? $height=$maxHeight;
??????????????????? }
??????????????? }
??????????????? //创建缩略图
??????????????? if($type!='gif' && function_exists('imagecreatetruecolor')){
??????????????????? $thumbImg = imagecreatetruecolor($width, $height);
??????????????? }else{
??????????????????? $thumbImg = imagecreate($width, $height);
??????????????? }
??????????????? // 复制图片
??????????????? if(!$scaleMode){
??????????????????? imagecopymerge($thumbImg, $thumbImgFirst, 0, 0, $startX, $startY, $width, $height, 100);
??????????????? }else{
??????????????????? if(function_exists("Imagecopyresampled")){
??????????????????????? imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth,$srcHeight);
??????????????????? }else{
??????????????????????? imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height,? $srcWidth,$srcHeight);
?????????????????? }
??????????????? }
??????????????? if('gif'==$type || 'png'==$type) {
??????????????????? $background_color = imagecolorallocate($thumbImg,? 0,255,0);? //指派一个绿色
??????????????????? imagecolortransparent($thumbImg,$background_color);? //设置为透明色,若注释掉该行则输出绿色的图
??????????????? }
??????????????? // 对jpeg图形设置隔行扫描
??????????????? if('jpg'==$type || 'jpeg'==$type)
??????????????????? imageinterlace($thumbImg,$interlace);
??????????????? // 生成图片保存
??????????????? $imageFun = 'image'.($type=='jpg'?'jpeg':$type);
??????????????? $thumbName=preg_replace('/(\.\w+)$/', $prefix.'\\1', $image);??? //abs_thumb.jpg
??????????????? if($saveImg)//是否需要保存文件
??????????????????? $imageFun($thumbImg,$thumbName);
??????????????? else
??????????????????? $imageFun($thumbImg);
??????????????? imagedestroy($thumbImg);
??????????????? imagedestroy($srcImg);
??????????????? return $thumbName;
??????????? }
??????????? return false;
??????? }
????? /**
???????? * 获取图片信息
???????? * @static
???????? * @param string $path 图片路径
???????? * @return array
???????? */
??????? static function getInfo($path){
??????????? $arr=getimagesize($path);
??????????? $info['width']=$arr[0];
??????????? $info['height']=$arr[1];
??????????? $info['mime']=$arr['mime'];
??????????? return $info;
??????? }
Copy after login

如果在使用过程中遇到什么问题请留言或评论。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? Where is the automatically saved image when posting? How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? Where is the automatically saved image when posting? Mar 22, 2024 am 08:06 AM

How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? Where is the automatically saved image when posting?

How to post pictures in TikTok comments? Where is the entrance to the pictures in the comment area? How to post pictures in TikTok comments? Where is the entrance to the pictures in the comment area? Mar 21, 2024 pm 09:12 PM

How to post pictures in TikTok comments? Where is the entrance to the pictures in the comment area?

Safari zoom issue on iPhone: Here's the fix Safari zoom issue on iPhone: Here's the fix Apr 20, 2024 am 08:08 AM

Safari zoom issue on iPhone: Here's the fix

How to make ppt pictures appear one by one How to make ppt pictures appear one by one Mar 25, 2024 pm 04:00 PM

How to make ppt pictures appear one by one

What to do if a black screen appears when taking a screenshot on a win10 computer_How to deal with a black screen when taking a screenshot on a win10 computer What to do if a black screen appears when taking a screenshot on a win10 computer_How to deal with a black screen when taking a screenshot on a win10 computer Mar 27, 2024 pm 01:01 PM

What to do if a black screen appears when taking a screenshot on a win10 computer_How to deal with a black screen when taking a screenshot on a win10 computer

How to share NetEase Cloud Music to WeChat Moments_Tutorial on sharing NetEase Cloud Music to WeChat Moments How to share NetEase Cloud Music to WeChat Moments_Tutorial on sharing NetEase Cloud Music to WeChat Moments Mar 25, 2024 am 11:41 AM

How to share NetEase Cloud Music to WeChat Moments_Tutorial on sharing NetEase Cloud Music to WeChat Moments

iPhone screenshots not working: How to fix it iPhone screenshots not working: How to fix it May 03, 2024 pm 09:16 PM

iPhone screenshots not working: How to fix it

How to use shortcut keys to take screenshots in Win8? How to use shortcut keys to take screenshots in Win8? Mar 28, 2024 am 08:33 AM

How to use shortcut keys to take screenshots in Win8?

See all articles