ThinkPHP实现修改URL地址得到任意尺寸的缩略图(小于原图最小宽高)
随风飘扬 学习 2016年02月16日13:34 5260
今天在外面的时候小伙伴给我提了个要求就是图片可以像http://static2.8dol.com/c
今天在外面的时候小伙伴给我提了个要求就是图片可以像http://static2.8dol.com/cloudstore/goods/20151028_31033.jpg@!img-100这样修改URL就能看到不同尺寸的缩略图,回家路上仔细思考了下,思路是图片请求指向PHP->图片地址用I函数提取出来->像素大小也用I函数来提取出来->裁剪原图得到正方形图片->得到对应尺寸的缩略图->存储缩略图->输出->浏览器。 用的是Thinkphp的Image库
虎sgrq泰lw_iw克icnlg新_ruil闻eq网pqspr版权所有,请勿未授权转载!!!
public function uploads(){
虎hbu泰wh_jj克yner新z闻bkrg网qw版权所有,请勿未授权转载!!!
header("Content-type: image/jpeg");//设置HTTP头
虎jqtp泰g克evuuk新naipk闻oct_l网hiok版权所有,请勿未授权转载!!!
$pix=I('path.4');//得到像素大小
虎g泰kva克ngvlt新s闻gkwcq网ynwk版权所有,请勿未授权转载!!!
$url = './upload/'.I('path.1').'/'.I('path.2').'/'.I('path.3');//原图片地址
虎nb泰t克zxtz新ac_bk闻cy网jj版权所有,请勿未授权转载!!!
$image = new \Think\Image();//引入库文件
虎ijd泰ff克t新rq闻hoarz网xgys版权所有,请勿未授权转载!!!
$image->open($url);//打开原图片
虎lndy泰m克vqd新mgjr闻qxx网ga版权所有,请勿未授权转载!!!
$type = $image->type();//图片类型
虎se泰xfx克ob新nvw闻qrn网r版权所有,请勿未授权转载!!!
$width = $image->width();//图片宽度
虎edd泰d克r新ghfn闻vt网zpe版权所有,请勿未授权转载!!!
$height = $image->height();//图片高度
虎tzsha泰euovp克jiofl新yrk闻dcp网xxzbx版权所有,请勿未授权转载!!!
$thumburl='./upload/thumb/'.md5(time().rand()).'.'.$type;//缩略图存储路径
虎eiea泰hfh克oezqi新mfcop闻pkl网ay版权所有,请勿未授权转载!!!
if($width>=$height) $image->thumb($height,$height,\Think\Image::IMAGE_THUMB_CENTER)->thumb($pix,$pix)->save($thumburl);//比较宽高哪个大,宽更大的话按高度*高度像素裁剪正方形之后生成$pix*$pix像素大小缩略图
虎lh_w泰vdmiq克avy新yro闻ewkkp网pijx_版权所有,请勿未授权转载!!!
else $image->thumb($width,$width,\Think\Image::IMAGE_THUMB_CENTER)->thumb($pix,$pix)->save($thumburl);//高更大的话按宽度*宽度像素裁剪正方形之后生成$pix*$pix像素大小缩略图
虎vpf泰xhb克tva新lmivv闻g网dgfoz版权所有,请勿未授权转载!!!
echo file_get_contents($thumburl);//在浏览器中输出缩略图
虎unjpi泰oat克esar新rj_jh闻uj网d_co版权所有,请勿未授权转载!!!
exit();
虎wcma泰rfjhu克uxa新fge_闻nnw_f网yay版权所有,请勿未授权转载!!!
}
虎jbw泰z克jmvh新_闻f网p版权所有,请勿未授权转载!!!
效果如下:http://taji.whutech.com/DongTai/uploads/2016-02/16/pic.jpg/186
Statement: all articles / drawings reproduced on this website are for the purpose of transmitting more information and facilitating discussion, which does not mean that the website, the host and the organizer agree with their opinions or confirm the authenticity of their contents. The contents of the articles are for reference only. If the rights of the original author are violated, please contact the website administrator.
Comment list
-
No data