基于ThinkPHP5.0实现图片上传插件
YPE html>
ç¸ä¿¡å¾å¤äººå¨ç½ç«åå°æä½çæ¶åï¼é½éè¦ä½¿ç¨ä¸ä¼ å¾çè¿ä¸ªåè½ï¼æ们ç¨åºåé常é½ä¼å°å¾çè£åªåå é¤ï¼æ¬ææ¯ç±ç«ææ¯é¢é为大家ä»ç»çåºäºThinkPHP5.0å®ç°å¾çä¸ä¼ æ件ï¼å¯ä»¥åèä¸ææä½ã
Â
ææé¢è§å¾ï¼
该æ件主è¦åè½æ¯ï¼å¯é¢è§è£åªå¾çåä¿ååå¾çï¼æ§è¡è£åªå¾çåä¼å é¤ è£åªçåå¾çç®å½ï¼ä»¥ä¾¿åå°ç©ºé´ã
ä¸ãä¸è½½é件
å°åï¼é¾æ¥: https://pan.baidu.com/s/1nuQ4NgP å¯ç : 4pbu
äºãå°é件ä¸çCropAvatar.phpæ¾å°èªå·±ç¨åºç®å½extend/orgç®å½ä¸,å¦æéå° exif_imagetype é误ï¼éè¦æå¼ php.ini ä¸ç extension=php_exif.dll
ä¸ãcommon.phpå ¬å ±å½æ°
æ¾å°åºç¨ç¨åºç®å½ä¸çcommon.phpæ件ï¼å¨éé¢æ·»å å ¬ç¨å½æ°ï¼
/** * 转æ¢åè * @param $bytes ä¼ å ¥åèæ°å¼ * @param int $decimals * @return string BKMGTP */ function human_filesize($bytes, $decimals = 2) { $sz = 'BKMGTP'; $factor = floor((strlen($bytes) - 1) / 3); return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[$factor]; } /** * å é¤è¯¥ç®å½ä»¥å该ç®å½ä¸é¢çæææ件åæ件夹 * @param $dir ç®å½ * @return bool */ function removeDir($dirName) { //å¤æä¼ å ¥åæ°æ¯å¦ç®å½ï¼å¦ä¸æ¯æ§è¡å é¤æ件 if (!is_dir($dirName)) { //å é¤æ件 @unlink($dirName); } //å¦æä¼ å ¥æ¯ç®å½ï¼ä½¿ç¨@opendirå°è¯¥ç®å½æå¼ï¼å°è¿åçå¥æèµå¼ç»$handle $handle = @opendir($dirName); //è¿éæç¡®å°æµè¯è¿åå¼æ¯å¦å ¨çäºï¼å¼åç±»åé½ç¸åï¼FALSE //å¦åä»»ä½ç®å½é¡¹çå称æ±å¼ä¸º FALSE çé½ä¼å¯¼è´å¾ªç¯åæ¢ï¼ä¾å¦ä¸ä¸ªç®å½å为â0âï¼ while (($file = @readdir($handle)) !== false) { //å¨æ件ç»æä¸ï¼é½ä¼å å«å½¢å¦â.âåâ..âçåä¸ç»æ //ä½æ¯å®ä»¬ä¸æ¯æ件æè æ件夹 if ($file != '.' && $file != '..') { //å½åæ件$dir为æ件ç®å½+æ件 $dir = $dirName . '/' .$file; //å¤æ$diræ¯å¦ä¸ºç®å½ï¼å¦ææ¯ç®å½åéå½è°ç¨reMoveDir($dirName)å½æ° //å°å ¶ä¸çæ件åç®å½é½å é¤ï¼å¦æä¸æ¯ç®å½ï¼åå é¤è¯¥æ件 is_dir($dir) ? removeDir($dir) : @unlink($dir); } } closedir($handle); return rmdir($dirName); }
åãä¿®æ¹é ç½®æ件
æ¾å°åºç¨ç¨åºç®å½ä¸çé ç½®æ件config.phpï¼å¨é ç½®éé¢æ·»å ï¼
//ä¸ä¼ é ç½® æ ¹ç®å½åéæ·»å ä¸ä¸ª . 'syc_images' => [ //缩å¾ä¿åä½ç½® 'thumb' => './uploads/thumbs', //é件å¾çä¿åä½ç½® 'image' => './uploads/images', //è£åªçåå§å¾çä¿åä½ç½® 'original' => './uploads/original', //ä¸ä¼ éå¶ 2*1024*1024 'size' => 2097152, ],
äºãå°file-thumd-modal.htmlæ件æ¾å°viewæ件夹éé¢ï¼ä½ä¸ºä¸ä¸ªæ¨¡æ¿æ件ï¼å¦ï¼
å ãæ§å¶å¨ï¼é件ä¸çThumbs.phpæ件æ¯ä¸ä¸ªç¤ºä¾æ§å¶å¨ï¼å¯ä»¥å°è¯¥æ§å¶å¨æ¿å°ä½ çç¨åºç®å½ä¸ï¼ä¿®æ¹ä¸å½å空é´ãæ¹æ³åçï¼è¯¥æ§å¶å¨éé¢æ个æ¹æ³ä¸ºï¼index()ï¼è¯¥æ¹æ³æ¯ä¸ä¼ æ件è°ç¨çæ¹æ³ï¼å¦ææ³è¦æ¾ç¤ºä¸ä¸ªéæ©æ件ç页é¢è¿éèªå·±å个æä½æ¹æ³ï¼å¦ï¼test()ã
public function test() { return $this->fetch(); } public function index() { ..... }
ä¸ãè§å¾å±ï¼å¨viewæ件夹éå®ä¹ä¸ä¸ªä¸testæ¹æ³ç¸å ³ç模æ¿æ件ï¼è¯¥é¡µé¢ç
æ ç¾éé¢éè¦å ä¸class="page-header-fixed"ï¼å 为JSéé¢éè¦ä½¿ç¨å°ã éæ©å¾çè¿ä¸ªæ ç¾ç¨æ¥æå¼æ¨¡ææ¡ï¼class为avatar-viewï¼{include file="public/file-thumd-modal"}ç¨æ¥å¼å ¥file-thumd-modal.htmlæ件ï¼éè¦æ¾å°è¯¥æ¨¡æ¿æ件åå°åã//id="art-thumb" è¿åçå¾çå°å //id="preview" è¿åç img å°å¾ //该页é¢éå¼å ¥bootstrapçjsï¼cssåjqueryçæ件{// file-thumd-modal.html å¯ä»¥å¼ç¨å°å ¶ä»é¡µé¢è¦ç¨å°çå°æ¹} {// å¼å ¥ä¸ä¼ å¾çmodal} {include file="public/file-thumd-modal"}{// è°ç¨ avatar-view} éæ©å¾ç
å好è¿äºæ¥éª¤ï¼æ们就å¯ä»¥ä½¿ç¨è¯¥æ件äºï¼å ¶å®çä¸äºåï¼å¾çåæ¾ä½ç½®ï¼å¾çè¿åè·¯å¾ççï¼å¯ä»¥åå¨ç¸åºçæ件éé¢è¿è¡ä¿®æ¹å³å¯ã
éè¿ç±ç«ææ¯é¢éå°ç¼ä»ç»çåºäºThinkPHP5.0å®ç°å¾çä¸ä¼ æ件ï¼å¤§å®¶é½æ¸ æ¥äºæä½æ¹æ³å§ï¼å¸æ大家ä»ç»çé 读å°ç¼ç»å¤§å®¶æ¨èçå 容ï¼ç±æèªå·±çå³å¨ææã