×ðÁú¿­Ê±¹ÙÍøµÇ¼

ÔõÑùÔÚ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ÖоÙÐи»Îı¾±à¼­Æ÷ͼƬÉÏ´«²Ù×÷£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
ÍøÕ¾µØͼ