您的位置:首页 > 编程语言 > PHP开发

tp-上传图片,自带截取图片

2015-12-22 15:51 671 查看
是tp自带的,然后包括截取图片,开发手册都有。这里是生成一张原图、一张截图。只是我返回的是原图。截图后有后缀名。

/*图片上传*/
public function upload2(){
$file = $_FILES['file'];
$upload = new \Think\Upload();// 实例化上传类
$upload->maxSize = 2*1024*1024;
$upload->rootPath  = './Uploads/'; // 设置附件上传根目录
$upload->savePath  = 'Home/lpq/'; // 设置附件上传(子)目录
$info = $upload->uploadOne($file);
/* --- 从这里开始做截图图片 start ---*/
$infourl= './Uploads/'.$info['savepath'].$info['savename'];
$image = new \Think\Image();
$image->open($infourl);//将图片裁剪为400x400并保存为corp.jpg
$width = $image->width(); // 返回图片的宽度
$height = $image->height(); // 返回图片的高度
$newUrl = str_replace('.'.$info['ext'],'',$infourl).'_t.'.$info['ext'];
$iw = 320;
$ih = 345;
$image->thumb($iw, $ih,\Think\Image::IMAGE_THUMB_CENTER)->save($newUrl);
/* --- 从这里开始做截图图片 end ---*/
exit(json_encode($info));
}


下面返回的是已经截好的图。

/*图片上传*/
public function upload2(){
$file = $_FILES['file'];
$upload = new \Think\Upload();// 实例化上传类
$upload->maxSize = 2*1024*1024;
$upload->rootPath  = './Uploads/'; // 设置附件上传根目录
$upload->savePath  = 'Home/lpq/'; // 设置附件上传(子)目录
$info = $upload->uploadOne($file);
/*--- 这里返回的是已经截好的图了。start --- */
$infourl='./Uploads/'.$info['savepath'].$info['savename'];
$image = new \Think\Image();
$image->open($infourl);//将图片裁剪为400x400并保存为corp.jpg
$width = $image->width(); // 返回图片的宽度
$height = $image->height(); // 返回图片的高度
$iw = $ih = 300;
if($iw>$width){
$iw = $width;
}
if($ih>$height){
$ih = $height;
}
if($width>300 || $height>300){
$image->thumb($iw, $ih,\Think\Image::IMAGE_THUMB_CENTER)->save($infourl);
}
exit(json_encode($info));
/*--- 这里返回的是已经截好的图了。end--- */
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: