當前位置:首頁 » 文件管理 » thinkphp上傳圖片預覽

thinkphp上傳圖片預覽

發布時間: 2022-02-28 04:07:31

① Thinkphp 生成多張不同尺寸的縮略圖

其實很簡單,那個縮略圖寬度和高度可以定義多個,然後用逗號分割就好了。

//公共上傳
privatefunction_upload($width,$height,$path,$prefix){
import('ORG.Net.UploadFile');
$upload=newUploadFile();//實例化上傳類
$upload->maxSize=C('UPLOAD_SIZE');//設置附件上傳大小
$upload->savePath='./Uploads/'.$path;//設置附件上傳目錄
$upload->allowExts=array('jpg','gif','png','jpeg');//設置附件上傳類型
$upload->saveRule='time';
$upload->uploadReplace=true;//是否存在同名文件是否覆蓋
$upload->thumb=true;//是否對上傳文件進行縮略圖處理
$upload->thumbMaxWidth=$width;//縮略圖處理寬度
$upload->thumbMaxHeight=$height;//縮略圖處理高度
$upload->thumbPrefix=$prefix;//縮略圖前綴
$upload->thumbPath='./Uploads/'.$path.date('Ymd',time()).'/';//縮略圖保存路徑
$upload->thumbRemoveOrigin=true;//上傳圖片後刪除原圖片
$upload->autoSub=true;//是否使用子目錄保存圖片
$upload->subType='date';//子目錄保存規則
$upload->dateFormat='Ymd';//子目錄保存規則為date時時間格式


if(!$upload->upload()){//上傳錯誤提示錯誤信息
echojson_encode(array('msg'=>$this->error($upload->getErrorMsg()),'status'=>0));
}else{//上傳成功獲取上傳文件信息
$info=$upload->getUploadFileInfo();
$picname=$info[0]['savename'];

$picname=explode('/',$picname);
$picname=$picname[0].'/'.$prefix.$picname[1];
echojson_encode(array('status'=>1,'msg'=>$picname));

}
}

然後你比如生成300*300的的圖片你就定義一個方法

例如://商品縮略圖上傳
public function uploadThumb() {
return $this->_upload('230,160', '230,121', 'thumb/', 'shop_,thumb_');
}


然後400*400的就又寫一個方法:

//商品縮略圖上傳
public function uploadThumb() {
return $this->_upload('400,200', '400,200', 'thumb/', 'shop_,thumb_');
}


然後調用的不同就好了

② thinkphp中webuploadery圖片上傳問題

如提交到Upload控制器的upload方法
設置
server:"U('Upload/upload')"


server:"/index.php/Upload/upload"
php上傳處理代碼:

$defaultConfigs = array(
'maxSize' => 1024*1024,
'exts' => array('jpg', 'gif', 'png', 'jpeg'),
'rootPath' => '',
'saveName' => date('YmdHis') . rand(1234, 9999) . rand(1234, 9999)
);
$upload = new \Think\Upload($defaultConfigs);
$info = $upload->upload(array($_FILES['FILE欄位名']));
mp($info);

③ ThinkPHP3.2.1 怎麼在上傳時自動生成縮略圖

$image = new ThinkImage();

$image->open('./1.jpg');

// 按照原圖的比例生成一個最大為150*150的縮略圖並保存為thumb.jpg

$image->thumb(150, 150)->save('./thumb.jpg');

http://document.thinkphp.cn/manual_3_2.html#image

④ thinkPHP做上傳,顯示不存在的圖像文件,是什麼原因

請檢測下:客戶端form表單中是否包含了'photo' 這個表單變數:

var_mp($_FILES);#檢測是否有值傳遞過來

⑤ thinkphp5 怎麼接受zyupload,上傳的圖片

引入這個類就可以

