imagecreatefromstring 


gd apg

CREATE a new image from the image stream in the string.





This function returns an image identifier representing the image obtained from the given $image.

The types supported by PHP wich will be automatically detected are: JPEG, PNG, GIF, BMP, WBMP and GD2.

This function returns FALSE if the image type is unsupported, the data is not in a recognised format, or the image is corrupt and cannot be loaded.

This function may also generates an E_WARNING level error, if the data is not in a recognized format.



<?php

GdImage
|false imagecreatefromstring string $image )

where,

$image A STRING containing the image data 

?>
 

  $image   


A string containing the image data.



  1 EXERCISE   

<?php

echo 'PHP VERSION: ' PHP_VERSION '<br><br>';

$img_src 'png/PNG 005 04.png';

$base64encoded 'dat/DAT 001 01 base64encoded.dat';

$base64decoded 'dat/DAT 001 01 base64decoded.dat';

// Choose any image file...
// if you prefer, use the star image 
// below as a reference

$img_bin fread(fopen($img_src'r'),
filesize($img_src));

$img_str base64_encode($img_bin);

// Save de image file as STRING
// encoded by base64_encode

$fse file_put_contents($base64encoded$img_str);

if(
$fse == true)
{
echo 
"Encoded file saved as expected!<br>";
}
else
{
echo 
"Encoded file NOT saved as expected!<br>";
}

$dec_dat fread(fopen($base64encoded'r'),
filesize($base64encoded));

$dec_str base64_decode($dec_dat);

// Save de image file as STRING
// decoded by base64_decode

$fsd file_put_contents($base64decoded$dec_str);

if(
$fsd == true)
{
echo 
"Decoded file saved as expected!<br>";
}
else
{
echo 
"Decoded file NOT saved as expected!<br>";
}

/* - - - - - - - - - - - - - - - - - - - - - - - -

To read these two files created 
in this exercise just use 
any common word processor.

- - - - - - - - - - - - - - - - - - - - - - - - */

?>

 RESULT   

PNG 005 04.png

PNG 005 04.png apr

Encoded file saved as expected!

Decoded file saved as expected!


The base64_encode and base64_decode functions are used to generate the STRING for use by the imagecreatefromstring function.

  2 EXERCISE   

<?php

echo 'PHP VERSION: ' PHP_VERSION '<br><br>';

$str2img 'png/PNG 082 02.png';

$img_str64 'dat/DAT 001 01 base64decoded.dat';

$result 'png/ 082 02 imagecreatefromstring.png';

$fp fopen($img_str64'r');
$fcontts fread($fpfilesize($img_str64));

// echo $fcontts;

$id_strimg imagecreatefromstring($fcontts);

imagepng($id_strimg$result);

echo 
'<br><br>' basename($result); ?><br><br>
<img src="<?php echo $result?>"
 alt="<?php echo $result?>
 title="<?php echo $result?>">

<?php fclose($fp); ?>


 RESULT   

DAT 001 01 base64decoded.dat


PNG 082 02 imagecreatefromstring.png

PNG 082 02 imagecreatefromstring.png apr


  3 EXERCISE   

<?php
 
echo 'PHP VERSION: ' PHP_VERSION '<br><br>';

$str2img 'png/PNG 083 03.png';

$img_str64 
'iVBORw0KGgoAAAANSUhEUgAAAIQAAACEBAMAAAC
jap6UAAAAB3RJTUUH2woHESodcsMP4gAAAAlwSFl
zAAAewQAAHsEBw2lUUwAAAARnQU1BAACxjwv8YQUA
AAAGUExURQAAAP8AABv/jSIAAAABdFJOUwBA5thmAA
AA/ElEQVR42u2WUQ7DIAxDewXuf9lpmlBsJ0D3a2Kp
Gi3hTbKI4XlarVbrTw3S931++81WNb6IPMbf+VT1jg
i0ks0MU7nmDsQc4Ua6FYGGVq3njuBxvbWqej9EFb/6
HOLXBPEcoK+WGCG4jdhWPaAWSCNETAQorN0sNUNwi2
kI8/eNmQYILGID93WOiJiujuX5jub6Iuo2wvDJs64I
jRe8vuJytdgRocbl6MVNVW4uG4QeN3xF4wuLP6K2V0
PmBsQYbJiWo73Lf7VBqJUYMnw83YCIJlq3WI4mf0QO
HrRykVmmiNxgem3zRug42xsV3giNX24obTFnRKvVah
30AdoGrbmpvCtJAAAAAElFTkSuQmCC'

// bas64 encoded

$img_str base64_decode($img_str64); 
// base64 decoded

/* 
echo 'The STRING to be used by imagecreatefromstring:<br><br>' . 
$img_str . '<br><br>';
*/

$id_strimg imagecreatefromstring($img_str);

imagepng($id_strimg$str2img);

echo 
basename($str2img); ?>
<br><br>The image created by the use of this STRING:
<br><br>
<img src="<?php echo $str2img?>"
 alt="<?php echo $str2img?>
 title="<?php echo $str2img?>">
 
 
 

 RESULT   

$img_str64


PNG 083 03.png

PNG 083 03.png apr