ÔõÑùÔÚThinkPHP6ÖоÙÐи»Îı¾±à¼Æ÷ͼƬÉÏ´«²Ù×÷£¿
thinkphp6×÷Ϊһ¿îÓÅÒìµÄphp¿ò¼Ü£¬ÌṩÁ˸»ºñµÄ²Ù×÷Àà¿âºÍ¹¤¾ß£¬Ê¹µÃ¿ª·¢Õß¿ÉÒÔ¸ü¿ì½ÝµØʵÏÖ¹¦Ð§ÐèÇó¡£ÔÚÍøÒ³¿ª·¢ÖУ¬¸»Îı¾±à¼Æ÷³£×÷Ϊ±Ø±¸¹¤¾ßÖ®Ò»£¬¿ÉÒÔ×ÊÖúÓû§¸üÀû±ãµØ±à¼ÄÚÈÝ¡£È»¶ø£¬µ±Óû§ÏëÒª²åÈëͼƬʱ£¬¾ÍÐèÒªÓÐͼƬÉÏ´«¹¦Ð§Ö§³Ö¡£
±¾ÎĽ«ÒÔUMEditor×÷ΪʾÀý£¬ÏÈÈÝÔÚThinkPHP6ÖÐÔõÑùʵÏÖ¸»Îı¾±à¼Æ÷ͼƬÉÏ´«¹¦Ð§¡£
µÚÒ»²½£ºÒýÈëUMEditor
½«UMEditorµÄ×ÊÔ´Îļþ£¨°üÀ¨js¡¢css¡¢imagesµÈ£©·Åµ½ÏîĿĿ¼ÏµÄpublicĿ¼ÖУ¬È»ºóÔÚHTMLÒ³ÃæÖÐÒýÈëUMEditorµÄÏà¹Ø×ÊÔ´Îļþ¡£Ê¾ÀýÈçÏ£º
<!-- ÒýÈëUMEditor --> <link href="/public/UMEditor/themes/default/css/umeditor.css" rel="stylesheet"> <script src="/public/UMEditor/umeditor.config.js"></script> <script src="/public/UMEditor/umeditor.min.js"></script>
µÇ¼ºó¸´ÖÆ
µÚ¶þ²½£º±àдͼƬÉÏ´«½Ó¿Ú
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
ÔÚThinkPHP6ÖУ¬¿ÉÒÔͨ¹ýRouteÀàºÍControllerÀàÀ´ÊµÏֽӿڵıàд¡£
ÔÚroutesĿ¼Ï½¨ÉèÒ»¸öupload.phpÎļþ£¬´úÂëÈçÏ£º
<?php use thinkacadeRoute; Route::post('upload/image', 'api/Upload/uploadImage');
µÇ¼ºó¸´ÖÆ
¸Ã·ÓÉÓ³ÉäµÄ¿ØÖÆÆ÷ÒªÁìΪUpload¿ØÖÆÆ÷µÄuploadImageÒªÁì¡£
ÔÚappĿ¼Ï½¨ÉèÒ»¸öapiĿ¼£¬È»ºóÔÚapiĿ¼Ï½¨ÉèÒ»¸öUpload¿ØÖÆÆ÷ºÍÒ»¸öUploadServiceЧÀÍ£¬´úÂëÈçÏ£º
Upload¿ØÖÆÆ÷£º
<?php namespace apppicontroller; use apppiserviceUploadService; use thinkacadeRequest; class Upload { public function uploadImage() { $file = Request::file('upfile'); $uploadService = new UploadService(); $result = $uploadService->uploadImage($file); return json($result); } }
µÇ¼ºó¸´ÖÆ
UploadServiceЧÀÍ£º
<?php namespace apppiservice; use thinkacadeFilesystem; use thinkacadeConfig; class UploadService { public function uploadImage($file) { $storage = Config::get('filesystem.default'); $savename = Filesystem::disk($storage)->putFile('images', $file); $url = Config::get("filesystem.disks.{$storage}.url") . '/' . str_replace('\', '/', $savename); return [ 'state' => 'SUCCESS', 'url' => $url, 'title' => '', 'original' => $file->getOriginalName(), 'type' => $file->getOriginalExtension(), 'size' => $file->getSize(), ]; } }
µÇ¼ºó¸´ÖÆ
ÔÚUploadServiceÖУ¬ÎÒÃÇʹÓÃÁËThinkPHP6ÌṩµÄÎļþ´æ´¢¹¦Ð§£¬ÒÔµÖ´ïͼƬÉÏ´«µÄÄ¿µÄ¡£ÏêϸʵÏÖʹÓÃÁËFilesystemÀàºÍConfigÀà¡£ÔÚÉèÖÃÎļþconfig/filesystem.phpÖУ¬ÐèÒªÉèÖÃÏìÓ¦µÄ´æ´¢·½·¨ºÍ·¾¶¡£
µÚÈý²½£ºÉèÖÃUMEditor
ÔÚHTMLÒ³ÃæÖÐʵÀý»¯UMEditor£¬²¢ÉèÖÃͼƬÉÏ´«½Ó¿Ú£º
<script type="text/javascript"> var um = UM.getEditor('myEditor', { imageUrl: '/upload/image', //ͼƬÉÏ´«½Ó¿Ú imageFieldName: 'upfile', //ͼƬÌá½»µÄ±íµ¥Ãû³Æ imageMaxSize: 2048000, //ͼƬ¾ÞϸÏÞÖÆ£¬µ¥Î»ÊÇ×Ö½Ú imageAllowFiles: ['.jpg', '.png', '.gif', '.jpeg'], //ÔÊÐíÉÏ´«µÄͼƬÀàÐÍ }); </script>
µÇ¼ºó¸´ÖÆ
ÉèÖÃUMEditorµÄÎļþÉÏ´«Â·¾¶£º
<script type="text/javascript"> UMEDITOR_CONFIG.serverUrl = '/public/UMEditor/php/controller.php/?action='; </script>
µÇ¼ºó¸´ÖÆ
ÔÚcontroller.phpÖУ¬ÐèÒª½«Í¼Æ¬ÉÏ´«µÄÇëÇóת·¢µ½ÎÒÃǸոձàдµÄͼƬÉÏ´«½Ó¿Ú¡£
ÖÁ´Ë£¬ÎÒÃÇÒѾÀֳɵØÔÚThinkPHP6ÖÐʵÏÖÁËUMEditor¸»Îı¾±à¼Æ÷µÄͼƬÉÏ´«¹¦Ð§¡£¿ª·¢Õß¿ÉÒÔͨ¹ýÀàËƵÄÒªÁ죬ʵÏÖÆäËü¸»Îı¾±à¼Æ÷µÄͼƬÉÏ´«¹¦Ð§¡£
ÒÔÉϾÍÊÇÔõÑùÔÚThinkPHP6ÖоÙÐи»Îı¾±à¼Æ÷ͼƬÉÏ´«²Ù×÷£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