Jump to content

Storing Images in PHP Scripts


flyhoney

Recommended Posts

This is semi-retarded, but I want to try it nonetheless.

 

Is is possible to store an image as a string in a PHP file in order to output it?

 

i.e. something like this:

 

$image = 'jfa we09fa'3pofjaspdfoua wpi WEIRD BINARY DATA STUFF sdflka sd;lf aksdfl;kasdf';
header('Content-Type: image/jpg');
echo $image;

Link to comment
https://forums.phpfreaks.com/topic/122621-storing-images-in-php-scripts/
Share on other sites

From the documentation:

<?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);
    imagedestroy($im);
}
else {
    echo 'An error occurred.';
}
?>

 

looks like what I need.  So to get the $data should I just print the result of file_get_contents and copy that to a string in my script?

I've not tried it (I do it with with gl though), but have a look at this.

 

If he wants to use GD, that's different than just outputting an image.  And actually, flyhoney, you could shorten it to:

 

<?php
header('Content-Type: image/jpg');
readfile('filename.jpg');

Using base64 encoded data is only necessary if the method used to transmit or store the data does not support binary. Binary data can be directly stored in a variable or a file.

 

Using gd functions to simply create and output an image from data in a string is not necessary unless you manipulate the image using other gd functions, which the posted code is not doing. The code posted above with the gd functions in it is a huge waste of processor time and resources.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.