Re: Retrieving Image Location in PHP from MySQL

From: Date: Mon, 09 Mar 2009 06:29:19 +0000
Subject: Re: Retrieving Image Location in PHP from MySQL
References: 1 2 3 4 5 6  Groups: php.general 
Request: Send a blank email to php-general+get-289635@lists.php.net to get a copy of this message
Hi Nathan, Thanks a lot for the suggestion. It is working fine for an example code I have used to test it. The code I have written after your suggestion is as follows. <?php $location="C:\wamp\bin\apache\apache2.2.8\htdocs\Bldgs_lots\SQUIRES.jpg"; header('Content-Type: image/jpeg'); imagejpeg(imagecreatefromjpeg($location)); ?> The above code is yielding me a picture. Now, When I tried to use it in my original code it is not giving me the image. Instead it is giving me this warning *Warning*: Cannot modify header information - headers already sent by (output started at C:\wamp\www\mysqli.php:65) in *C:\wamp\www\mysqli.php* on line *221* and a lot binary characters again instead of the image. I know something about this warning that we should not output anything to the browser before the header. But I cannot do that, since I need the user input using a html form and I use the input to fetch the data and dispaly it in a table along with the image. I am posting the whole code below. Please do let me know about any changes, corrections or modifications that are needed. The image display code is towards the last. Thanks, Sashi <html> <body> <form action="mysqli.php" method="post"> <br> <div align="center"> Building Name:<select name="name"> <option value=""> Select a Building</option>
     <option value="Williams Hall">Williams Hall</option>
     <option value="Women's Softball Field">Women's Softball Field</option>
     <option value="Wright House">Wright House</option>
</select> </div>
                   <input type="submit" />
</form> <?php // Connects to your Database $host="*********"; $user="*********"; $password="*********"; $dbname="*********"; $cxn=mysqli_connect($host, $user, $password, $dbname) ; if (!$cxn=mysqli_connect($host, $user, $password, $dbname)) {
       $error=mysqli_error($cxn);
       echo "$error";
       die();
} else {
       echo "Connection established successfully";
} //Define the variables for Day and Month $Today=date("l"); $Month=date("F"); $build=$_POST["name"]; $low_speed=2.5; $high_speed=4; $hour=date("G"); $minute=date("i"); if ($minute>=00 && $minute<=14) {
       $minute=00;
} elseif ($minute>=15 && $minute<=29) {
       $minute=15;
} elseif ($minute>=30 && $minute<=44) {
       $minute=30;
} else {
       $minute=45;
} $times="10:$minute"; $sql="SELECT buildingname, parking_lot_name, empty_spaces, distance, round(distance/($low_speed*60),1) AS low_time, round(distance/($high_speed*60),1) AS high_time, Location FROM buildings, buildings_lots, parkinglots, occupancy2, Image where (buildings.buildingcode=occupancy2.building AND buildings.buildingcode=buildings_lots.building_code AND parkinglots.parking_lot_code=buildings_lots.parking_lot_code AND parkinglots.parking_lot_code=occupancy2.parking_lot AND Buildings.BuildingCode=Image.BuildingCode) AND buildingname='$build' AND month='$Month' AND day='Monday' AND Time='$times'"; $data = mysqli_query($cxn,$sql); if (!$data=mysqli_query($cxn,$sql)) {
       $error=mysqli_error($cxn);
       echo "$error";
       die();
} else {
       echo "<br>";
       echo "Query sent successfully";
} echo "<br>"; echo "<h1> PARKING LOT INFORMATION <h1>"; echo "<table border='1' cellspacing='5' cellpadding='2'>"; echo "<tr>\n
       <th>Building</th>\n
       <th>Parking Lot</th>\n
       <th>Estimated Number of Empty Spaces</th>\n
       <th>Distance (Feet)</th>\n
       <th>Estimated walking time to the building</th>\n
</tr>\n"; while ($row=mysqli_fetch_array($data)) {
       extract($row);
$building = $row[0];
     $parking_lot = $row[1];
     $Number_of_Empty_Spaces = $row[2];
     $Distance = $row[3];
     $time_l = $row[4];
     $time_h=$row[5];
     $location=$row[6];   echo "<tr>\n
             <td>$building</td>\n
             <td>$parking_lot</td>\n               <td>$Number_of_Empty_Spaces</td>\n
             <td>$Distance</td>\n
             <td>$time_h - $time_l mins</td>\n
<td>$location</td>\n
             </tr>\n";
} echo "</table>\n"; header('Content-Type: image/jpeg'); imagejpeg(imagecreatefromjpeg("$location")); ?> </body> </html> Nathan Nobbe wrote:
On Sat, Mar 7, 2009 at 9:36 PM, Sashikanth Gurram <sashi34u@vt.edu> wrote:
The thing is, I do not have a website or a place where I am storing my images. I am storing the images on my local PC in folder as mentioned in my earlier post. I have tried using only the else { header('Content-Type: image/jpeg'); imagejpeg($img); imagedestroy($img); } Which you have suggested. This is the piece of code which is returning all the binary character stuff. The code is definitely fetching the image. But it is not able to deliver to the browser in the form of an image. I am trying to read the image file as binary, evident from the FILE_BINARY command. So may be, that is causing the image to be displayed as binary. Is there any way we can convert the binary content to jpeg/jpg content again. This is my guess after having gone through the code again.
    
i spent 2 minutes working on it and read this in the manual, the first param of imagejpeg(), An image resource, returned by one of the image creation functions, such as imagecreatetruecolor()<http://us.php.net/manual/en/function.imagecreatetruecolor.php> . so, then i tossed this together, http://nathan.moxune.com/echoImage.php <?php header('Content-Type: image/jpeg'); imagejpeg(imagecreatefromjpeg('./phpLogo.jpeg')); -nathan
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Sashikanth Gurram Graduate Research Assistant Department of Civil and Environmental Engineering Virginia Tech Blacksburg, VA 24060, USA

« previous php.general (#289635) next »