Smart coordinates

Smart coordinates were implemented to make operations that involve coordinates, dimensions, and positioning easier. They can be useful with any operation that requires coordinates as parameters, like resize, crop, etc.

For the examples below, the $image dimensions are 1000×800.

Numeric coordinates

These coordinates are the simplest of all, they tell how many pixels should the operation use as value.

  1. // resize to 100×50
  2. $resized = $image->resize(100, 50, 'fit');
  3.  
  4. // produces an equal result
  5. $resized = $image->resize('100', "50", 'fit');

Percent string

With percent strings, the values passed refers to the relevant dimension.

  1. // resize to half-of-width x 25%-of-height = 500×200
  2. $resized = $image->resize('50%', '25%', 'fit');

Alignment labels

Use alignment labels (left, center, right, top, middle, bottom) to perform operations aligned to image border. Alignment is supported for operations that involve two images (i.e. watermark is aligned relative to the base image), and the crop operation.

  1. // Apply watermark at the right bottom corner
  2. $result = $image->merge($watermark, "right", "bottom", 50);
  3.  
  4. // Crop a 50×40 rectangle from center bottom
  5. $result = $image->crop("center", "bottom", 50, 40);

Alignment labels are a shortcut to pre-calculated coordinates. “Left” and “top” always refer to 0 (zero), “center” and “middle” refer to offset-center when two images are involved (meaning the center of the base image, offset by half of the second image), and to exact numerical center of the image when a single image is operated on (same as 50%). Similarly, “right” and “bottom” refer to the outer edge, offset by second image’s width/height with two images, and just edge coordinate for a single image (equal to 100%).

Complex coordinates

You can combine these coordinates in a more complex notation.

  1. // crops a 50×50 image from the middle of the image
  2. // for a 1000×800 image, this means a 50×50 rectangle at (475, 175)
  3. $cropped = $image->crop('50%-25', '50%-25', 50, 50);
  4. // you can get an equal result with alignment labels
  5. $cropped = $image->crop('center', 'center', 50, 50);
  6.  
  7. // crop a 60×40 rectangle
  8. //   out of the bottom-right corner of the image, with a 10px margin
  9. $cropped = $image->crop('100%-70', '100%-50', 60, 40);
  10. // or, with alignment labels:
  11. $cropped = $image->crop('right – 10', 'bottom – 10', 60, 40);
  12. // or, the complicated version:
  13. $cropped = $image->crop('- 73 + 100% + 3', '-50 + 100%', 60, 40);
  14.  
  15. // apply a watermark to the bottom-right corner with 10px margin
  16. $watermarked = $base->merge($logo, 'right – 10', 'bottom – 10', 30);

The general notation for the complex form is: [+-] a [+-] b …, where each operation must be either + or -, and each operand must be either a numeric value, a percent value or an alignment label. More complex calculations and other operators are not supported.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Reddit
  • Tumblr
  • Twitter