Resize picture [SOLVED]

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
evolozik
Forum Newbie
Posts: 14
Joined: Thu Jan 04, 2007 1:20 pm

Resize picture [SOLVED]

Post by evolozik »

hi everyone
i need some help please
i am using PHP5 and i used a tutorial and when i copied the code for Resampling an image proportionally, it worked perfectly
but when i added it in my page it did not work

here is what i did:

Code: Select all

function resizeimage($images)
{
// Retrieving path
$filename = $images;
// Set a maximum height and width
$width = 100;
$height = 100;

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);

$ratio_orig = $width_orig/$height_orig;

if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

// Output
$new=imagejpeg($image_p, null, 100);
return $new;
}
the after some other code, i wrote:

Code: Select all

$img=$newRow['picture'];
echo "<a href=\"test1.php\">resizeimage($img)</a>";
$newRow['picture'] is the path of the image saved in the database

however, the picture doesn't display but it displays for example:
resizeimage(images/d500.jpg)

can someone help me figure what the problem is please?
Last edited by evolozik on Thu Jan 04, 2007 3:38 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

HTML and images cannot be sent in the same stream. You need to output an image tag. This image tag must reference the script that will perform the resize (or the filename and path you saved it as.)
evolozik
Forum Newbie
Posts: 14
Joined: Thu Jan 04, 2007 1:20 pm

Post by evolozik »

hello feyd

i am not saving the picture, it has to display as soon as it has been resized
i tried something and noticed that whenever i inserted some code between this:

Code: Select all

//some code



function resizeimage($images)

{

// Retrieving path

$filename = $images;

// Set a maximum height and width

$width = 100;

$height = 100;



// Get new dimensions

list($width_orig, $height_orig) = getimagesize($filename);



$ratio_orig = $width_orig/$height_orig;



if ($width/$height > $ratio_orig) {

$width = $height*$ratio_orig;

} else {

$height = $width/$ratio_orig;

}



// Resample

$image_p = imagecreatetruecolor($width, $height);

$image = imagecreatefromjpeg($filename);

imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);



// Output

$new=imagejpeg($image_p, null, 100);

}
and this code

Code: Select all

$img=$newRow['picture'];

resizeimage($img)
weird characters appears

but if there is no code in between, the picture appears well

what is wrong?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Time to re-read that tutorial I think.. or maybe a different one.

An IMG tag expects the URL to an image file - not the contents of the file.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

To repeat what feyd said;

Place the function and the function call in a seperate file, lets say image.php. You then call the file in this manner

Code: Select all

<img src="image.php" />
evolozik
Forum Newbie
Posts: 14
Joined: Thu Jan 04, 2007 1:20 pm

Post by evolozik »

i separated the image and the html

i put the image in the image.php and it is as follows:

Code: Select all

// Retrieving path
		$filename = $image;
		// Set a maximum height and width
		$width = 50;
		$height = 50;
			
		// Get new dimensions
		list($width_orig, $height_orig) = getimagesize($filename);
		
		$ratio_orig = $width_orig/$height_orig;
		
		if ($width/$height > $ratio_orig) {
		   $width = $height*$ratio_orig;
		} else {
		   $height = $width/$ratio_orig;
		}
		
		// Resample
		$image_p = imagecreatetruecolor($width, $height);
		$image = imagecreatefromjpeg($filename);
		imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
		
		// Output
		imagejpeg($image_p, null, 100);
then in my other page:

Code: Select all

$image=$newRow['picture'];
global $image;
echo "<a href=\"test.php\"><img src=image.php></a>";
fortunately the weird characters do not appears but my image neither
why can't it display the image?
there is a cross instead
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

try browsing to the image URL and see what you get... I suspect you'll get a bunch of "crap". This can be solved by sending the appropriate header for a jpeg.

Take a look on the php manual page for the header() function for more
evolozik
Forum Newbie
Posts: 14
Joined: Thu Jan 04, 2007 1:20 pm

Post by evolozik »

yes you are right, so i used this header

Code: Select all

header('Content-type: image/jpeg');
but it still doesn't display the picture
there is no garbage but simply a square with a cross

is there another way to solve this?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

image.php is not told what $image is.
evolozik
Forum Newbie
Posts: 14
Joined: Thu Jan 04, 2007 1:20 pm

Post by evolozik »

i changed the code as follows:

image.php

Code: Select all

// Retrieving path
                $filename = $_GET['image'];
                // Set a maximum height and width
                $width = 50;
                $height = 50;
                       
                // Get new dimensions
                list($width_orig, $height_orig) = getimagesize($filename);

		// Content type
		header('Content-type: image/jpeg');
               
                $ratio_orig = $width_orig/$height_orig;
               
                if ($width/$height > $ratio_orig) {
                   $width = $height*$ratio_orig;
                } else {
                   $height = $width/$ratio_orig;
                }
               
                // Resample
                $image_p = imagecreatetruecolor($width, $height);
                $image = imagecreatefromjpeg($filename);
                imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
               
                // Output
                imagejpeg($image_p, null, 100);
and the other code:

Code: Select all

$image=$newRow['picture'];
echo "<a href=\"test.php\"><img src=\"include/image.php?image=$image\" border=0></a>";
but still the image doesn't appear :(
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

check your path... and do all your testing by entering the URL manually into the browser for now (until it works)

hint: you can temporarily disable to "header" function to see errors
evolozik
Forum Newbie
Posts: 14
Joined: Thu Jan 04, 2007 1:20 pm

Post by evolozik »

Thx everyone for helping, i really appreciate :D
was able to figure out my error :)
thx again :D
Post Reply