TGWSE_GY Posted April 30, 2009 Share Posted April 30, 2009 Rather than the script displaying the image, the browser for some odd reason is trying to download the php script page. Here is the code: $row = mysql_fetch_assoc($GetPhotos); $Type = $row['ImageType']; $Search = "image/"; $Replacement = ""; $EncodedImage = $row['Image']; $Decoded = base64_decode($EncodedImage); $CompiledImage = imagecreatefromstring($Decoded); header('Content-Type:' . ' $Type'); $FileFormat = str_replace($Search, $Replacement, $Type); $FileFormat = "image" . $FileFormat; echo $FileFormat($CompiledImage); imagedestroy($CompiledImage); die(); Any Ideas guys, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/156278-solved-script-not-displaying-image-rather-the-browser-tries-to-download-the-script/ Share on other sites More sharing options...
Adam Posted April 30, 2009 Share Posted April 30, 2009 you don't "echo" out the image. Replace: header('Content-Type:' . ' $Type'); $FileFormat = str_replace($Search, $Replacement, $Type); $FileFormat = "image" . $FileFormat; echo $FileFormat($CompiledImage); imagedestroy($CompiledImage); With: header('Content-Type:' . ' $Type'); preg_match('/(png|jpeg|gif|bmp)/', $Type, $match); $function_name = 'image' . $match[1]; $function_name($im); imagedestroy($im); Not tested. An it's a little messy, there's a good possibility I'm making it more complex than it needs to be! Quote Link to comment https://forums.phpfreaks.com/topic/156278-solved-script-not-displaying-image-rather-the-browser-tries-to-download-the-script/#findComment-822730 Share on other sites More sharing options...
PFMaBiSmAd Posted April 30, 2009 Share Posted April 30, 2009 Using GD functions to create an image from and existing image so that you can output it is silly and is a waste of time and a huge waste of server resources. You just output the correct content type header followed by the base64 decoded image data. Quote Link to comment https://forums.phpfreaks.com/topic/156278-solved-script-not-displaying-image-rather-the-browser-tries-to-download-the-script/#findComment-822745 Share on other sites More sharing options...
TGWSE_GY Posted April 30, 2009 Author Share Posted April 30, 2009 Thanks MrAdam and PFMaBiSmAd, I chose to just follow the header with the base64 decoded image, However the browser rather than echoing the image is still trying to download my script. Why is this? Any Ideas? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/156278-solved-script-not-displaying-image-rather-the-browser-tries-to-download-the-script/#findComment-822775 Share on other sites More sharing options...
PFMaBiSmAd Posted April 30, 2009 Share Posted April 30, 2009 Either the page is outputting something else or $row['ImageType'] does not contain what you think it does. What is the full code for the page, temporarily comment out the header() statement and echo the $Type variable so that you can see exactly what is being output to the browser. Quote Link to comment https://forums.phpfreaks.com/topic/156278-solved-script-not-displaying-image-rather-the-browser-tries-to-download-the-script/#findComment-822784 Share on other sites More sharing options...
TGWSE_GY Posted April 30, 2009 Author Share Posted April 30, 2009 $Type echos image/jpeg so that is right, but what else could be causing the issue, here is my code: session_start(); if (isset($_SESSION['Usr'])) { // Get database connection info include('config.php'); // Connect to Database $con = mysql_connect($Host, $Login, $Pass); mysql_select_db("login_ums", $con); // Get the username from the session $varUser = $_SESSION['Usr']; //Get the users CustID $GetCustID = mysql_query("SELECT CustID FROM login WHERE Usr = '$varUser'"); $CustIDArray = mysql_fetch_assoc($GetCustID); $CustID = $CustIDArray[CustID]; //Get users Photos $GetPhotos = mysql_query("SELECT * FROM profile_user_images WHERE CustID = '$CustID'"); if (mysql_num_rows($GetPhotos) != 1) { die(); } //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); //Initialize the counter this will be used for outputting multiple images to the browser in a do while loop $I = 1; //Initialize and process the data in $FetchArray while displaying the results to the browser for viewer interaction $row = mysql_fetch_assoc($GetPhotos); $Type = $row['ImageType']; $Search = "image/"; $Replacement = ""; $EncodedImage = $row['Image']; $Decoded = base64_decode($EncodedImage); $CompiledImage = imagecreatefromstring($Decoded); header('Content-Type:' . ' $Type'); $FileFormat = str_replace($Search, $Replacement, $Type); $FileFormat = "image" . $FileFormat; echo $FileFormat($CompiledImage); imagedestroy($CompiledImage); die(); } Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/156278-solved-script-not-displaying-image-rather-the-browser-tries-to-download-the-script/#findComment-822813 Share on other sites More sharing options...
PFMaBiSmAd Posted April 30, 2009 Share Posted April 30, 2009 You can only output the header and data for one image at a time. Any use of $I or any loop you had planned won't work. After your query that gets the type and data, all you need is - $row = mysql_fetch_assoc($GetPhotos); header('Content-type: ' . $row['ImageType']); echo base64_decode($row['Image']); In the code you had, $Type was inside of single-quotes in the header() statement. Php variables are not parsed inside of single-quotes. Quote Link to comment https://forums.phpfreaks.com/topic/156278-solved-script-not-displaying-image-rather-the-browser-tries-to-download-the-script/#findComment-822903 Share on other sites More sharing options...
TGWSE_GY Posted April 30, 2009 Author Share Posted April 30, 2009 Thanks PFMaBiSmAd, However there may be limitations with the header, but there is always a way to do something, you just have to figure out how to do it. I have found programming to only be limited by ones thinking and how they think around the problems that they are faced with to reach the best solution. Thanks again for the help. Quote Link to comment https://forums.phpfreaks.com/topic/156278-solved-script-not-displaying-image-rather-the-browser-tries-to-download-the-script/#findComment-823024 Share on other sites More sharing options...
TGWSE_GY Posted May 1, 2009 Author Share Posted May 1, 2009 Okay this is not working for some reason I have done what was suggested as you can see here in this code on the last 3 lines however I just get a blank page. Any Ideas. Thanks Guys <?php // Get database connection info include('config.php'); // Connect to Database $con = mysql_connect($Host, $Login, $Pass); mysql_select_db("login_ums", $con); // Get the username from the session $varUser = $_SESSION['Usr']; //Get the users CustID $GetCustID = mysql_query("SELECT CustID FROM login WHERE Usr = '$varUser'"); $CustIDArray = mysql_fetch_assoc($GetCustID); $CustID = $CustIDArray[CustID]; //Get users Photos $GetPhotos = mysql_query("SELECT * FROM profile_user_images WHERE CustID = '$CustID'"); if (mysql_num_rows($GetPhotos) != 1) { die(); } $row = mysql_fetch_assoc($GetPhotos); header('Content-type: ' . $row['ImageType']); echo base64_decode($row['Image']); ?> Quote Link to comment https://forums.phpfreaks.com/topic/156278-solved-script-not-displaying-image-rather-the-browser-tries-to-download-the-script/#findComment-823431 Share on other sites More sharing options...
PFMaBiSmAd Posted May 1, 2009 Share Posted May 1, 2009 It's going to be really hard for a $_SESSION variable to exist without a session_start() statement. Your previously posted code - Posted on: Yesterday at 10:58:07 AM had one. Quote Link to comment https://forums.phpfreaks.com/topic/156278-solved-script-not-displaying-image-rather-the-browser-tries-to-download-the-script/#findComment-823440 Share on other sites More sharing options...
TGWSE_GY Posted May 1, 2009 Author Share Posted May 1, 2009 Session start is there sorry, missed it when copying. I will repost the code. session_start(); // Get database connection info include('config.php'); // Connect to Database $con = mysql_connect($Host, $Login, $Pass); mysql_select_db("login_ums", $con); // Get the username from the session $varUser = $_SESSION['Usr']; //Get the users CustID $GetCustID = mysql_query("SELECT CustID FROM login WHERE Usr = '$varUser'"); $CustIDArray = mysql_fetch_assoc($GetCustID); $CustID = $CustIDArray[CustID]; //Get users Photos $GetPhotos = mysql_query("SELECT * FROM profile_user_images WHERE CustID = '$CustID'"); if (mysql_num_rows($GetPhotos) != 1) { die(); } $row = mysql_fetch_assoc($GetPhotos); header('Content-type: ' . $row['ImageType']); echo base64_decode($row['Image']); ?> Any ideas? ??? ??? ??? ??? ??? ??? ??? ??? Quote Link to comment https://forums.phpfreaks.com/topic/156278-solved-script-not-displaying-image-rather-the-browser-tries-to-download-the-script/#findComment-823446 Share on other sites More sharing options...
PFMaBiSmAd Posted May 1, 2009 Share Posted May 1, 2009 So, what have you done to troubleshoot what it is doing? We don't have access to your database, nor do we know what you are supplying as input to your script, so the only one here that can troubleshoot what it is doing it you. Is the first query working and returning what you expect in $CustID? Is the second query working and what value does mysql_num_rows($GetPhotos) have? Quote Link to comment https://forums.phpfreaks.com/topic/156278-solved-script-not-displaying-image-rather-the-browser-tries-to-download-the-script/#findComment-823451 Share on other sites More sharing options...
Adam Posted May 1, 2009 Share Posted May 1, 2009 however I just get a blank page. Would that not suggest then that possibily the die() function is being called? Try switching it for: die("mysql error: " . mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/156278-solved-script-not-displaying-image-rather-the-browser-tries-to-download-the-script/#findComment-823464 Share on other sites More sharing options...
TGWSE_GY Posted May 1, 2009 Author Share Posted May 1, 2009 Okay it is echoing the customer ID 556118453 and mysql_num_rows echos 2, I figured out that there was a problem in the if statement, and I fixed that. But now it is echoing the script location: http://www.thegayestcommunityever.com/dev/php/includes/content/members_home/user_images/view_images.php to the browser. Here is the revised code: <?php session_start(); if (isset($_SESSION['Usr'])) { // Get database connection info include('config.php'); // Connect to Database $con = mysql_connect($Host, $Login, $Pass); mysql_select_db("login_ums", $con); // Get the username from the session $varUser = $_SESSION['Usr']; //Get the users CustID $GetCustID = mysql_query("SELECT CustID FROM login WHERE Usr = '$varUser'"); $CustIDArray = mysql_fetch_assoc($GetCustID); echo $CustID = $CustIDArray[CustID]; //Get users Photos $GetPhotos = mysql_query("SELECT * FROM profile_user_images WHERE CustID = '$CustID'"); echo mysql_num_rows($GetPhotos); if (mysql_num_rows($GetPhotos) == 0) { die(); } $row = mysql_fetch_assoc($GetPhotos); header('Content-type: ' . $row['ImageType']); echo base64_decode($row['Image']); ?> I think it has something to do with the header I am unsure, because I have also echoed the base64_decoded and it echos the proper material. Thanks Guys Quote Link to comment https://forums.phpfreaks.com/topic/156278-solved-script-not-displaying-image-rather-the-browser-tries-to-download-the-script/#findComment-823470 Share on other sites More sharing options...
Adam Posted May 1, 2009 Share Posted May 1, 2009 What does "$row['ImageType']" return? Quote Link to comment https://forums.phpfreaks.com/topic/156278-solved-script-not-displaying-image-rather-the-browser-tries-to-download-the-script/#findComment-823532 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.