• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

thinkphp3.2上传图片兼容小程序

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

第一步在配置文件中设置图片的大小和路径

return array(
    //'配置项'=>'配置值'
    'img_save'=>[
        'size' =>[
            'app_gszc_Card'=>[
                'compress'=>array(
                    'w'=> 720,
                    'h'=> 520
                ),
                'page_size'=>array(
                    'w'=> 720,
                    'h'=> 520
                ),
            ],

        ],
        'save_path'=>[
            'app_gszc_Card' => array(
                'origin'            => './Uploads/gszc_card/origin/',
                'page_size'    => './Uploads/gszc_card/page_size/',
            )
        ]
    ]
);

二.上传图片类文件

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017-4-27 0027
 * Time: 10:49
 */
namespace Gszc\Event;

use \Think\Image;
use \Think\Upload;
class ImgEvent
{

    private $THINK_IMAGE = null;
    private $THINK_UPLOAD = null;

    public function __construct()
    {
        $this->THINK_IMAGE =  new Image();
        $this->THINK_UPLOAD =  new Upload();
    }

    /*
     * 保存base64文件
     * $img    string    base64类型的文件
     * $type   string    保存的文件类型
     *      app_user_head_img   用户头像
     *
     *
     */
    public function saveImg_base64($img = null , $type = null)
    {
        //获取保存图片配置
        $imgConfig_savePath = C("img_save.save_path");
        $imgConfig_size     = C("img_save.size");
        $saveFlag = false;
//        dump($imgConfig_savePath[$type]);
//        dump($imgConfig_size);

        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $img, $result) && $imgConfig_savePath[$type])
        {

            $img_ext                = $result[2]; //图片后缀
            $img_header             = $result[1];//图片头信息
            $new_file_name          = date('Ymd').'/'.uniqid().'.'.$img_ext;
            $origin_img_path        = '';//原图的保存路径
            $origin_img_save_flag   = true;//
            foreach($imgConfig_savePath[$type] as $k => $v)
            {

                if(!is_dir($v.date('Ymd')))
                {
                    mkdir($v.date('Ymd'),0777,true);
                }

                if ($k == 'origin')
                {
                    //先保存一份原图,然后其他尺寸的保存直接调用原图路径origin_img_path.
                    $origin_res = file_put_contents($v.$new_file_name, base64_decode(str_replace($img_header, '', $img)));
                    if (!$origin_res)
                    {
                        $origin_img_save_flag = false;
                        break;
                    }
                    else
                    {
                        $saveFlag = $new_file_name;
                        $origin_img_path = $v.$new_file_name;
                        $this->THINK_IMAGE->open($origin_img_path);
                    }
                }
                else
                {
                    if ($origin_img_save_flag)
                    {
                        $width = $imgConfig_size[$type][$k]['w'];
                        $height = $imgConfig_size[$type][$k]['h'];
                        $this->THINK_IMAGE->thumb($width, $height,3)->save($v.$new_file_name);
                    }

                }
            }

        }
        return $saveFlag;
    }


    /*
     *  保存file类型文件
     *
     */
    public function saveImg_file($img = null , $type = null)
    {

        //获取保存图片配置
        $imgConfig_savePath = C("img_save.save_path");
        $imgConfig_size     = C("img_save.size");
        $saveFlag = false;

        if ($img && $imgConfig_savePath[$type])
        {

            $origin_img_save_flag   = true;
            foreach($imgConfig_savePath[$type] as $k => $v)
            {

                if(!is_dir($v.date('Ymd')))
                {
                    mkdir($v.date('Ymd'),0777,true);
                }

                if ($k == 'origin')
                {
                    $this->THINK_UPLOAD->rootPath = $v;
                    $this->THINK_UPLOAD->subName = date('Ymd');
                    $this->THINK_UPLOAD->saveName = array('uniqid','');
                    $saveRes = $this->THINK_UPLOAD->uploadOne($img);
                    if (!$saveRes)
                    {
                        $origin_img_save_flag = false;
                    }
                    else
                    {
                        $saveFlag = true;
                        $origin_img_path = $v.$saveRes['savepath'].$saveRes['savename'];
                        $this->THINK_IMAGE->open($origin_img_path);
                        $fileName = $saveRes['savepath'].$saveRes['savename'];
                    }
                }
                else
                {
                    if ($origin_img_save_flag)
                    {
                        $width = $imgConfig_size[$type][$k]['w'];
                        $height = $imgConfig_size[$type][$k]['h'];
                        $saveFlag = $this->THINK_IMAGE->thumb($width, $height,3)->save($v.$fileName);

                        if (!$saveFlag)
                        {
                            $saveFlag = false;
                            $origin_img_save_flag = false;
                        }

                    }
                }


            }

            if($saveFlag)
            {
                $saveFlag = $fileName;
            }

        }
        return $saveFlag;
    }

    /*
     *  保存file类型文件,多文件
     *
     */
    public function saveImgs_files($imgs = null , $type = null)
    {

        //获取保存图片配置s
        $imgConfig_savePath = C("img_save.save_path");
        $imgConfig_size     = C("img_save.size");
        $saveFlag = false;
        $imgResArr = array();//保存图片路径
        $origin_img_path = array();//原图路径

        if ($imgs && $imgConfig_savePath[$type])
        {

            $origin_img_save_flag   = true;
            foreach($imgConfig_savePath[$type] as $k => $v)
            {

                if(!is_dir($v.date('Ymd')))
                {
                    mkdir($v.date('Ymd'),0777,true);
                }

                if ($k == 'origin')
                {
                    $this->THINK_UPLOAD->rootPath = $v;
                    $this->THINK_UPLOAD->subName = date('Ymd');
                    $this->THINK_UPLOAD->saveName = array('uniqid','');
                    $saveRes = $this->THINK_UPLOAD->upload($imgs);

                    if ($saveRes)
                    {
                        $saveFlag = true;
                        foreach ($saveRes as $srK => $srV)
                        {
                            $origin_img_path[] = $v.$saveRes[$srK]['savepath'].$saveRes[$srK]['savename'];
                            $fileName = $saveRes[$srK]['savepath'].$saveRes[$srK]['savename'];
                            $imgResArr[] = $fileName;
                        }


                    }
                }
                else
                {
                    foreach($origin_img_path as $oipK => $oipV)
                    {
                        if ($saveFlag)
                        {
                            $width = $imgConfig_size[$type][$k]['w'];
                            $height = $imgConfig_size[$type][$k]['h'];
                            $this->THINK_IMAGE->open($oipV);
                            $saveThumb = $this->THINK_IMAGE->thumb($width, $height,3)->save($v.$imgResArr[$oipK]);
                            if (!$saveThumb)
                            {
                                $saveFlag = false;
                                break;
                            }
                        }
                    }

                }

            }

            if ($saveFlag)
            {
                $saveFlag = $imgResArr;
            }
        }
        return $saveFlag;
    }

    /*
     * 保存微信头像 - url
     *
     */
    public function save_WxImg($picUrl = '',$type = null)
    {

        $ch = curl_init($picUrl);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_NOBODY, 0);    //对body进行输出。
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $package = curl_exec($ch);
        $httpInfo = curl_getinfo($ch);
        curl_close($ch);
        $media = array_merge(array('mediaBody' => $package), $httpInfo);

        //求出文件格式
        preg_match('/\w\/(\w+)/i', $media["content_type"], $extmatches);
        $fileExt = $extmatches[1];

        $saveName = md5(microtime(true)).".{$fileExt}";

        //获取保存图片配置
        $imgConfig_savePath = C("img_save.save_path");
        $imgConfig_size     = C("img_save.size");
        $saveFlag = false;

        if ($imgConfig_savePath[$type])
        {

            $origin_img_save_flag   = true;
            foreach($imgConfig_savePath[$type] as $k => $v)
            {

                if(!is_dir($v.date('Ymd')))
                {
                    mkdir($v.date('Ymd'),0777,true);
                }

                if ($k == 'origin')
                {
                    file_put_contents($v.'/'.date('Ymd').'/'.$saveName,$media['mediaBody']);

                    $origin_img_path = $v.'/'.date('Ymd').'/'.$saveName;
                    $this->THINK_IMAGE->open($origin_img_path);
                    $fileName = date('Ymd').'/'.$saveName;
                }
                else
                {
                    if ($origin_img_save_flag)
                    {
                        $width = $imgConfig_size[$type][$k]['w'];
                        $height = $imgConfig_size[$type][$k]['h'];
                        $saveFlag = $this->THINK_IMAGE->thumb($width, $height,3)->save($v.$fileName);

                        if (!$saveFlag)
                        {
                            $saveFlag = false;
                            $origin_img_save_flag = false;
                        }
                        else
                        {
                            $saveFlag = $fileName;
                        }
                    }

                }
            }
        }
        return $saveFlag;

    }

    /*
     *  保存file类型文件
     *
     */
    public function saveImg_Wx($WxImg = array() , $type = null)
    {

        //获取保存图片配置
        $imgConfig_savePath = C("img_save.save_path");
        $imgConfig_size     = C("img_save.size");
        $saveFlag = false;
        $imgResArr = array();
        
        if ($WxImg && $imgConfig_savePath[$type])
        {


            foreach ($WxImg as $wiK => $wiV)
            {

                $wxImg = $this->getWxImg($wiV);

                $origin_img_save_flag   = true;
                foreach($imgConfig_savePath[$type] as $k => $v)
                {

                    if(!is_dir($v.date('Ymd')))
                    {
                        mkdir($v.date('Ymd'),0777,true);
                    }

                    if ($k == 'origin')
                    {
                        file_put_contents($v.'/'.date('Ymd').'/'.$wxImg['saveName'],$wxImg['imgMedia']['mediaBody']);

                        $origin_img_path = $v.'/'.date('Ymd').'/'.$wxImg['saveName'];
                        $this->THINK_IMAGE->open($origin_img_path);
                        $fileName = date('Ymd').'/'.$wxImg['saveName'];
                        $imgResArr[] = $fileName;
                    }
                    else
                    {
                        if ($origin_img_save_flag)
                        {
                            $width = $imgConfig_size[$type][$k]['w'];
                            $height = $imgConfig_size[$type][$k]['h'];
                            $saveFlag = $this->THINK_IMAGE->thumb($width, $height,3)->save($v.$fileName);

                            if (!$saveFlag)
                            {
                                $saveFlag = false;
                                $origin_img_save_flag = false;
                            }
                            else
                            {
                                $saveFlag = $fileName;
                            }
                        }

                    }
                }
            }


            if($saveFlag)
            {
                $saveFlag = $imgResArr;
            }

        }
        return $saveFlag;
    }


    /*
     * 保存用户的推广图片
     */
    public function save_userShareImg($bg_img_path = '' , $head_img_path = '' , $qrCode_img_path = '' , $saveFile = '' , $userName = '' , $userDscp = '' , $font_path = '')
    {

        
        //设置头像图片为圆角
        $head_img_radius = $this->radius_img($head_img_path,240);
        //合并到背景图中
        $this->mergeImage($bg_img_path,$head_img_radius,$saveFile,array('left' => 200, 'top' => 55, 'width' => 480, 'height' => 480));
        //设置名称
        $this->writeText($saveFile, $saveFile, $userName,255,255,255,1010,32,$font_path, array());
        //设置二维码内容
        $this->mergeImage($saveFile,$qrCode_img_path,$saveFile,array('left' => 255, 'top' => 684, 'width' => 250, 'height' => 245));

        return $saveFile;
    }

    /*
     * 合并图片
     */
    private function mergeImage($bg_img, $main_img, $saveFile, $param)
    {

        extract($param);
//        list($bgWidth, $bgHeight) = getimagesize($bg_img);
        $bgImg = $this->imagecreate($bg_img);
        if (!is_resource($main_img))
        {
            list($mainWidth, $mainHeight) = getimagesize($main_img);
            $mainImg = $this->imagecreate($main_img);
        }
        else
        {
            $mainImg = $main_img;

            $mainWidth = $width;
            $mainHeight = $height;
        }
        imagecopyresized($bgImg, $mainImg, $left, $top, 0, 0, $width, $height, $mainWidth, $mainHeight);
//        imagecopyresized($bgImg, $mainImg, $left, $top, 0, 0, $width, $height, $width,$height);
        ob_start();
        // output jpeg (or any other chosen) format & quality
        imagejpeg($bgImg, NULL, 100);
        $contents = ob_get_contents();
        ob_end_clean();
        imagedestroy($bgImg);
        imagedestroy($mainImg);
        $fh = fopen($saveFile, "w+");
        fwrite($fh, $contents);
        fclose($fh);
    }

    /*
     * 图片写文字
     * $bg_img 背景图片
     * $saveFile 保存图片路径
     * $text 文字
     * $fontColor 字体颜色
     * $top 图片距离顶端高度
     * $fontSize 字体大小
     * $font 字体路径
     * $param 传递的参数
     */
    private function writeText($bg_img, $saveFile, $text, $colorR,$colorG,$colorB , $top , $fontSize , $font , $param = array())
    {
        list($bgWidth, $bgHeight) = getimagesize($bg_img);
        $im = imagecreatefromjpeg($bg_img);

        $fontColor = imagecolorallocate($im, $colorR, $colorG, $colorB);//字的RGB颜色
        $str = mb_convert_encoding($text, "html-entities", "utf-8");;//解决乱码问题

        $fontBox = imagettfbbox($fontSize, 0, $font, $str);//文字水平居中实质
        $width = imagesx($im);
        imagettftext($im, $fontSize, 0, ceil(($width - $fontBox[2]) / 2), $top, $fontColor, $font, $str);


        ob_start();
        // output jpeg (or any other chosen) format & quality
        imagejpeg($im, NULL, 100);
        $contents = ob_get_contents();
        ob_end_clean();
        imagedestroy($im);
        $fh = fopen($saveFile, "w+");
        fwrite($fh, $contents);
        fclose($fh);
    }

    /**
     * 处理圆角图片
     * @param  string $imgPath 源图片路径
     * @param  integer $radius 圆角半径长度默认为15,处理成圆型
     * @return [type]           [description]
     */
    public function radius_img($imgPath = '', $radius = 65)
    {
        $ext = pathinfo($imgPath);
        $src_img = null;
        switch ($ext['extension']) {
            case 'jpg':
                $src_img = imagecreatefromjpeg($imgPath);
                break;
            case 'png':
                $src_img = imagecreatefrompng($imgPath);
                break;
            default:
                $src_img = imagecreatefromjpeg($imgPath);
                break;
        }
        $wh = getimagesize($imgPath);
        $w = $wh[0];
        $h = $wh[1];
        // $radius = $radius == 0 ? (min($w, $h) / 2) : $radius;
        $img = imagecreatetruecolor($w, $h);
        //这一句一定要有
        imagesavealpha($img, true);
        //拾取一个完全透明的颜色,最后一个参数127为全透明
        $bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
        imagefill($img, 0, 0, $bg);
        $r = $radius; //圆 角半径
        for ($x = 0; $x < $w; $x++) {
            for ($y = 0; $y < $h; $y++) {
                $rgbColor = imagecolorat($src_img, $x, $y);
                if (($x >= $radius && $x <= ($w - $radius)) || ($y >= 
                      

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
SpringBoot+微信小程序_保存微信登录者的个人信息发布时间:2022-07-18
下一篇:
二十四、小程序中改变checkbox和radio的样式发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap