Web Hosting Talk







View Full Version : imagecopyresampled


=-D
03-31-2005, 08:42 PM
I dont know why this peice of code is not working. I've checked out the docs for the function imagecopyresampled at http://www.php.net/imagecopyresampled (http:/www.php.net/imagecopyresampled) but it doesnt function to how i want it.

I't supposed to resize an image down to a specific size and crop it in the middle.

if ($_FILES['image']['error'] == 0) {
move_uploaded_file($_FILES['image']['tmp_name'], $image_file);
}

$image = @imagecreatefromjpeg($image_file);
if ($image)
{
$desired_width = 67;
$desired_height = 62;

// need to crop to dimensions similar to 67x62
$factor_x = imagesx($image) / $desired_width;
$factor_y = imagesy($image) / $desired_height;

// 500/67 = 7.46
// 400/62 = 6.45
// new x before resize = 432

if ($factor_x > $factor_y) // move page to center (x axis)
{
$width = floor($factor_y * $desired_width);
$height = imagesy($image);
$start_x = (imagesx($image)-$width) / 2;
$start_y = 0;
}
elseif ($factor_x < $factor_y) // move page to center (y axis)
{
$width = imagesx($image);
$height = floor($factor_x * $desired_height);
$start_x = 0;
$start_y = (imagesy($image)-$height) / 2;
}
elseif ($factor_x == $factor_y)
{
$width = imagesx($image);
$height = imagesy($image);
$start_x = 0;
$start_y = 0;
}
print "width=$width<br>\n";
print "desired_width=$desired_width<br>\n";
print "desired_height=$desired_height<br>\n";
print "imagesx=". imagesx($image) ."<br>\n";
print "imagesy=". imagesy($image) ."<br>\n";
print "factor_x=$factor_x<br>\n";
print "factor_y=$factor_y<br>\n";
print "width=$width<br>\n";
print "height=$height<br>\n";
print "start_x=$start_x<br>\n";
print "start_y=$start_y<br>\n";

$thumb = imagecreatetruecolor($desired_width, $desired_height);
if ($thumb)
{
print imagecopyresampled($thumb, $image, 0,0, $start_x,$start_y, $desired_width,$desired_height, $width,$height);

$image_file = '../images/members/' . $_POST['member_id'] . '.jpg';
imagejpeg($image, $image_file, 80);
}
}

This is the output:
width=243
desired_width=67
desired_height=62
imagesx=300
imagesy=225
factor_x=4.4776119402985
factor_y=3.6290322580645
width=243
height=225
start_x=28.5
start_y=0
1

mitchlrm
04-04-2005, 11:04 AM
Imagecopyresampled only allows integer parameters. The start_x value of 28.5 is likely causing a problem. You want to use intval() to convert any calculated numbers into an integer.

Besides...you said it isn't working as you expected. What exactly are you getting as a result?

Webxplosion
04-04-2005, 05:35 PM
Uhm, maybe it's too late for me to think straight, but why in gods name are you PRINTING the imagecopyresampled?

Also, you might wanna add the appropriate header:

header("Content-type: image/jpeg");

Brad457
04-04-2005, 07:39 PM
He's right you shouldn't be printing it, you should be doing imagecreatepng(), imagecreatejpeg() or imagecreategif()

=-D
04-08-2005, 08:03 PM
Originally posted by mitchlrm
Imagecopyresampled only allows integer parameters. The start_x value of 28.5 is likely causing a problem. You want to use intval() to convert any calculated numbers into an integer.

Besides...you said it isn't working as you expected. What exactly are you getting as a result?

thanks for reading. but read the code mate. it outputs it to a file so no need for the content-type header.

anyways, imagecopyresampled has a bug in it so it doesnt properly work.. there is a report on bugs.php.net somewhere i saw

............

its cool found a way

Area09
04-08-2005, 11:19 PM
Even if the files is saved to the server, you can still print/show it.