zoukankan      html  css  js  c++  java
  • 图片转base64

    /**
     * 查看通行证
     */
    public function viewPassCode() {
        $visitor_id = $this->input_data['visitor_id'];
        $customer_visitor = M('customer_visitor');
        $customer_visitor_info = $customer_visitor->where(['id'=>$visitor_id])->find();
        $qr_content = $customer_visitor_info['qr_content'];
        // $qrCode = new QrCode($qr_content);
        // // 指定内容类型
        // header('Content-Type: '.$qrCode->getContentType());
        // // 输出二维码
        // echo $qrCode->writeString();
    
        $qrCode = new QrCode($qr_content);
        $imgContent = $qrCode->writeString();
        $base64 = ImageUtil::contentToBase64($imgContent);
        $this->json->ok($base64);
    }
    
    <?php
    
    /**
     * 图片工具
     * User: Eden
     * Date: 19-4-26 上午9:23
     */
    
    namespace CommonUtil;
    
    class ImageUtil extends CommonUtil
    {
        /**
         * 获取网络图片的Base64编码
         * $img_url 网络图片地址
         * $hasPre  是否有前缀
         * @return string
         */
        public static function imgToBase64($img_url,$hasPre = true)
        {
            $img_base64 = '';
            $imageInfo = getimagesize($img_url);
            if (!$imageInfo) {
                return false;
            }
            $img_base64 = "" . chunk_split(base64_encode(file_get_contents($img_url)));
            if ($hasPre) {
                $img_base64 = 'data:' . $imageInfo['mime'] . ';base64,'.$img_base64;
            }
            return $img_base64;
        }
    
        /**
         * 获取网络图片的Base64编码
         * $img_url 网络图片地址
         * $hasPre  是否有前缀
         * @return string
         */
        public static function contentToBase64($content,$hasPre = true)
        {
            $img_base64 = '';
            $img_base64 = "" . chunk_split(base64_encode($content));
            if ($hasPre) {
                $img_base64 = 'data:image/png;base64,'.$img_base64;
            }
            return $img_base64;
        }
    }
    
    
  • 相关阅读:
    数据结构2
    EF Fluent API
    VS2017+mysql5.7 连接数据库生成实体
    JavaScript中的枚举
    EasyUI datagird 排序 按数字类型的问题
    php 将秒数转换为时间(年、天、小时、分、秒)
    mySQL把秒转换成日期
    Android 界面间传参数
    android 登陆界面
    Android 在已有的项目上创建新的项目
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/14598271.html
Copyright © 2011-2022 走看看