PHP.net Official Documentation – base64_encode() function.How to convert from string to array using PHP.We hope you could understand its benefit, drawback of converting and displaying image as base64, and make good use of it in your projects. The guide on how to convert image to base64 using PHP is now over. Encode the image data to base64 and display the imageĮcho base64_encode($imageData) Final thoughts Set header as the image mimetype we want Using this method will have the php page to display the page as if it’s displaying an image file. is the extracted base64 data from the image.įor example, with png image format: echo '' Use PHP to output as a standalone image.We can display the image in tag by passing the base64 data as this format: echo '' Using PHP to output as a standalone image.Usually, there are two ways we wanted to display the image in base64 format: Open image or URL in read moreįclose($file) Display the image in base64 format This is another safe method to get image content that won’t be blocked on most web hosting. Option 2: Get image content using stream handler Please note that, there would be web hosting disabled the file_get_contents() for security purposes. $imageData = file_get_contents('image.png') Get image content from image file or URL This is the basic method to extract file contents. Option 1: Get image content using file_get_contents() There is various ways to get the image file content. The primary steps to convert an image to base64 is: icons) that would save the amount of requests to a webpage. In conclusion, the benefit mostly only applies to small, tiny images (eg. The converted base64 format’s amount of data will be always bigger than the original binary format by around 33%. Therefore, the number of HTTP request is recuded.Īlthough so, the actual data transmission using the image’s base64 data is actually more than the binary file itself. This way, we can display, or transfer the image data easier in text format.Īnother minor benefit of using base64 image format is to be able to display the image on webpages using its base64 data in text format, without having to load the image file. The main purpose of converting image to base64 data is to be able to transmit and store image data from binary format to ASCII format. Optionally, to display the image using converted base64 data: echo '' īelow is more detailed information on the conversion process with notes and alternative methods for converting image to base64 data and display it. The script below will use PHP to convert image to base64: // Get the image content
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |