Re: Retrieving Image Location in PHP from MySQL

From: Date: Mon, 09 Mar 2009 14:44:59 +0000
Subject: Re: Retrieving Image Location in PHP from MySQL
References: 1 2 3 4 5 6 7 8 9  Groups: php.general 
Request: Send a blank email to php-general+get-289661@lists.php.net to get a copy of this message
On Mon, Mar 9, 2009 at 9:32 AM, Sashikanth Gurram <sashi34u@vt.edu> wrote: >> On Mon, Mar 9, 2009 at 1:29 AM, Sashikanth Gurram <sashi34u@vt.edu> 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. >>> >>> <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')); >>>> >> >> 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" /> Somehow, you managed to completely ignore this line while reading my reply. >> ... >> </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. > > Yes, the problem was solved, but It did not work fine when I used the same > code in my larger file. Now it makes sense. Let me just repeat what you have > said just to make sure that I did not misread you. > So you say that the solution to this problem is to create another php file > with the image fetching header and just write an img tag <img > src="myimagescript.php?id=1234" /> in my original php file (with the html > tags). This is what I have understood. > Regarding the point you have mentioned ( If you set the content type using > header() to "image/jpeg", do not use HTML tags to display your image!), I > definitely need the HTML tags, because this application works based on the > user input. So unless there is not input through a html form, it wont work. Read my reply a bit more carefully. If you have a script whose purpose is to output an image (not a link to an image, not the tag for an image, but the actual data contained in the image file itself) then you do not--IN THAT PARTICULAR SCRIPT--use HTML tags. Why? Because they are not binary data, and they are not a part of the image! If you want to display this image IN A WEB PAGE, A SCRIPT THAT IS SEPARATE FROM YOUR IMAGE SCRIPT, then you would be just fine referencing the image with <img> tags. It doesn't make any difference if this web page is a form, has AJAX, chews gum and walks down the street at the same time, etc. -- // Todd

« previous php.general (#289661) next »