虎泰克博客

ThinkPHP实现修改URL地址得到任意尺寸的缩略图(小于原图最小宽高)


今天在外面的时候小伙伴给我提了个要求就是图片可以像http://static2.8dol.com/c

今天在外面的时候小伙伴给我提了个要求就是图片可以像http://static2.8dol.com/cloudstore/goods/20151028_31033.jpg@!img-100这样修改URL就能看到不同尺寸的缩略图,回家路上仔细思考了下,思路是图片请求指向PHP->图片地址用I函数提取出来->像素大小也用I函数来提取出来->裁剪原图得到正方形图片->得到对应尺寸的缩略图->存储缩略图->输出->浏览器。 用的是Thinkphp的Image库

虎ybspi泰ez克u新wehi闻eeewy网srpxr版权所有,请勿未授权转载!!!

public function uploads(){ 

虎kg泰iedl克dverf新ark闻tm网t版权所有,请勿未授权转载!!!

 header("Content-type: image/jpeg");//设置HTTP头 

虎jh泰cea克t_q新pov闻ul网wur版权所有,请勿未授权转载!!!

 $pix=I('path.4');//得到像素大小 

虎u泰s克qppi新umg闻_网g版权所有,请勿未授权转载!!!

 $url = './upload/'.I('path.1').'/'.I('path.2').'/'.I('path.3');//原图片地址 

虎w泰sicav克qdlvl新kucgr闻bla网hp版权所有,请勿未授权转载!!!

 $image = new \Think\Image();//引入库文件 

虎is泰n克k新f闻o网hbebh版权所有,请勿未授权转载!!!

 $image->open($url);//打开原图片 

虎r泰yehw克aq新w闻zwim网k版权所有,请勿未授权转载!!!

 $type = $image->type();//图片类型 

虎z泰yzq_z克d新zs闻f网htu版权所有,请勿未授权转载!!!

 $width = $image->width();//图片宽度 

虎e泰oha克diz新uci闻f网yxkb版权所有,请勿未授权转载!!!

 $height = $image->height();//图片高度 

虎biqrz泰ou克phocl新bm闻eac网glk版权所有,请勿未授权转载!!!

 $thumburl='./upload/thumb/'.md5(time().rand()).'.'.$type;//缩略图存储路径 

虎bzssn泰_jwmj克ofe新li闻irtj网elizi版权所有,请勿未授权转载!!!

if($width>=$height) $image->thumb($height,$height,\Think\Image::IMAGE_THUMB_CENTER)->thumb($pix,$pix)->save($thumburl);//比较宽高哪个大,宽更大的话按高度*高度像素裁剪正方形之后生成$pix*$pix像素大小缩略图 

虎t泰zgrzh克ttdy新i闻tnt网tse版权所有,请勿未授权转载!!!

else $image->thumb($width,$width,\Think\Image::IMAGE_THUMB_CENTER)->thumb($pix,$pix)->save($thumburl);//高更大的话按宽度*宽度像素裁剪正方形之后生成$pix*$pix像素大小缩略图 

虎b泰zcczj克_ol新crtq闻vv网kiwy版权所有,请勿未授权转载!!!

 echo file_get_contents($thumburl);//在浏览器中输出缩略图 

虎xag泰tepau克lszxw新ggd闻oohsr网ywyuz版权所有,请勿未授权转载!!!

 exit(); 

虎rpt泰hrxtc克nek新cevt闻x网pbet版权所有,请勿未授权转载!!!

}

虎h泰t克dy新evo闻s网fxly版权所有,请勿未授权转载!!!

效果如下:http://taji.whutech.com/DongTai/uploads/2016-02/16/pic.jpg/186



Verklaring: alle artikelen /tekeningen die op deze website worden weergegeven, zijn bedoeld om meer informatie te verzenden en de discussie te vergemakkelijken, wat niet betekent dat de website, de host en de organisator het eens zijn met hun meningen of de authenticiteit van hun inhoud bevestigen. De inhoud van de artikelen is alleen voor referentie. Indien de rechten van de oorspronkelijke auteur worden geschonden, neem dan contact op met de beheerder van de website.

Schrijf in om commentaar te geven

Lijst van opmerkingen

    Geen gegevens