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

Parameters
Property Description
$file
mixed

The file array provided by Laravel's Input::file('field_name') or a path to a file

Returns
mixed

public crop (int $cropStartX, int $cropStartY, int $newWidth, int $newHeight, int $srcWidth = null, int $srcHeight = null) : self

Crops an image from its center

Parameters
Property Description
$cropStartX
int

Start on X axis

$cropStartY
int

Start on Y axis

$newWidth
int

The new width

$newHeight
int

The new height

$srcWidth
int

Source area width.

$srcHeight
int

Source area height.

Returns
self

public static open (mixed $file) : Resizer

Static call, Laravel style.

Returns a new Resizer object, allowing for chainable calls

Parameters
Property Description
$file
mixed

The file array provided by Laravel's Input::file('field_name') or a path to a file

Returns
Resizer

public reset () : self

Resets the image back to the original.

Returns
self

public resize (int $newWidth, int $newHeight, array $options = []) : self

Resizes and/or crops an image

Parameters
Property Description
$newWidth
int

The width of the image

$newHeight
int

The height of the image

$options
array

A set of resizing options

Returns
self

public save (string $savePath)

Save the image based on its file type.

Parameters
Property Description
$savePath
string

Where to save the image

Returns
mixed

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)
Parameters
Property Description
$options
array

Set of resizing option

Returns
self

public sharpen (int $sharpness) : self

Sharpen the image across a scale of 0 - 100

Parameters
Property Description
$sharpness
int
Returns
self

protected getDimensions (int $newWidth, int $newHeight) : array

Return the image dimensions based on the option that was chosen.

Parameters
Property Description
$newWidth
int

The width of the image

$newHeight
int

The height of the image

Returns
array

protected getExtension (string $path) : string

Get the extension from the options, otherwise use the filename extension

Parameters
Property Description
$path
string
Returns
string

protected getHeight () : int

Receives the image's height while respecting the exif orientation

Returns
int

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.

Parameters
Property Description
$newWidth
int

The width of the image

$newHeight
int

The height of the image

Returns
array

protected getOption (string $option) : mixed

Gets an individual resizer option.

Parameters
Property Description
$option
string

Option name to get

Returns
mixed

Depends on the option

protected getOrientation (Symfony\Component\HttpFoundation\File\File $file) : int | null

Receives the image's exif orientation

Parameters
Property Description
$file
Symfony\Component\HttpFoundation\File\File
Returns
int | null

protected getRotatedOriginal () : GdImage | false

Receives the original but rotated image according to exif orientation

Returns
GdImage | false

protected getSizeByAuto (int $newWidth, int $newHeight) : array

Checks to see if an image is portrait or landscape and resizes accordingly.

Parameters
Property Description
$newWidth
int

The width of the image

$newHeight
int

The height of the image

Returns
array

protected getSizeByFit (int $maxWidth, int $maxHeight) : array

Fit the image inside a bounding box using maximum width and height constraints.

Parameters
Property Description
$maxWidth
int

The maximum width of the image

$maxHeight
int

The maximum height of the image

Returns
array

protected getSizeByFixedHeight (int $newHeight) : int

Returns the width based on the image height

Parameters
Property Description
$newHeight
int

The height of the image

Returns
int

protected getSizeByFixedWidth (int $newWidth) : int

Returns the height based on the image width

Parameters
Property Description
$newWidth
int

The width of the image

Returns
int

protected getWidth () : int

Receives the image's width while respecting the exif orientation

Returns
int

protected openImage (Symfony\Component\HttpFoundation\File\File $file) : mixed

Open a file, detect its mime-type and create an image resource from it.

Parameters
Property Description
$file
Symfony\Component\HttpFoundation\File\File

File instance

Returns
mixed

protected retainImageTransparency (GdImage $img) : void

Manipulate an image resource in order to keep transparency for PNG and GIF files.

Parameters
Property Description
$img
GdImage
Returns
void

protected setOption (string $option, mixed $value) : self

Sets an individual resizer option.

Parameters
Property Description
$option
string

Option name to set

$value
mixed

Option value to set

Returns
self
Copyright © 2024 Winter CMS