PHP implements base64 transcoding into images

PHP中文网
Release: 2023-03-15 17:28:01
Original
3837 people have browsed it

base64 image encoding conversion, wrote a method. Two parameters, one is base64 encoding and the other is the file path

<?php
    /**
    * base64转码图片
    * @param $base64
    * @param string $path
    * @return bool|string
    */
   function get_base64_img($base64,$path = &#39;data/upload/sign/&#39;){
    if (preg_match(&#39;/^(data:\s*image\/(\w+);base64,)/&#39;, $base64, $result)){
    $type = $result[2];
       $new_file = $path.time().".{$type}";
  if (file_put_contents($new_file, base64_decode(str_replace($result[1], &#39;&#39;, $base64)))){
    return $new_file;
    }else{
    return	false;
    }
    }
   }
?>
Copy after login


The above is the detailed content of PHP implements base64 transcoding into images. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!