您好,登錄后才能下訂單哦!
ThinkPHP5.0 怎么實現(xiàn)圖片上傳生成縮略圖?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
代碼如下所示:
<?php namespace app\common\controller; use app\common\model\Goods; class Tools { public static function upload_goods_img($whereName="", $width="", $height="") { // 打開圖片的相對路徑 $imgpath = config('img_path'); // 絕對路徑 $imgRootPath = config('imgRootPath'); $storeId = '自定義'; $merchantId = '自定義'; $old_filename = $storeId . $merchantId . time(); $filename = $storeId . $merchantId . time() . mt_rand(1000, 9999); $type = Goods::upload($whereName, $old_filename); if($type) { $savepath = $imgRootPath . '/' . $whereName . '/' . $filename . '.' . $type; $thumbfile = $filename . '.' . $type; $thumbName = $imgpath . '/' . $whereName . '/' . $thumbfile; $image = \think\Image::open($imgpath . '/'. $whereName .'/' . $old_filename . '.' . $type); $image->thumb($width, $height, \think\Image::THUMB_FIXED)->save($thumbName); $data = [ 'access_url' => $imgRootPath . '/' . $whereName . '/' . $filename . '.' . $type, 'filename' => $thumbfile, ]; return $data; } } }
調(diào)用:
class Goods { public function upload_sku() { $whereName = 'goods/sku'; $width = 750; $height = 750; $data = Tools::upload_goods_img($whereName,$width, $height); return returnJson(1, '上傳成功', $data);; } }
PS:下面在看一段代碼tp5中上傳圖片方法,并生成相應(yīng)縮略圖的方法
//接收上傳文件的name $file = $this->_req->file("upload_head_image"); //將上傳的文件移動到public/uploads/user $info = $file->validate(['size'=>5242880,'ext'=>'jpg,jpeg,png'])->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'user'); if($info){ $pic = new \app\home\model\User(); $pic_url = $pic->thumbImage($file,$info); $user['portrait'] = 'uploads/user/'.$pic_url; //print_r($pic_url);exit(); } ///model中代碼如下 /** * [生成用戶頭像縮略圖,180、50] * @param [type] $file [獲取上傳文件$_FILE] * @param [type] $pic [上傳文件的路徑] * @return [type] [返回處理后的文件路徑] */ public function thumbImage($file,$pic){ $image = \think\Image::open($file); $getSaveName = str_replace('\\','/',$pic->getSaveName()); $portrait_thumbnail_180= 'uploads/user/'.str_replace($pic->getFilename(),'180_'.$pic->getFilename(),$getSaveName); $image->thumb(180,180,\think\Image::THUMB_CENTER)->save(ROOT_PATH . 'public' . DS . $portrait_thumbnail_180,null,100,true); $portrait_thumbnail_80 = 'uploads/user/'.str_replace($pic->getFilename(),'80_'.$pic->getFilename(),$getSaveName); $image->thumb(80,80,\think\Image::THUMB_CENTER)->save(ROOT_PATH . 'public' . DS . $portrait_thumbnail_80,null,100,true); $portrait_thumbnail_50 = 'uploads/user/'.str_replace($pic->getFilename(),'50_'.$pic->getFilename(),$getSaveName); $image->thumb(50,50,\think\Image::THUMB_CENTER)->save(ROOT_PATH . 'public' . DS . $portrait_thumbnail_50,null,100,true); if ($image) { return $getSaveName; } }
看完上述內(nèi)容,你們掌握ThinkPHP5.0 怎么實現(xiàn)圖片上傳生成縮略圖的方法了嗎?如果還想學到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。