PHP turns images into base64

Source: Internet
Author: User

Tag:ret   div   cti    Address    gpo    encode   exists   base   gif   

/** * Get image Base64 encoding (URL not supported) * @param $img _file incoming local image address * @return string */function imgToBase64 ($img _file) {$img _base6    4 = ";  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 Code switch ($img _info[2]) {//Interpret picture type 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 base64 encoding of _content;//synthetic images} fclose ($FP); } return $img _base64; Returns the method used by the base64}//call of the picture $img_dir = DirName (__file__). '/uploads/img/11213223.jpg '; $img _base64 = ImgToBase64 ($img _dir); Echo '  ';           Echo ' 

  

PHP turns images into base64

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.