[NOTE Aug 5, 2014] I wrote this in 2010 and took it offline between 2012 and 2014, but it is now back up. Since then, similar ideas have cropped up elsewhere and are worth consulting. Promising options include entropy-based methods (reddit, google search) and a commercial service (cropp.me).
Opticrop is a PHP script I wrote to crop a thumbnail of a specific width and height from a full-sized image.
Unlike most cropping routines out there, Opticrop uses edge-detection to find the most “interesting” part of the image to crop, so you won’t get a useless thumbnail just because the top-left corner of your image happened to be a big patch of featureless sky. This post is an overview of usage and implementation. For a more general discussion, see my post on the methods behind the script. Also check out the live demo of Opticrop, with a slick jQuery interface. (Sorry–this is down right now. Back up soon.) You can also get the Opticrop code on Github.
To use the Opticrop image-cropping script, download opticrop.php and save it into its own folder on your webserver. You’ll need ImageMagick and the Imagick PHP extension installed. If all goes well, calling the script by its URL with the following arguments should give you an image in response.
- src – The path to the image to be cropped. This must be on the same machine as the script, but you can provide its path in URL form, as a relative path to the directory the script is in, or as an absolute path on the webserver’s document root (i.e. preceded by a /)
- w – The width of the thumbnail you want, in pixels.
- h – The height of the thumbnail you want, in pixels.
- gamma (optional) – A number between 0 and 1, for tuning the “tightness” of the crop. Default is 0.2. See discussion.
- format (optional) – Either
json. If set to
json, script will return a JSON-encoded object containing the height, width, and x and y coordinates of the top-left corner of the optimal crop region in the full-sized image.
- cache (optional) – Either
refresh. Set to
noto disable caching temporarily for current request, and
refreshto update the cached image for the given set of parameters.
- debug (optional) – Either 0 (default) or 1. If set to 1, returns a page with debugging info rather than an image.
For example, to run my copy of the script on a test image on my server, go to:
This will display some debug output, and the result image (a 100×100 thumbnail) at the very end. Remove the
debug=1 from the URL to get just the result image.
Explanation of script to come. Until then, you can see the source at GitHub.