Documentation

Resizer
in package

Image resizer

Usage: Resizer::open(mixed $file) ->resize(int $width , int $height, string 'exact, portrait, landscape, auto, fit or crop') ->save(string 'path/to/file.jpg', int $quality);

// Resize and save an image. Resizer::open(Input::file('field_name')) ->resize(800, 600, 'crop') ->save('path/to/file.jpg', 100);

// Recompress an image. Resizer::open('path/to/image.jpg') ->save('path/to/new_image.jpg', 60);

Tags
author

Alexey Bobkov, Samuel Georges

Table of Contents

$extension  : string
$file  : resource
$height  : int
$image  : resource
$mime  : string
$options  : array<string|int, mixed>
$orientation  : int|null
$originalImage  : resource
$width  : int
__construct()  : mixed
Instantiates the Resizer and receives the path to an image we're working with
crop()  : self
Crops an image from its center
open()  : Resizer
Static call, Laravel style.
reset()  : self
Resets the image back to the original.
resize()  : self
Resizes and/or crops an image
save()  : mixed
Save the image based on its file type.
setOptions()  : self
Sets resizer options. Available options are: - mode: Either exact, portrait, landscape, auto, fit or crop.
sharpen()  : self
Sharpen the image across a scale of 0 - 100
getDimensions()  : array<string|int, mixed>
Return the image dimensions based on the option that was chosen.
getHeight()  : int
Receives the image's height while respecting the exif orientation
getOptimalCrop()  : array<string|int, mixed>
Attempts to find the best way to crop. Whether crop is based on the image being portrait or landscape.
getOption()  : mixed
Gets an individual resizer option.
getOrientation()  : int|null
Receives the image's exif orientation
getRotatedOriginal()  : resource
Receives the original but rotated image according to exif orientation
getSizeByAuto()  : array<string|int, mixed>
Checks to see if an image is portrait or landscape and resizes accordingly.
getSizeByFit()  : array<string|int, mixed>
Fit the image inside a bounding box using maximum width and height constraints.
getSizeByFixedHeight()  : int
Returns the width based on the image height
getSizeByFixedWidth()  : int
Returns the height based on the image width
getWidth()  : int
Receives the image's width while respecting the exif orientation
openImage()  : mixed
Open a file, detect its mime-type and create an image resource from it.
retainImageTransparency()  : mixed
Manipulate an image resource in order to keep transparency for PNG and GIF files.
setOption()  : self
Sets an individual resizer option.

Properties

$extension

protected string $extension

The extension of the uploaded file.

$file

protected resource $file

The symfony uploaded file object.

$height

protected int $height

Original height of the image being resized.

$image

protected resource $image

The image (on disk) that's being resized.

$mime

protected string $mime

The mime type of the uploaded file.

$options

protected array<string|int, mixed> $options = []

Array of options used for resizing.

$orientation

protected int|null $orientation

Exif orientation of image

$originalImage

protected resource $originalImage

The cached, original image.

$width

protected int $width

Original width of the image being resized.

Methods

__construct()

Instantiates the Resizer and receives the path to an image we're working with

public __construct(mixed $file) : mixed
Parameters
$file : mixed

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

Tags
throws
Exception
Return values
mixed —

crop()

Crops an image from its center

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

Start on X axis

$cropStartY : int

Start on Y axis

$newWidth : int

The new width

$newHeight : int

The new height

$srcWidth : int = null

Source area width.

$srcHeight : int = null

Source area height.

Return values
self —

open()

Static call, Laravel style.

public static open(mixed $file) : Resizer

Returns a new Resizer object, allowing for chainable calls

Parameters
$file : mixed

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

Tags
throws
Exception
Return values
Resizer —

reset()

Resets the image back to the original.

public reset() : self
Return values
self —

resize()

Resizes and/or crops an image

public resize(int $newWidth, int $newHeight[, array<string|int, mixed> $options = [] ]) : self
Parameters
$newWidth : int

The width of the image

$newHeight : int

The height of the image

$options : array<string|int, mixed> = []

A set of resizing options

Tags
throws
Exception
Return values
self —

save()

Save the image based on its file type.

public save(string $savePath) : mixed
Parameters
$savePath : string

Where to save the image

Tags
throws
Exception

Thrown for invalid extension

Return values
mixed —

setOptions()

Sets resizer options. Available options are: - mode: Either exact, portrait, landscape, auto, fit or crop.

public setOptions(array<string|int, mixed> $options) : self
  • 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
$options : array<string|int, mixed>

Set of resizing option

Return values
self —

sharpen()

Sharpen the image across a scale of 0 - 100

public sharpen(int $sharpness) : self
Parameters
$sharpness : int
Return values
self —

getDimensions()

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

protected getDimensions(int $newWidth, int $newHeight) : array<string|int, mixed>
Parameters
$newWidth : int

The width of the image

$newHeight : int

The height of the image

Tags
throws
Exception

Thrown for invalid dimension string

Return values
array<string|int, mixed> —

getHeight()

Receives the image's height while respecting the exif orientation

protected getHeight() : int
Return values
int —

getOptimalCrop()

Attempts to find the best way to crop. Whether crop is based on the image being portrait or landscape.

protected getOptimalCrop(int $newWidth, int $newHeight) : array<string|int, mixed>
Parameters
$newWidth : int

The width of the image

$newHeight : int

The height of the image

Return values
array<string|int, mixed> —

getOption()

Gets an individual resizer option.

protected getOption(string $option) : mixed
Parameters
$option : string

Option name to get

Return values
mixed —

Depends on the option

getOrientation()

Receives the image's exif orientation

protected getOrientation(File $file) : int|null
Parameters
$file : File
Return values
int|null —

getRotatedOriginal()

Receives the original but rotated image according to exif orientation

protected getRotatedOriginal() : resource
Return values
resource —

(gd)

getSizeByAuto()

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

protected getSizeByAuto(int $newWidth, int $newHeight) : array<string|int, mixed>
Parameters
$newWidth : int

The width of the image

$newHeight : int

The height of the image

Return values
array<string|int, mixed> —

getSizeByFit()

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

protected getSizeByFit(int $maxWidth, int $maxHeight) : array<string|int, mixed>
Parameters
$maxWidth : int

The maximum width of the image

$maxHeight : int

The maximum height of the image

Return values
array<string|int, mixed> —

getSizeByFixedHeight()

Returns the width based on the image height

protected getSizeByFixedHeight(int $newHeight) : int
Parameters
$newHeight : int

The height of the image

Return values
int —

getSizeByFixedWidth()

Returns the height based on the image width

protected getSizeByFixedWidth(int $newWidth) : int
Parameters
$newWidth : int

The width of the image

Return values
int —

getWidth()

Receives the image's width while respecting the exif orientation

protected getWidth() : int
Return values
int —

openImage()

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

protected openImage(File $file) : mixed
Parameters
$file : File

File instance

Tags
throws
Exception

Thrown for invalid MIME type

Return values
mixed —

retainImageTransparency()

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

protected retainImageTransparency( $img) : mixed
Parameters
$img :
Return values
mixed —

setOption()

Sets an individual resizer option.

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

Option name to set

$value : mixed

Option value to set

Return values
self —

Search results