继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

常见问题02:php实现图片上传功能(带缩略图)

largeQ
关注TA
已关注
手记 824
粉丝 92
获赞 585

源码地址:php实现图片上传功能(带缩略图)

PicUpload

Summary

图片上传(带缩略图)

该插件集成了图片上以及图片压缩的功能。

核心方法为:

upload($folder, $thumb = true);
第一个参数$folder为上传的文件到哪个目录的意思,第二个参数默认为true表示同时上传缩略图。

Usage

上传图片:

function uploadPics($filename,$img_dir='test') {        //上传图片部分
        $result = [];        if(!empty($_FILES[$filename]['name'])) {
            $img = new UploadImg($filename);
            $img->upload($img_dir,false);
            $result['pic'] = str_replace(DS, '/', $img->path . '/' . $img->filename);
            $result['width'] = $img->width;
            $result['height'] = $img->height;
            $result['size'] = $img->size;            unset($img);
        }        return $result;
    }

上传图片(带缩略图):

function uploadPics($filename,$img_dir='test') {        //上传图片部分
        $result = [];        if(!empty($_FILES[$filename]['name'])) {

            $img = new UploadImg($filename);
            $thumb_w = 160;//缩略图宽
            $thumb_h = 100;//缩略图高
            $img->add_thumb('thumb', 'thumb_', $thumb_w, $thumb_h);
            $img->use_mment = false; // 关闭最大尺寸限制
            $img->upload($img_dir);

            $result['pic'] = str_replace(DS, '/', $img->path . '/' . $img->filename);
            $result['thumb'] = str_replace(DS, '/', $img->path . '/' . $img->thumb_filenames['thumb']['filename']);
            $result['width'] = $img->width;
            $result['height'] = $img->height;
            $result['size'] = $img->size;            unset($img);
        }        return $result;
    }

Author

nosee123

License

MIT Public License



作者:Nosee123
链接:https://www.jianshu.com/p/8e0f4c14c9b8

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP