0

I have some upload form for images. I want to crop images automatically. And on submit save cropped images.

<input type="file" id="file" name="files[]" multiple/>

I want to crop image: example 800x600, but from center of image, not from corners.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Nebojsa Sapic
  • 9,285
  • 1
  • 22
  • 23
  • You want the crop of images to occur on the browser or the web server? – Tasos K. Sep 09 '14 at 13:19
  • @TasosK. I want while image uploading, at the same time to be cropped and saved. Like all online websites for uploading photo but with croping them – Nebojsa Sapic Sep 09 '14 at 13:23
  • If you know `python` and could enable `cgi-scripting` on your server, it would be a lot more easier. – Weafs.py Sep 09 '14 at 13:24
  • @gandaliter it isn't duplicate, first read all. I want to crop image from center, this isn't same questions – Nebojsa Sapic Sep 09 '14 at 13:24
  • @chipChocolate.py I don't know python, I just want some solutions for solving my problem, thanks – Nebojsa Sapic Sep 09 '14 at 13:26
  • So the actual cropping will occur on the web server, but how it will be cropped it will determined by the user in the browser. Right? If yes, take a look to this plugin http://odyniec.net/projects/imgareaselect/ – Tasos K. Sep 09 '14 at 13:30
  • @TasosK. He specified about cropping them **automatically**, from the center. The only thing that may vary is the size. – Traian Tatic Sep 09 '14 at 13:40

1 Answers1

1

Just upload the file normally. Then, this should do the trick:

$filename = "LINK TO IMAGE";

// Get dimensions of the coriginal image
list($width, $height) = getimagesize($filename);

// Resample the image
$canvas = imagecreatetruecolor('800', '600');
$current_image = imagecreatefromjpeg($filename);
imagecopy($canvas, $current_image, 0, 0, $width/2, $height/2, '800', '600');
imagejpeg($canvas, $filename.'_cropped.jpg', 100);
chmod($filename.'_cropped.jpg', 0644);
unlink($filename);

I did not test the above code. In case of errors please add a comment and I'll help.

Traian Tatic
  • 681
  • 5
  • 18
  • Thanks for the answer, but this $filename is when i already have uploaded image, i want before picture is uploaded to do this trick, but don't know how :/ – Nebojsa Sapic Sep 09 '14 at 13:40
  • @shapic94 Doing it before upload will require javascript or other client side language. Just upload it, resize it and delete the original (the `unlink($filename);` deletes the original). If you consider using PHP, having it on the server in the `/tmp` folder or another folder is the same thing, so just do like I suggested – Traian Tatic Sep 09 '14 at 13:44
  • Thanks one more time, there is one new question. I working on project where user can sell their stuff. like ebay. And when they upload picture, to save original picture or to crop it? Because after on ads view( all pictures to be same width height). Is it better to crop pic on upload, or to crop it when they looking picture. If you understand what i want to tell – Nebojsa Sapic Sep 09 '14 at 15:15