<?php
//視圖表單
//支持多張圖片上傳
classupload{
var$dir;//附件存放物理目錄
var$time;//自定義文件上傳時間
var$allow_types;//允許上傳附件類型
var$field;//上傳控制項名稱
var$maxsize;//最大允許文件大小,單位為KB
var$thumb_width;//縮略圖寬度
var$thumb_height;//縮略圖高度
var$watermark_file;//水印圖片地址
var$watermark_pos;//水印位置
var$watermark_trans;//水印透明度
//構造函數
//$types:允許上傳的文件類型,$maxsize:允許大小,$field:上傳控制項名稱,$time:自定義上傳時間
functionupload($types='jpg|png',$maxsize=1024,$field='attach',$time=''){
$this->allow_types=explode('|',$types);
$this->maxsize=$maxsize*1024;
$this->field=$field;
$this->time=$time?$time:time();
}
//設置並創建文件具體存放的目錄
//$basedir:基目錄,必須為物理路徑
//$filedir:自定義子目錄,可用參數{y}、{m}、{d}
functionset_dir($basedir,$filedir=''){
$dir=$basedir;
!is_dir($dir)&&@mkdir($dir,0777);
if(!empty($filedir)){
$filedir=str_replace(array('{y}','{m}','{d}'),array(date('Y',$this->time),date('m',$this->time),date('d',$this->time)),strtolower($filedir));//用string_replace把{y}{m}{d}幾個標簽進行替換
$dirs=explode('/',$filedir);
foreach($dirsas$d){
!empty($d)&&$dir.=$d.'/';
!is_dir($dir)&&@mkdir($dir,0777);
}
}
$this->dir=$dir;
}
//圖片縮略圖設置,如果不生成縮略圖則不用設置
//$width:縮略圖寬度,$height:縮略圖高度
functionset_thumb($width=0,$height=0){
$this->thumb_width=$width;
$this->thumb_height=$height;
}
//圖片水印設置,如果不生成添加水印則不用設置
//$file:水印圖片,$pos:水印位置,$trans:水印透明度
functionset_watermark($file,$pos=6,$trans=80){
$this->watermark_file=$file;
$this->watermark_pos=$pos;
$this->watermark_trans=$trans;
}
/*—————————————————————-
執行文件上傳,處理完返回一個包含上傳成功或失敗的文件信息數組,
其中:name為文件名,上傳成功時是上傳到伺服器上的文件名,上傳失敗則是本地的文件名
dir為伺服器上存放該附件的物理路徑,上傳失敗不存在該值
size為附件大小,上傳失敗不存在該值
flag為狀態標識,1表示成功,-1表示文件類型不允許,-2表示文件大小超出
—————————————————————–*/
functionexecute(){
$files=array();//成功上傳的文件信息
$field=$this->field;
$keys=array_keys($_FILES[$field]['name']);
foreach($keysas$key){
if(!$_FILES[$field]['name'][$key])continue;
$fileext=$this->fileext($_FILES[$field]['name'][$key]);//獲取文件擴展名
$filename=date('Ymdhis',$this->time).mt_rand(10,99).'.'.$fileext;//生成文件名
$filedir=$this->dir;//附件實際存放目錄
$filesize=$_FILES[$field]['size'][$key];//文件大小
//文件類型不允許
if(!in_array($fileext,$this->allow_types)){
$files[$key]['name']=$_FILES[$field]['name'][$key];
$files[$key]['flag']=-1;
continue;
}
//文件大小超出
if($filesize>$this->maxsize){
$files[$key]['name']=$_FILES[$field]['name'][$key];
$files[$key]['name']=$filesize;
$files[$key]['flag']=-2;
continue;
}
$files[$key]['name']=$filename;
$files[$key]['dir']=$filedir;
$files[$key]['size']=$filesize;
//保存上傳文件並刪除臨時文件
if(is_uploaded_file($_FILES[$field]['tmp_name'][$key])){
move_uploaded_file($_FILES[$field]['tmp_name'][$key],$filedir.$filename);
@unlink($_FILES[$field]['tmp_name'][$key]);
$files[$key]['flag']=1;
//對圖片進行加水印和生成縮略圖,這里演示只支持jpg和png(gif生成的話會沒了幀的)
if(in_array($fileext,array('jpg','png'))){
if($this->thumb_width){
if($this->create_thumb($filedir.$filename,$filedir.'thumb_'.$filename)){
$files[$key]['thumb']='thumb_'.$filename;//縮略圖文件名
}
}
$this->create_watermark($filedir.$filename);
}
}
}
return$files;
}
//創建縮略圖,以相同的擴展名生成縮略圖
//$src_file:來源圖像路徑,$thumb_file:縮略圖路徑
functioncreate_thumb($src_file,$thumb_file){
$t_width=$this->thumb_width;
$t_height=$this->thumb_height;
if(!file_exists($src_file))returnfalse;
$src_info=getImageSize($src_file);
//如果來源圖像小於或等於縮略圖則拷貝源圖像作為縮略圖,免去操作
if($src_info[0]<=$t_width&&$src_info[1]<=$t_height){
if(!($src_file,$thumb_file)){
returnfalse;
}
returntrue;
}
//按比例計算縮略圖大小
if(($src_info[0]-$t_width)>($src_info[1]-$t_height)){
$t_height=($t_width/$src_info[0])*$src_info[1];
}else{
$t_width=($t_height/$src_info[1])*$src_info[0];
}
//取得文件擴展名
$fileext=$this->fileext($src_file);
switch($fileext){
case'jpg':
$src_img=ImageCreateFromJPEG($src_file);break;
case'png':
$src_img=ImageCreateFromPNG($src_file);break;
case'gif':
$src_img=ImageCreateFromGIF($src_file);break;
}
//創建一個真彩色的縮略圖像
$thumb_img=@ImageCreateTrueColor($t_width,$t_height);
//ImageCopyResampled函數拷貝的圖像平滑度較好,優先考慮
if(function_exists('imageresampled')){
@ImageCopyResampled($thumb_img,$src_img,0,0,0,0,$t_width,$t_height,$src_info[0],$src_info[1]);
}else{
@ImageCopyResized($thumb_img,$src_img,0,0,0,0,$t_width,$t_height,$src_info[0],$src_info[1]);
}
//生成縮略圖
switch($fileext){
case'jpg':
ImageJPEG($thumb_img,$thumb_file);break;
case'gif':
ImageGIF($thumb_img,$thumb_file);break;
case'png':
ImagePNG($thumb_img,$thumb_file);break;
}
//銷毀臨時圖像
@ImageDestroy($src_img);
@ImageDestroy($thumb_img);
returntrue;
}
//為圖片添加水印
//$file:要添加水印的文件
functioncreate_watermark($file){
//文件不存在則返回
if(!file_exists($this->watermark_file)||!file_exists($file))return;
if(!function_exists('getImageSize'))return;
//檢查GD支持的文件類型
$gd_allow_types=array();
if(function_exists('ImageCreateFromGIF'))$gd_allow_types['image/gif']='ImageCreateFromGIF';
if(function_exists('ImageCreateFromPNG'))$gd_allow_types['image/png']='ImageCreateFromPNG';
if(function_exists('ImageCreateFromJPEG'))$gd_allow_types['image/jpeg']='ImageCreateFromJPEG';
//獲取文件信息
$fileinfo=getImageSize($file);
$wminfo=getImageSize($this->watermark_file);
if($fileinfo[0]<$wminfo[0]||$fileinfo[1]<$wminfo[1])return;
if(array_key_exists($fileinfo['mime'],$gd_allow_types)){
if(array_key_exists($wminfo['mime'],$gd_allow_types)){
//從文件創建圖像
$temp=$gd_allow_types[$fileinfo['mime']]($file);
$temp_wm=$gd_allow_types[$wminfo['mime']]($this->watermark_file);
//水印位置
switch($this->watermark_pos){
case1://頂部居左
$dst_x=0;$dst_y=0;break;
case2://頂部居中
$dst_x=($fileinfo[0]-$wminfo[0])/2;$dst_y=0;break;
case3://頂部居右
$dst_x=$fileinfo[0];$dst_y=0;break;
case4://底部居左
$dst_x=0;$dst_y=$fileinfo[1];break;
case5://底部居中
$dst_x=($fileinfo[0]-$wminfo[0])/2;$dst_y=$fileinfo[1];break;
case6://底部居右
$dst_x=$fileinfo[0]-$wminfo[0];$dst_y=$fileinfo[1]-$wminfo[1];break;
default://隨機
$dst_x=mt_rand(0,$fileinfo[0]-$wminfo[0]);$dst_y=mt_rand(0,$fileinfo[1]-$wminfo[1]);
}
if(function_exists('ImageAlphaBlending'))ImageAlphaBlending($temp_wm,True);//設定圖像的混色模式
if(function_exists('ImageSaveAlpha'))ImageSaveAlpha($temp_wm,True);//保存完整的alpha通道信息
//為圖像添加水印
if(function_exists('imageCopyMerge')){
ImageCopyMerge($temp,$temp_wm,$dst_x,$dst_y,0,0,$wminfo[0],$wminfo[1],$this->watermark_trans);
}else{
ImageCopyMerge($temp,$temp_wm,$dst_x,$dst_y,0,0,$wminfo[0],$wminfo[1]);
}
//保存圖片
switch($fileinfo['mime']){
case'image/jpeg':
@imageJPEG($temp,$file);
break;
case'image/png':
@imagePNG($temp,$file);
break;
case'image/gif':
@imageGIF($temp,$file);
break;
}
//銷毀零時圖像
@imageDestroy($temp);
@imageDestroy($temp_wm);
}
}
}
//獲取文件擴展名
functionfileext($filename){
returnstrtolower(substr(strrchr($filename,'.'),1,10));
}
}
?>

