Class:SmartestImage

From The Smartest Wiki
Jump to: navigation, search

This class is Smartest's all-purpose class for image resizing, manipulation and display.

A subclass of SmartestFile, SmartestImage always corresponds to an actual image file on the server somewhere, in contrast to SmartestAsset, which corresponds to a database record, even if it wraps a SmartestImage instance (in the case of the three binary image types).

Combined with the basic functionalities of being a file, such as moving and copying, SmartestImage also has very powerful methods for cropping, resizing, overlapping and re-shaping image files. It can be used with JPEG, PNG or GIF files transparently.

Offsets

  • width - returns the width of the image as an integer
  • height - returns the height of the image as an integer
  • retina_width - returns half the actual pixel-for-pixel width given by $image.width, so that retina-friendly high density versions can be displayed (create the image double the required size, then use this offset to reduce the size of it by half in the markup, without having to produce a separate image resource in order to do the calculations).
  • retina_height - returns half the actual pixel-for-pixel height given by $image.height. See $image.retina_width.
  • is_portrait - returns a boolean as to whether the width of the image is larger than the height (true) or not (false). If both are the same, the value is false.
  • is_landscape - returns a boolean as to whether the height of the image is larger than the width (true) or not (false). If both are the same, the value is false.
  • is_square - returns boolean true if the image's height and width are equal, otherwise false.
  • web_path - returns the path to access the image (even if it is the cached result of an earlier resize or crop operation)
  • empty - returns true if the object has not yet been loaded with an image.
  • og_meta - returns a complete <meta name="og:image" ... > tag for the image, including a full path with domain name - a useful shortcut.
  • css_dimensions - returns a string containing the width and height pixel values in CSS format.
  • retina_css_dimensions - returns a string containing the halved width and height pixel values in CSS format, so that retina-friendly high density versions can be displayed (create the image double the required size, then use this offset to reduce the size of it by half in the markup, without having to produce a separate image resource in order to do the calculations).
  • NxN (100x100) - will crop and resize as necessary to ensure a version of the image that is the stated dimensions. Will not squash or stretch the image or alter the proportions of its contents. Oriented around the centre of your image. Returns a new SmartestImage which means it can be chained.
  • square_N (square_100) - Crops and resizes to produce a square image focussed on the centre of the image. Will enlarge if necessary in order to reach the stated size. Returns a new SmartestImage which means it can be chained.
  • width_N (width_100) - Will restrict the image to the given width, but not enlarge it if it is already within the stated limit. Returns a new SmartestImage which means it can be chained.
  • height_N (height_100) - Will restrict the image to the given height, but not enlarge it if it is already within the stated limit Returns a new SmartestImage which means it can be chained.
  • constrain_NxN (constrain_100x100) - Will ensure the image fits within the stated dimensions, but will not enlarge if it fits already. Returns a new SmartestImage which means it can be chained.
  • stretch_NxN (stretch_100x100) - Will brutally stretch your image without any concern for its content in order to reach the stated dimensions. Returns a new SmartestImage which means it can be chained.