Working Inside with Image/JPEG

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
SheDesigns
Forum Commoner
Posts: 42
Joined: Tue Nov 18, 2008 9:51 am
Location: Buffalo, NY

Working Inside with Image/JPEG

Post by SheDesigns »

I'm trying to resize an image, right on the page, but the output is just gibberish of numbers and characters. I realize this is because it doesn't know the type in Image/JPEG. This is within an already created webpage, so I can't simply put it at the top - it doesn't do a damn thing, and keeps the page from loading correctly.

If I put it too early, it says, "Headers already sent", with a parse error. If I put it in where it needs to go, it returns a bunch of random characters. HELP!!

Code: Select all

$query1 = "select * from propertyimages where property = '$propid' and `order` = '1' ";
$result1 = mysql_query($query1);
$count1 = mysql_num_rows($result1);
if($count1 == 0)
{ echo "<i>No Photo Available</i><br><br>"; }
elseif($count1 > 0)
{   header("Content-Type: image/jpeg");
$fetching = mysql_fetch_array($result1);
    extract($fetching);
                    $add = "admin/".$imgsrc;
 
                    $width = 100;
                    $height = 100;
                    list($width_orig, $height_orig) = getimagesize($add);
                    $ratio_orig = $width_orig/$height_orig;
                    if ($width/$height > $ratio_orig) {
                       $width = $height*$ratio_orig;
                    } else {
                       $height = $width/$ratio_orig;
                    }
                    $image_p = imagecreatetruecolor($width, $height);
                    $image = imagecreatefromjpeg($add);
                    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
                    imagejpeg($image_p);                
}
//
 
 
echo "</center><br>
<b>$streetaddress</b><br>$citystatezip<br><a href=\"\">See More Information</a></td>";
 $row_count++;
}
 
echo "</table>";
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Working Inside with Image/JPEG

Post by Eran »

Code: Select all

header("Content-Type: image/jpeg");
You are sending headers after output has been sent already, which you can't do. You also can't mix image headers with normal html headers - a script like the one in your original post can only be run from a separate request, meaning a different page or from inside a an image tag src attribute.

You can change it to save the images as physical files instead, and link to those from the script.
Instead of:

Code: Select all

imagejpeg($image_p);
Something like:

Code: Select all

imagejpeg($image_p,$filename); //$filename holds the complete path to the image you want to create
echo '<img src="' . $filename . '" />';
Post Reply