php8.5
Home/ Manual/ image / functions/ imagecreatefromstring

imagecreatefromstring

PHP function Edit on GitHub ✎

(PHP 4 >= 4.0.4, PHP 5, PHP 7, PHP 8)

Create a new image from the image stream in the string

Description

imagecreatefromstring(string $data): GdImage|false

imagecreatefromstring() returns an image identifier representing the image obtained from the given data. These types will be automatically detected if your build of PHP supports them: JPEG, PNG, GIF, BMP, WBMP, GD2, WEBP and AVIF.

Parameters

data

A string containing the image data.

Return Values

An image object will be returned on success. false is returned if the image type is unsupported, the data is not in a recognised format, or the image is corrupt and cannot be loaded.

Errors/Exceptions

imagecreatefromstring() raises an E_WARNING level error, if the data is not in a recognized format.

Changelog

VersionDescription
8.0.0On success, this function returns a GDImage instance now; previously, a resource was returned.
7.3.0WEBP is supported now (if supported by the libgd in use).

Examples

imagecreatefromstring() example

php
<?php
$data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
       . 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
       . 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
       . '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
$data = base64_decode($data);

$im = imagecreatefromstring($data);
if ($im !== false) {
    header('Content-Type: image/png');
    imagepng($im);
}
else {
    echo 'An error occurred.';
}
?>

The above example will output something similar to: Output of example : imagecreatefromstring()

See Also

Source: reference/image/functions/imagecreatefromstring.xml · from the official PHP manual (php/doc-en)