ThinkPHP5.0 圖片上傳生成縮略圖實(shí)例代碼說明
很多朋友遇到這樣一個問題,圖片上傳生成縮略圖,很多人在本機(jī)(win)測試成功,上傳到linux 服務(wù)器后錯誤。
我也遇到同樣的問題。網(wǎng)上一查,有無數(shù)的人說是服務(wù)器臨時文件目錄權(quán)限問題。
幾經(jīng)思考后,發(fā)現(xiàn)并非如此。
其根本的原因是,保存到變量的信息是之前的,之后又move移動到了自己指定的目錄下,同時臨時文件已經(jīng)不存在。所以再生成縮略圖的時候,需要open的,文件地址應(yīng)該是自己定義的目錄+文件名。然而很多實(shí)例文檔中,還是使用的move 之前的信息。
又加之在win服務(wù)器下,move后,指定目錄已生成了文件,同時臨時文件未被刪除。所以能用move之前的信息生成縮略圖。
希望不多的言語能幫助遇到同樣問題的你。
下面在通過實(shí)例代碼給大家介紹ThinkPHP5.0 圖片上傳生成縮略圖的方法。
代碼如下所示:
<?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;
}
}
總結(jié)
以上所述是小編給大家介紹的ThinkPHP5.0 圖片上傳生成縮略圖實(shí)例代碼說明,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
laravel5.1 ajax post 傳值_token示例
今天小編就為大家分享一篇laravel5.1 ajax post 傳值_token示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
thinkPHP3.2使用RBAC實(shí)現(xiàn)權(quán)限管理的實(shí)現(xiàn)
這篇文章主要介紹了thinkPHP3.2使用RBAC實(shí)現(xiàn)權(quán)限管理的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
ZendFramework2連接數(shù)據(jù)庫操作實(shí)例
這篇文章主要介紹了ZendFramework2連接數(shù)據(jù)庫操作,結(jié)合完整實(shí)例形式分析了ZendFramework2連接數(shù)據(jù)庫的具體步驟、配置方法、相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2017-04-04
PHP實(shí)現(xiàn)爬蟲爬取圖片代碼實(shí)例
這篇文章主要介紹了PHP實(shí)現(xiàn)爬蟲爬取圖片代碼實(shí)例,有實(shí)際的代碼例子,感興趣的同學(xué)可以嘗試下2021-03-03

