Home >Backend Development >PHP Tutorial >php convert image to base64 encoding

php convert image to base64 encoding

WBOY
WBOYOriginal
2016-07-29 08:57:45926browse

<code><span><?php</span><span>$file</span> = <span>"example.jpg"</span>;
<span>$type</span> = getimagesize( <span>$file</span> ); <span>//取得图片的大小,类型等</span><span>$file_content</span> = base64_encode( file_get_contents( <span>$file</span> ) );
<span>switch</span> ( <span>$type</span>[<span>2</span>] ) { <span>//判读图片类型</span><span>case</span><span>1</span>:
        <span>$img_type</span> = <span>"gif"</span>;
        <span>break</span>;
    <span>case</span><span>2</span>:
        <span>$img_type</span> = <span>"jpg"</span>;
        <span>break</span>;
    <span>case</span><span>3</span>:
        <span>$img_type</span> = <span>"png"</span>;
        <span>break</span>;
}
<span>$img</span> = <span>'data:image/'</span> . <span>$img_type</span> . <span>';base64,'</span> . <span>$file_content</span>; <span>//合成图片的base64编码</span><span>echo</span><span>'<img src="'</span> . <span>$img</span> . <span>'" >'</span>;</code>


').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces how to convert image to base64 encoding in PHP, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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
Previous article:PHP variable principleNext article:PHP variable principle