TGWSE_GY Posted April 29, 2009 Share Posted April 29, 2009 Hi Guys, In the following code I have successfully pulled the base64_encoded string from my database, decoded it and trying to pass it to header to recompile the image like sloth456 suggested in one of my previous topics. However all it is giving me is the decoded image string and asking me if I want to download view_images.php which is the script that is running the following code, any ideas? Here is the code: <?php $GetPhotos = mysql_query("SELECT * FROM profile_user_images WHERE CustID = '$CustID'"); if (mysql_num_rows($GetPhotos) > 0) { //Fetching the db records and putting them into an array //$FecthArray = mysql_fetch_array($GetPhotos); //Get number of records in $FetchArray $TotalRecords = mysql_num_rows($GetPhotos); echo $TotalRecords; //Initialize the counter $I = 1; //Initialize and process the data in $FetchArray while displaying the results to the browser for viewer interaction while ($row = mysql_fetch_assoc($GetPhotos)) { $Type = $row['ImageType']; header('Content-Type: $Type'); $EncodedImage = $row['Image']; $Decoded = base64_decode($EncodedImage); print_r ($Decoded); } die(); ?> Thanks Guys :-\ Link to comment https://forums.phpfreaks.com/topic/156191-solved-recompiling-image-from-stored-base_64-string-in-database/ Share on other sites More sharing options...
gffg4574fghsDSGDGKJYM Posted April 29, 2009 Share Posted April 29, 2009 You can replace this : <?php if (mysql_num_rows($GetPhotos) > 0) With this : <?php if (mysql_num_rows($GetPhotos) != 1) { die(); } The script will ouput 1 image only so don't allow for more or less. You can also remove the while, you can't output more than 1 image each time the script is called. I don't know about print_r() to echo a image but this should work fine : <?php $Type = $row['ImageType']; $EncodedImage = $row['Image']; $Decoded = base64_decode($EncodedImage); $db_img = imagecreatefromstring($Decoded); if ($db_img !== false) { /* Add some if () to see the right content-type/gd function to use for jpg and gif */ header("Content-Type: image/png"); imagepng($db_img); } imagedestroy($db_img); ?> Be sure to remove echo $TotalRecords; and don't echo anything even space or enter in the file or before the <?php or the header() function will fail. Once the script is done you can go to http://web-sniffer.net/ to test your script and see if the HTTP header are ok or if there any error. You won't actually see the image but you can see php error. Link to comment https://forums.phpfreaks.com/topic/156191-solved-recompiling-image-from-stored-base_64-string-in-database/#findComment-822298 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.