PHP turns images into base64

Source: Internet
Author: User
Tags base64

PHP support for Base64 is very good, there is a built-in Base64_encode and Base64_decode responsible for the image of the Base64 encoding and decoding.

On the encoding, as long as the picture stream is read, and then using Base64_encode to encode can be obtained.

/** * Get image Base64 encoding (URL not supported) * @date 2017-02-20 19:41:22 * * @param $img _file incoming local image address * * @return string*/functionImgToBase64 ($img _file) {    $img _base64= ' '; if(file_exists($img _file)) {        $app _img_file=$img _file;//Picture Path        $img _info=getimagesize($app _img_file);//get pictures of size, type, etc.//echo ' <pre> '. Print_r ($img _info, True). ' </pre><br> ';        $fp=fopen($app _img_file, "R");//whether the picture is readable permission        if($fp) {            $filesize=filesize($app _img_file); $content=fread($fp,$filesize); $file _content=Chunk_split(Base64_encode($content));//base64 Encoding            Switch($img _info[2]) {//Interpreting picture Types                 Case1:$img _type= "GIF";  Break;  Case2:$img _type= "JPG";  Break;  Case3:$img _type= "png";  Break; }            $img _base64= ' data:image/'.$img _type. '; Base64, '.$file _content;//base64 encoding of synthetic images        }        fclose($fp); }    return $img _base64;//returns the base64 of a picture}//method used by call$img _dir=dirname(__file__) . '/uploads/img/11213223.jpg ';$img _base64= ImgToBase64 ($img _dir);Echo' $img _base64. ' > ';//picture form ShowEcho' ;Echo $img _base64;//Output BASE64 encoding

And decoding a little bit of trouble, the reason is that the image encoded into a base64 string, the code will be added to these characters data:image/png;base64, originally used for base64 identification. But if you put it directly into PHP with the Base64_decode function decoding will cause the final saved picture file format corruption, and the solution is to first remove this string of characters:

$base 64_string Explode $base 64_string // intercept Data:image/png;base64, the comma-after character $data Base64_decode  ($base 64_string[1]); // use Base64_decode to decode the truncated character file_put_contents ($url$data// write file and save

Ext.: https://www.cnblogs.com/cloudshadow/p/php_img_to_base64.html

Https://www.cnblogs.com/byuc/p/7600451.html

PHP to convert pictures to base64

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.