Page 1 of 1

displaying image issue

Posted: Fri Feb 23, 2007 11:49 pm
by skyrise85
I have a file uploaded to my webserver, and stored as a blob in my database. I want to take the image, make it smaller, then display. Below is the best code I've found to accomplish the task but it doesn't work because the include statement always triggers the error:

Warning: Cannot modify header information - headers already sent by...(*my include statement for my database stuff*)

Does anyone have a work around?

Code: Select all

$file = 'http://www.***.net/images/71_1.jpg';

//This will set our output to 45% of the original size
$size = 0.45;

// This sets it to a .jpg, but you can change this to png or gif
header('Content-type: image/jpeg'); //THIS IS WHAT IS CAUSING THE ISSUE

// Setting the resize parameters
list($width, $height) = getimagesize($file);
$modwidth = $width * $size;
$modheight = $height * $size;

// Resizing the Image
$tn = imagecreatetruecolor($modwidth, $modheight);
$image = imagecreatefromjpeg($file);
print_r($image);
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);

// Outputting a .jpg, you can make this gif or png if you want
//notice we set the quality (third value) to 100
imagejpeg($tn, null, 100);
thank you for looking, and any help!

Posted: Fri Feb 23, 2007 11:57 pm
by feyd
Remove all output from your scripts prior to the header() call. Anything: white-space, html, extra carriage returns. PHP says exactly where output started (the last line of the first offending output that is.)

As a side note, remotely requesting an image is often not recommended (especially for resizing without storing it.) Also note that there are very few times when it can be recommended to store entire files in a database (as opposed to the file system.)

Posted: Sat Feb 24, 2007 12:08 am
by skyrise85
So you're saying that I could move this line to the the top of my code (by my include statement) and it should work:

Code: Select all

header('Content-type: image/jpeg');
From a methodical standpoint how would you suggest storing/displaying/resizing an image a user uploads?

Posted: Sat Feb 24, 2007 10:23 am
by feyd
skyrise85 wrote:So you're saying that I could move this line to the the top of my code (by my include statement) and it should work
Potentially. It all depends on where your output started. Even then, if you still have output other than the image data, the image will be corrupted.
skyrise85 wrote:From a methodical standpoint how would you suggest storing/displaying/resizing an image a user uploads?
Using the file system. A URL is a remote request. If the sizes you resize these images to are fairly static (the same size a lot) you should probably store the resized image. You can either do the resize during the upload process or you can do it on demand via checking if the resized version is saved. If not, resize and save it for future use. The first request for the resized image will be slower this way, but subsequent requests will be quite fast.

multiple images with text output

Posted: Sat Feb 24, 2007 11:18 am
by skyrise85
Thank you, I think I have a better understanding now. With this method, if it is impossible to output anything other than the image, how does one go about outputting multiple images within a table?

My ultimate goal here is to have a lengthy table with one column displaying a thumbnail of the original image (static size) that can be clicked on to open a "_blank" which displays the full size image. All other columns within the row have text in them, except one -- checkbox.

Posted: Sat Feb 24, 2007 11:22 am
by feyd
Separate <img> tags will make separate requests for each file. Your script would be the file they are requesting with information to tell the script which image they are wanting for that particular tag.

Posted: Sat Feb 24, 2007 12:11 pm
by skyrise85
You'll have to bare with me. This is my first PHP project.

You've lost me with your last reply. As versed as I am w/coding, I'm much better at learning from examples.
Do you mind helping w/my code?

Code: Select all

include("dbrhino.inc.php");
mysql_connect($hostname,$username,$password);
@mysql_select_db($database) or die( "Unable to select database"); 

		//****ABOVE this loop I have output for the table column labels and a sql statement for $numresultsK****
                while ($i < $numrows ) 
		{ //start while-4
			$row = mysql_fetch_assoc($numresultsK);
			$item = $row['itemNum'];
			$description = $row['desc'];
			$size = $row['size'];
			$inches = $row['inches'];
			$unit = $row['unit'];
			$unitP = $row['unitPerBox'];
			$color = $row['color'];
			$priceP = $row['pricePerBox'];
			$optional = $row['optional'];
			$plat = $row['plat'];
	
                        echo "<tr>
			<td></blockquote><input type=\"checkbox\" name=\"sBox[]\" value=\"$itemID\"/></td>
			<td> Image will<br>go here</td>
			<td>$item</td>
			<td>$description</td>
			<td>$size</td>
			<td>$inches</td>
			<td>$unit</td>
			<td>$unitP</td>
			<td>$color</td>
			<td>$priceP</td>
			<td>$plat</td>
			<td>$optional</td>
			</tr>";
                      i++
                     }

Posted: Sat Feb 24, 2007 2:09 pm
by feyd
I don't see anything in your code that is about images.

The rough example is

Code: Select all

<img src="imageScript.php?id=1234" />
Where imageScript.php is the script that does the actual fetch for the image referenced by $_GET['id']. Its sole purpose would be to retrieve the image as if it was an image, nothing more is required of it.