⑥ thinkphp關於上傳圖片到資料庫裡面大小的問題

上傳圖片的時候,進行圖片縮放:

http://document.thinkphp.cn/manual_3_2.html#image
裡面寫的很詳細,很全

⑦ 使用thinkphp做一個上傳圖片,並且要將圖片直接保存到資料庫里。感覺現在好沒有頭緒,不知道如何下手。

圖片直接保存到資料庫里?圖片應該是保存在文件夾裡面的吧,然後把圖片在文件夾的路徑保存在資料庫里,這樣才對吧?

⑧ thinkphp的框架 怎麼能實現圖片上傳功能和寫入資料庫,form 這樣定義之後 post獲取不到img_url值

思路是可以的,那提交數據後image可以獲取嗎?如果可以,那肯定是填寫鏈接的input有問題,而且你下面的圖有兩個圖片和鏈接,說明你是點了"點擊添加多個圖片"那個操作,那個操作之後新建的html一樣?如果一樣就會有兩個'img_url'和'image'input標簽,你應該用'img_url[]' 和 'image[]',大概想到這些

⑨ thinkphp作為伺服器端獲取上傳的圖片並保存的介面怎麼寫

就這么寫:
上傳操作
ThinkPHP文件上傳操作使用Think\Upload類,假設前面的表單提交到當前控制器的upload方法,我們來看下upload方法的實現代碼:
public function upload(){ $upload = new \Think\Upload();// 實例化上傳類 $upload->maxSize = 3145728 ;// 設置附件上傳大小 $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 設置附件上傳類型 $upload->rootPath = './Uploads/'; // 設置附件上傳根目錄 $upload->savePath = ''; // 設置附件上傳(子)目錄 // 上傳文件 $info = $upload->upload(); if(!$info) {// 上傳錯誤提示錯誤信息 $this->error($upload->getError()); }else{// 上傳成功 $this->success('上傳成功!'); }}

⑩ thinkphp如何實現圖片上傳

ThinkPHP里也有自帶的圖片上傳類(UploadFile.class.php) 和圖片模型類(Image.class.php)。方便於我們去實現圖片上傳功能。去看看手冊吧http://doc.thinkphp.cn/manual/upload.html

熱點內容
為什麼存儲卡 發布:2024-10-28 08:26:11 瀏覽:11
ssr自動搭建腳本 發布:2024-10-28 08:20:05 瀏覽:461
加密支導線 發布:2024-10-28 07:59:55 瀏覽:854
華中存儲的總公司 發布:2024-10-28 07:47:23 瀏覽:303
上傳文件至hdfs 發布:2024-10-28 07:40:40 瀏覽:936
美版安卓手機怎麼下載 發布:2024-10-28 07:38:10 瀏覽:286
賣褲子腳本 發布:2024-10-28 07:38:10 瀏覽:218
微信公眾平台緩存 發布:2024-10-28 07:25:15 瀏覽:824
小米電腦怎麼鎖屏密碼 發布:2024-10-28 07:24:34 瀏覽:942
eNSP華為IPv6如何連接伺服器 發布:2024-10-28 06:54:51 瀏覽:557