Encoding an image as Base64 in PHP
There are many ways to encode images into base64 format using several built-in functions in PHP.
These functions include:
-
pathinfo
-
file_get_contents
-
base64_encode
-
mime_content_type
Encode images to Base64
in PHP using file_get_contents
, pathinfo
andbase64_encode
The image should have a defined path stored in a variable to encode the image as base64 using file_get_contents
, , pathinfo
and .base64_encode
Use pathinfo
the function to read the information. Then pass the resulting information to file_get_contents
the function.
file_get_contents
The job of is to read the image file into a string. We can pass the string to base64_encode
the function.
To display a base64-encoded image, concatenate the string data:image/
, pathinfo
the information from , the string ;base64
, and the base64-encoded string.
<?php
// Define a path for the image
$image_path = 'xps-unsplash.jpg';
// Check the image type
$image_type = pathinfo($image_path, PATHINFO_EXTENSION);
// Read the image into a string
$data = file_get_contents($image_path);
// Encode the image
$base64_encode = 'data:image:/' . $image_type . ';base64,' . base64_encode($data);
// Return the first 50 characters of the
// base64 encoded string
echo substr($base64_encode, 0, 50);
?>
Output:
data:image:/jpg;base64,/9j/4AAQSkZJRgABAQEASABIAAD
Encode images as Base64
in PHP using file_get_contents
andbase64_encode
To convert an image to base64, pass the path to the image directly to file_get_contents
the function.
file_get_contents
base64_encode
The function will return a string. It will help if you provide this string as a parameter to the function.
<?php
// Define a path for the image
$data = file_get_contents('xps-unsplash.jpg');
// Encode the image
$base64_encode = base64_encode($data);
// Return the first 50 characters of the
// base64 encoded string
echo substr($base64_encode, 0, 50);
?>
Output:
/9j/4AAQSkZJRgABAQEASABIAAD/4gIcSUNDX1BST0ZJTEUAAQ
Encode images as Base64
in PHP using base64_encode
andmime_content_type
Use file_get_contents
the function to read the image path. Then convert the resulting string into base64 encoding.
You can format the image source before passing it to the < <img>
img src="https://github.com/openwrt/images/blob/master ...src
To do this you need to format your image as data:{mime};base64,{data}
. You can infer from the format what image mime type you need.
You can mime_content_type
get the image mime type using the getImage function. Finally, you pass the format as the value of the attribute <img>
of the getImage tag src
.
<?php
// Define a path for the image
$image_path = 'xps-unsplash.jpg';
// Read the image path and convert it to
// base64
$base64_encode = base64_encode(file_get_contents($image_path));
// Prepare the image for use in an img
// src tag
$image_source = 'data: ' . mime_content_type($image_path) . ';base64,' . $base64_encode;
// Display the image in the img src tag
echo '<img src="' . $image_source . '">';
?>
Output:
Encode image to Base64 using custom function in PHP
In the definition of this function, you can use methods such as file_get_contents
, base64_encode
, pathinfo
and to handle image encoding.
You can use file_put_contents
to implement caching functionality for better performance.
<?php
function encode_html_image (
$image_file,
$alternate_text = NULL,
$image_cache,
$image_extension = NULL
) {
// check if the image is not a file
if (!is_file($image_file)) {
return false;
}
// Save the image as base64 extension for
// use as a cache
$image_with_base64_extension = "{$image_file}.base64";
if ($image_cache && is_file($image_with_base64_extension)) {
// Use the cache image
$base64_encoded_image = file_get_contents($image_with_base64_extension);
} else {
// Create a new base64 encoded image
$image_binary_data = file_get_contents($image_file);
$base64_encoded_image = base64_encode($image_binary_data);
if ($image_cache) {
file_put_contents($image_with_base64_extension, $base64_encoded_image);
}
}
$image_extension = pathinfo($image_file, PATHINFO_EXTENSION);
return "<img alt='{$alternate_text}' src='data:image/{$image_extension};base64,{$base64_encoded_image}' />";
}
$sample_image = 'xps-unsplash.jpg';
$encoded_image = encode_html_image($sample_image, 'XPS by DELL', true);
echo $encoded_image;
?>
Output:
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Check if a Post exists in PHP
Publish Date:2025/04/13 Views:170 Category:PHP
-
PHP $_POST is a super global variable that can contain key-value pairs of HTML form data submitted through the post method. We will learn different ways to check $_POST if a and contains some data in this article. These methods will use iss
PHP with Ajax
Publish Date:2025/04/13 Views:139 Category:PHP
-
We will use PHP and ajax by printing a simple sum of two numbers 2 and . Also, print a php array in JSON. 3 object We will also use PHP with ajax by getting the HTML formatted output from the number division in PHP. Printing simple addition
Store Div Id in PHP variable and pass it to JavaScript
Publish Date:2025/04/13 Views:51 Category:PHP
-
This article shows you how to div id store a in a PHP variable and pass it to JavaScript code. We will answer the following questions. What is div id ? How to div id store in a PHP variable? How to pass variables to JavaScript code? Let’s
Returns the article tag with ID from the action page
Publish Date:2025/04/13 Views:80 Category:PHP
-
Let's say you're in a login form and you enter the wrong information; in this case, you probably want to go back to the login page. PHP has a built-in function header() to redirect a page to a specific page. But what if the login page is at
Switching PHP versions on Ubuntu
Publish Date:2025/04/13 Views:78 Category:PHP
-
Different tasks may require running multiple versions of PHP. You may need to switch PHP versions by running two sites on the same server or testing older versions of code using outdated methods. We can switch PHP versions on Ubuntu using t
Resizing images in PHP
Publish Date:2025/04/13 Views:155 Category:PHP
-
In this tutorial article, we will discuss about resizing images in PHP. Load the image before resizing Before we can resize an image, we must first load it as an image resource in our script. This is file_get_contents() different from using
PHP upload image
Publish Date:2025/04/13 Views:61 Category:PHP
-
We can upload images in PHP using simple file upload operation, but first, php.ini file upload should be enabled from Files. This tutorial demonstrates how to upload images in PHP. php.ini Enable file upload from file in PHP to upload image
Creating a signature from Hash_hmac() and Sha256 in PHP
Publish Date:2025/04/13 Views:107 Category:PHP
-
PHP has one of the best encryption functions for data security. Hash_hmac() The encrypt function is one of the most famous encryptors. We'll show you how to use hash_hmac and sha256 encryptors to create 安全签名 one that you can store i
Updating PHP 7.x to 7.4 on CentOS
Publish Date:2025/04/13 Views:131 Category:PHP
-
This article shows the steps to update the PHP version from 7.x version to 7.4 in CentOS. How to Update PHP from 7.X to 7.4 in CentOS Update operating system packages. yum update -y Check your PHP version in CentOS. php -v Prints a list of