Re: Retrieving Image Location in PHP from MySQL

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Mar 9, 2009 at 1:29 AM, Sashikanth Gurram <sashi34u@xxxxxx> wrote:
> 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@xxxxxx> 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'));

I see this one has already been solved, but I felt like I needed to
point this out: If you set the content type using header() to
"image/jpeg", do not use HTML tags to display your image! The expected
output is the actual binary data from the image, not an <img
src="asdf" /> tag. The OUTPUT of this script could be used in an IMG
tag elsewhere, perhaps, like so:

<html>
...
<img src="myimagescript.php?id=1234" />
...
</html>

Where "myimagescript.php" uses the Content-type header() and outputs
the binary JPEG data. If you were using PHP to build a javascript file
to be included, you wouldn't wrap it in <script> tags, either. Same
principle applies here.

HTH,


-- 
// Todd

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux