Reputation: 997
I have an existing image upload script (below) which works fine but I would like to add a crop function to it, so each uploaded photo maintains the right aspect ration, but is cropped to, say, 200 x 200px.
I have looked at other questions on SO relating to this but ideally I'd like to add cropping to my script, rather than implement a whole new one, if that makes sense.
Could anyone help?
Thanks as always.
mkdir("images/$user_id");
$saveto = "images/$user_id/$user_id.jpg";
move_uploaded_file($_FILES['image']['tmp_name'], $saveto);
$typeok = TRUE;
switch($_FILES['image']['type'])
{
case "image/gif": $src = imagecreatefromgif($saveto); break;
case "image/jpeg": // Both regular and progressive jpegs
case "image/pjpeg": $src = imagecreatefromjpeg($saveto); break;
case "image/png": $src = imagecreatefrompng($saveto); break;
default: $typeok = FALSE; break;
}
if ($typeok)
{
list($w, $h) = getimagesize($saveto);
$max = 200;
$tw = $w;
$th = $h;
if ($w > $h && $max < $w)
{
$th = $max / $w * $h;
$tw = $max;
}
elseif ($h > $w && $max < $h)
{
$tw = $max / $h * $w;
$th = $max;
}
elseif ($max < $w)
{
$tw = $th = $max;
}
$tmp = imagecreatetruecolor($tw, $th);
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $tw, $th, $w, $h);
imageconvolution($tmp, array( // Sharpen image
array(-1, -1, -1),
array(-1, 16, -1),
array(-1, -1, -1)
), 8, 0);
imagejpeg($tmp, $saveto);
imagedestroy($tmp);
imagedestroy($src);
}
EDIT: I have found the following script which works fine when its on its own page, however I'm having trouble implementing it either into or after my existing upload script - I get a few 'failed to open stream: No such file or directory' errors - however the path to the image is correct (i've echoed it out to be sure):
$filename = 'images/$user_id/$user_id.jpg';
// Get dimensions of the original image
list($current_width, $current_height) = getimagesize($filename);
// The x and y coordinates on the original image where we
// will begin cropping the image
$left = 25;
$top = 25;
// This will be the final size of the image (e.g. how many pixels
// left and down we will be going)
$crop_width = 200;
$crop_height = 200;
// Resample the image
$canvas = imagecreatetruecolor($crop_width, $crop_height);
$current_image = imagecreatefromjpeg($filename);
imagecopy($canvas, $current_image, 0, 0, $left, $top, $current_width, $current_height);
imagejpeg($canvas, $filename, 100);
Could someone help me put the two together?
Thanks
Upvotes: 0
Views: 2372
Reputation: 997
Thanks to everyone for there responses - I got my script working by changing
$filename = 'images/$user_id/$user_id.jpg';
to
$filename = "images/$user_id/$user_id.jpg";
Upvotes: 0
Reputation: 2928
Take a look on : Gregwar/Image
It's very easy to use, and very efficient.
resize($width, $height, $background): resizes the image, will preserve scale and never enlarge it
scaleResize($width, $height, $background): resizes the image, will preserve scale
forceResize($width, $height, $background): resizes the image, will orce the image to be exactly $width by $height
cropResize($width, $height, $background): resizes the image preserving scale and croping the whitespaces
Upvotes: 1