uCrop
uCrop is a library offering a unified interface for graphical libraries such as ImageMagick and GD. The wrapper's API does not differ between the actual libraries, enabling the developer to use simple image processing functions, such as resizing or cropping, without worrying about the actual graphical library used., (*1)
Installation
uCrop can be installed using Composer:, (*2)
composer require mingos/ucrop:dev-master
Usage
uCrop image factory
An image factory is exposed in the form of the uCrop
class. The simplest way of instantiating it is simply creating an instance of uCrop
:, (*3)
use Mingos\uCrop\uCrop;
$uCrop = new uCrop();
You can also pass in an array containing one or more default settings (they will be passed to each created image, but can be overridden):, (*4)
$uCrop = new uCrop(array(
uCrop::SETTING_COMPRESSION_QUALITY => 80,
uCrop::SETTING_GRAVITY => uCrop::GRAVITY_NORTHWEST,
uCrop::SETTING_LIBRARY => uCrop::LIBRARY_GD
));
From this point onwards, images can be created using the uCrop#image()
method:, (*5)
$image = $uCrop->image("/path/to/image.png");
Again, you can pass in an array of settings to override the default ones:, (*6)
$image = $uCrop->image("/path/to/image.png", array(
uCrop::SETTING_COMPRESSION_QUALITY => 60,
uCrop::SETTING_GRAVITY => uCrop::GRAVITY_WEST
));
Manually instantiating images
You do not need to use uCrop#image()
to instantiate individual images if you do not wish to. You can create instances manually., (*7)
use Mingos\uCrop\Image\Gd;
use Mingos\uCrop\Image\Imagick;
$imgGd = new Gd("/path/to/image.png", array(
uCrop::SETTING_COMPRESSION_QUALITY => 60,
uCrop::SETTING_GRAVITY => uCrop::GRAVITY_WEST
));
$imgImagick = new Imagick("/path/to/image.png", array(
uCrop::SETTING_COMPRESSION_QUALITY => 60,
uCrop::SETTING_GRAVITY => uCrop::GRAVITY_WEST
));
Passing in the path to an image file is mandatory; the settings are optional., (*8)
Image manipulation
Images can be manipulated using the methods available in the Mingos\uCrop\ImageInterface
interface. Please have a look at the available methods to see what you can do with the images., (*9)
resize
Resize image without maintaining aspect ratio., (*10)
Params:, (*11)
- integer
$w
- Target width; null
= leave at original width
- integer
$h
- Target height; null
= leave at original height
Returns: ImageInterface
, (*12)
$image->resize(320, 240); // resize to 320x240
$image->resize(320, null); // resize to 320 width, leave height unaltered
$image->resize(null, 240); // resize to 230 height, leave width unaltered
scale
Scale an image proportionally., (*13)
Params:, (*14)
- integer
$w
- Width
- integer
$h
- Height
- boolean
$fitInside
- Whether to fit inside the specified dimensions. Optional; default to false.
Returns: ImageInterface
, (*15)
$image = $uCrop->image("image800x600.png");
$image->scale(300, 300); // scale to 400x300
$image->scale(300, 300, true); // scale to 300x225
crop
Crop the image using either the current gravity setting or manually specified crop start position, (*16)
Params:, (*17)
- integer
$w
- Desired width
- integer
$h
- Desired height
-
null
|integer $x
- Crop starting X position. Optional; defaults to null
.
-
null
|integer $y
- Crop starting Y position. Optional; defaults to null
.
Returns: ImageInterface
, (*18)
$image->crop(300, 300); // 300x300 image, cropping from autocalculated location
$image->crop(300, 300, 0, 0); // cropped from the top left corner
cropRelative
Crop the image using either the current gravity setting or manually specified crop start position, (*19)
Params:, (*20)
- integer
$w
- Desired width
- integer
$h
- Desired height
- integer
$x
- Crop starting X position relative to the current gravity setting.
- integer
$y
- Crop starting Y position relative to the current gravity setting.
Returns: ImageInterface
, (*21)
$image->setGravity(uCrop::GRAVITY_SOUTHEAST); // crop from bottom right
$image->cropRelative(300, 300, -10, -10); // cropped from the bottom right,
// but offset by 10px up and 10px left
flip
Flips the image horizontally, vertically or both. The three modes actually act as aliases to more specialised methods:
flipHorizontal
, flipVertical
and flipBoth
., (*22)
Params:, (*23)
- integer
$mode
- Mode indicating a horizontal and/or vertical flip
Returns: ImageInterface
, (*24)
$image->flip(uCrop::FLIP_HORIZONTAL);
$image->flip(uCrop::FLIP_VERTICAL);
$image->flip(uCrop::FLIP_BOTH);
flipHorizontal
Flips the image horizontally., (*25)
Returns: ImageInterface
, (*26)
$image->flipHorizontal();
flipVertical
Flips the image vertically., (*27)
Returns: ImageInterface
, (*28)
$image->flipVertical();
flipBoth
Flips the image horizontally and vertically., (*29)
Returns: ImageInterface
, (*30)
$image->flipBoth();
overlay
Overlay the image with another image. The overlay image will be scaled to match the original image's dimensions., (*31)
Params:, (*32)
-
ImageInterface
$image - The image that should be placed on top.
Returns: ImageInterface
, (*33)
$stamp = $uCrop->image("/path/to/stamp.png");
$image->overlay($stamp);
setGravity
Sets the image's gravity for various operations., (*34)
Params:, (*35)
- integer
$gravity
- Gravity setting
Returns: ImageInterface
, (*36)
$image->setGravity(uCrop::GRAVITY_NORTHEAST);
save
Saves the image on disc. The passed in file name's extension determines the output file format. In case of a lossy file
format, the image's compression setting is used., (*37)
Params:, (*38)
- string
$filename
- Target file name
Returns: ImageInterface
, (*39)
$image->save(__DIR__ . "/output.jpg");
getWidth
Get the image's width in pixels., (*40)
Returns: integer, (*41)
$width = $image->getWidth();
getHeight
Get the images height in pixels., (*42)
Returns: integer, (*43)
$height = $image->getHeight();
getImage
Get the image resource the object is working on. The returned object type depends on what the current image type is., (*44)
Returns: resource or Imagick
, (*45)
$im = $image->getImage();