将网络图片Base64(摘抄笔记)

public static function networkImgToBase64($url){

        if(false === self::urlExists($url)){
            return false;
        }

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

        $file_content = base64_encode(file_get_contents($url)); // base64编码
        switch ($img_info[2]) {
            case 1: $img_type = "gif";
                break;
            case 2: $img_type = "jpg";
                break;
            case 3: $img_type = "png";
                break;
            default:
                $img_type = "jpg";
        }

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

        return $img_base64;

    }

    public static function urlExists($url) {

        if (!is_string($url) || empty($url)) {
            return false;
        }

        $head = @get_headers($url);

        if(is_array($head)) {
            return true;
        }

        return false;
    }
原文地址:https://www.cnblogs.com/youligai/p/15346647.html