Opticrop: Usage and Implementation

[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.

Usage

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 img (default) or 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 yes (default), no, or refresh. Set to no to disable caching temporarily for current request, and refresh to 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:

http://jueseph.com/scripts/opticrop/opticrop.php?src=test3.jpg&w=100&h=100&debug=1&cache=no

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.

Implementation

Explanation of script to come. Until then, you can see the source at GitHub.

Advertisements
This entry was posted in Not biology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s