Page 1 of 1

Resize picture [SOLVED]

Posted: Thu Jan 04, 2007 1:28 pm
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?

Posted: Thu Jan 04, 2007 1:34 pm
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.)

Posted: Thu Jan 04, 2007 1:43 pm
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?

Posted: Thu Jan 04, 2007 1:50 pm
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.

Posted: Thu Jan 04, 2007 2:31 pm
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" />

Posted: Thu Jan 04, 2007 2:46 pm
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

Posted: Thu Jan 04, 2007 2:54 pm
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

Posted: Thu Jan 04, 2007 3:01 pm
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?

Posted: Thu Jan 04, 2007 3:01 pm
by feyd
image.php is not told what $image is.

Posted: Thu Jan 04, 2007 3:12 pm
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 :(

Posted: Thu Jan 04, 2007 3:18 pm
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

Posted: Thu Jan 04, 2007 3:23 pm
by evolozik
Thx everyone for helping, i really appreciate :D
was able to figure out my error :)
thx again :D