=-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
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
