Resizer
Image resizer
class Winter\Storm\Database\Attach\Resizer
Usage: Resizer::open(mixed $file) ->resize(int $width , int $height, string 'exact, portrait, landscape, auto, fit or crop') ->setOptions(['quality' => int $quality]) ->save(string 'path/to/file.jpg');
// Resize and save an image. Resizer::open(Input::file('field_name')) ->resize(800, 600, 'crop') ->setOptions(['quality' => 100]) ->save('path/to/file.jpg');
// Recompress an image. Resizer::open('path/to/image.jpg') ->setOptions(['quality' => 60]) ->save('path/to/new_image.jpg');
Properties
protected $extension : string
The extension of the uploaded file.
protected $file : resource
The symfony uploaded file object.
protected $height : int
Original height of the image being resized.
protected $image : GdImage
The image (on disk) that's being resized.
protected $mime : string
The mime type of the uploaded file.
protected
$options
:
array
= []
Array of options used for resizing.
protected $orientation : int | null
Exif orientation of image
protected $originalImage : GdImage
The cached, original image.
protected $width : int
Original width of the image being resized.
Methods
public __construct (mixed $file)
Instantiates the Resizer and receives the path to an image we're working with
| Property | Type | Description |
|---|---|---|
| $file | mixed |
mixed
The file array provided by Laravel's Input::file('field_name') or a path to a file |
public
crop (int $cropStartX, int $cropStartY, int $newWidth, int $newHeight, int $srcWidth = null, int $srcHeight = null)
: self
Crops an image from its center
| Property | Type | Description |
|---|---|---|
| $cropStartX | int |
int
Start on X axis |
| $cropStartY | int |
int
Start on Y axis |
| $newWidth | int |
int
The new width |
| $newHeight | int |
int
The new height |
| $srcWidth | int |
int
Source area width. |
| $srcHeight | int |
int
Source area height. |
public static open (mixed $file) : Resizer
Static call, Laravel style.
Returns a new Resizer object, allowing for chainable calls
| Property | Type | Description |
|---|---|---|
| $file | mixed |
mixed
The file array provided by Laravel's Input::file('field_name') or a path to a file |
public reset () : self
Resets the image back to the original.
public
resize (int $newWidth, int $newHeight, array $options = [])
: self
Resizes and/or crops an image
| Property | Type | Description |
|---|---|---|
| $newWidth | int |
int
The width of the image |
| $newHeight | int |
int
The height of the image |
| $options | array |
array
A set of resizing options |
public save (string $savePath)
Save the image based on its file type.
| Property | Type | Description |
|---|---|---|
| $savePath | string |
string
Where to save the image |
public setOptions (array $options) : self
Sets resizer options. Available options are:
- mode: Either exact, portrait, landscape, auto, fit or crop.
- offset: The offset of the crop = [ left, top ]
- sharpen: Sharpen image, from 0 - 100 (default: 0)
- interlace: Interlace image, Boolean: false (disabled: default), true (enabled)
- quality: Image quality, from 0 - 100 (default: 90)
| Property | Type | Description |
|---|---|---|
| $options | array |
array
Set of resizing option |
public sharpen (int $sharpness) : self
Sharpen the image across a scale of 0 - 100
| Property | Type | Description |
|---|---|---|
| $sharpness | int |
int
|
protected getDimensions (int $newWidth, int $newHeight) : array
Return the image dimensions based on the option that was chosen.
| Property | Type | Description |
|---|---|---|
| $newWidth | int |
int
The width of the image |
| $newHeight | int |
int
The height of the image |
protected getExtension (string $path) : string
Get the extension from the options, otherwise use the filename extension
| Property | Type | Description |
|---|---|---|
| $path | string |
string
|
protected getHeight () : int
Receives the image's height while respecting the exif orientation
protected getOptimalCrop (int $newWidth, int $newHeight) : array
Attempts to find the best way to crop. Whether crop is based on the image being portrait or landscape.
| Property | Type | Description |
|---|---|---|
| $newWidth | int |
int
The width of the image |
| $newHeight | int |
int
The height of the image |
protected getOption (string $option) : mixed
Gets an individual resizer option.
| Property | Type | Description |
|---|---|---|
| $option | string |
string
Option name to get |
Depends on the option
protected getOrientation (Symfony\Component\HttpFoundation\File\File $file) : int | null
Receives the image's exif orientation
| Property | Type | Description |
|---|---|---|
| $file | Symfony\Component\HttpFoundation\File\File |
Symfony\Component\HttpFoundation\File\File
|
protected getRotatedOriginal () : GdImage | false
Receives the original but rotated image according to exif orientation
protected getSizeByAuto (int $newWidth, int $newHeight) : array
Checks to see if an image is portrait or landscape and resizes accordingly.
| Property | Type | Description |
|---|---|---|
| $newWidth | int |
int
The width of the image |
| $newHeight | int |
int
The height of the image |
protected getSizeByFit (int $maxWidth, int $maxHeight) : array
Fit the image inside a bounding box using maximum width and height constraints.
| Property | Type | Description |
|---|---|---|
| $maxWidth | int |
int
The maximum width of the image |
| $maxHeight | int |
int
The maximum height of the image |
protected getSizeByFixedHeight (int $newHeight) : int
Returns the width based on the image height
| Property | Type | Description |
|---|---|---|
| $newHeight | int |
int
The height of the image |
protected getSizeByFixedWidth (int $newWidth) : int
Returns the height based on the image width
| Property | Type | Description |
|---|---|---|
| $newWidth | int |
int
The width of the image |
protected getWidth () : int
Receives the image's width while respecting the exif orientation
protected openImage (Symfony\Component\HttpFoundation\File\File $file) : mixed
Open a file, detect its mime-type and create an image resource from it.
| Property | Type | Description |
|---|---|---|
| $file | Symfony\Component\HttpFoundation\File\File |
Symfony\Component\HttpFoundation\File\File
File instance |
protected retainImageTransparency (GdImage $img) : void
Manipulate an image resource in order to keep transparency for PNG and GIF files.
| Property | Type | Description |
|---|---|---|
| $img | GdImage |
GdImage
|
protected setOption (string $option, mixed $value) : self
Sets an individual resizer option.
| Property | Type | Description |
|---|---|---|
| $option | string |
string
Option name to set |
| $value | mixed |
mixed
Option value to set |