Friday, August 29, 2025
HomeLanguagesPHP | Imagick __toString() Function

PHP | Imagick __toString() Function

The Imagick::__toString() function is an inbuilt function in PHP which is used to return the image as a string. This function will only return a single image and should not be used for Imagick objects containing multiple images.

Syntax:

string Imagick::__toString( void )

Parameters:This function doesn’t accepts any parameter.

Return Value: This function returns the current image as string.

Exceptions: This function throws ImagickException on error.

Below programs illustrate the Imagick::__toString() function in PHP:

Program 1:




<?php
  
// Create a new imagick object
$imagick = new Imagick(
  
// Convert it into string
$string = $imagick->__toString();
echo $string;
?>


Output:

This will display a large text which is the string form of image.

Program 2:




<?php
  
// Create a new imagick object
$imagick = new Imagick(
  
// Convert it into string
$string = $imagick->__toString();
  
// Show the output from string
header("Content-Type: image/png");
echo $string;
?>


Output:

Reference: https://www.php.net/manual/en/imagick.tostring.php

RELATED ARTICLES

Most Popular

Dominic
32249 POSTS0 COMMENTS
Milvus
80 POSTS0 COMMENTS
Nango Kala
6617 POSTS0 COMMENTS
Nicole Veronica
11790 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11838 POSTS0 COMMENTS
Shaida Kate Naidoo
6731 POSTS0 COMMENTS
Ted Musemwa
7012 POSTS0 COMMENTS
Thapelo Manthata
6689 POSTS0 COMMENTS
Umr Jansen
6701 POSTS0 COMMENTS