flyhoney Posted September 3, 2008 Share Posted September 3, 2008 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 More sharing options...
DarkWater Posted September 3, 2008 Share Posted September 3, 2008 Yes, but make sure you actually have the correct binary data. file_get_contents() should do the trick. Link to comment https://forums.phpfreaks.com/topic/122621-storing-images-in-php-scripts/#findComment-633130 Share on other sites More sharing options...
rarebit Posted September 3, 2008 Share Posted September 3, 2008 I've not tried it (I do it with with gl though), but have a look at this. Link to comment https://forums.phpfreaks.com/topic/122621-storing-images-in-php-scripts/#findComment-633132 Share on other sites More sharing options...
flyhoney Posted September 3, 2008 Author Share Posted September 3, 2008 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? Link to comment https://forums.phpfreaks.com/topic/122621-storing-images-in-php-scripts/#findComment-633138 Share on other sites More sharing options...
DarkWater Posted September 3, 2008 Share Posted September 3, 2008 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'); Link to comment https://forums.phpfreaks.com/topic/122621-storing-images-in-php-scripts/#findComment-633139 Share on other sites More sharing options...
rarebit Posted September 3, 2008 Share Posted September 3, 2008 you'll need to encode binary data into base64 to store in a plain old string! Link to comment https://forums.phpfreaks.com/topic/122621-storing-images-in-php-scripts/#findComment-633143 Share on other sites More sharing options...
flyhoney Posted September 3, 2008 Author Share Posted September 3, 2008 Just figured that out Thanks for the help guys, works perfectly! Link to comment https://forums.phpfreaks.com/topic/122621-storing-images-in-php-scripts/#findComment-633146 Share on other sites More sharing options...
PFMaBiSmAd Posted September 3, 2008 Share Posted September 3, 2008 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. Link to comment https://forums.phpfreaks.com/topic/122621-storing-images-in-php-scripts/#findComment-633193 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.