Jump to content

[SOLVED] Script not displaying image, rather the browser tries to download the script!


TGWSE_GY

Recommended Posts

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.  8)

Link to comment
Share on other sites

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!

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

$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  8)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 ;D

 

<?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']);
?>

Link to comment
Share on other sites

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?  ??? ??? ??? ??? ??? ??? ??? ???

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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());

Link to comment
Share on other sites

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  :P

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.