美文网首页
PHP图片转base 64(来源:梦行云软件)

PHP图片转base 64(来源:梦行云软件)

作者: 梦行Monxin | 来源:发表于2020-10-07 19:32 被阅读0次

function imgToBase64($img_file) {//图片转 base 64

    $img_base64 = '';

    if (file_exists($img_file)) {

        $app_img_file = $img_file; // 图片路径

        $img_info = getimagesize($app_img_file); // 取得图片的大小,类型等

        //echo '<pre>' . print_r($img_info, true) . '</pre><br>';

        $fp = fopen($app_img_file, "r"); // 图片是否可读权限

        if ($fp) {

            $filesize = filesize($app_img_file);

            $content = fread($fp, $filesize);

            $file_content = chunk_split(base64_encode($content)); // base64编码

            switch ($img_info[2]) {          //判读图片类型

                case 1: $img_type = "gif";

                    break;

                case 2: $img_type = "jpg";

                    break;

                case 3: $img_type = "png";

                    break;

            }

            $img_base64 = 'data:image/' . $img_type . ';base64,' . $file_content;//合成图片的base64编码

        }

        fclose($fp);

    }

    return $img_base64; //返回图片的base64

}

相关文章

网友评论

      本文标题:PHP图片转base 64(来源:梦行云软件)

      本文链接:https://www.haomeiwen.com/subject/izrtpktx.html