October\Rain\Database\Attach\Resizer

Source: ~/vendor/october/rain/src/Database/Attach/Resizer.php

Image resizer

Usage: Resizer::open(mixed $file) ->resize(int $width , int $height, string 'exact, portrait, landscape, auto 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);

Protected properties

protected Resource $file

The symfony uploaded file object.

protected Resource $extension

The extension of the uploaded file.

protected Resource $image

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

protected Resource $originalImage

The cached, original image.

protected int $width

Original width of the image being resized.

protected int $height

Original height of the image being resized.

protected int|null $orientation

Exif orientation of image

protected array $options

Array of options used for resizing.

Public methods

public void __construct(mixed $file)

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

Parameters
  • mixed $file - The file array provided by Laravel's Input::file('field_name') or a path to a file

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

Crops an image from its center

Parameters
  • int $cropStartX - Start on X axis
  • int $cropStartY - Start on Y axis
  • int $newWidth - The new width
  • int $newHeight - The new height
  • int $srcWidth - Source area width.
  • int $srcHeight - Source area height.

public static Resizer open(mixed $file)

Static call, Laravel style. Returns a new Resizer object, allowing for chainable calls

Parameters
  • mixed $file - The file array provided by Laravel's Input::file('field_name') or a path to a file

public self reset()

Resets the image back to the original.

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

Resizes and/or crops an image

Parameters
  • int $newWidth - The width of the image
  • int $newHeight - The height of the image
  • array $options - A set of resizing options

public void save(string $savePath)

Save the image based on its file type.

Parameters
  • string $savePath - Where to save the image

public self setOptions(array $options)

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: 95)
Parameters
  • array $options - Set of resizing option

public self sharpen(int $sharpness)

Sharpen the image across a scale of 0 - 100

Parameters
  • int $sharpness

Protected methods

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

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

Parameters
  • int $newWidth - The width of the image
  • int $newHeight - The height of the image

protected int getHeight()

Receives the image's height while respecting the exif orientation

protected array getOptimalCrop(int $newWidth, int $newHeight)

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

Parameters
  • int $newWidth - The width of the image
  • int $newHeight - The height of the image

protected mixed getOption(string $option)

Gets an individual resizer option.

Parameters
  • string $option - Option name to get

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

Receives the image's exif orientation

Parameters
  • Symfony\Component\HttpFoundation\File\File $file

protected resource getRotatedOriginal()

Receives the original but rotated image according to exif orientation

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

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

Parameters
  • int $newWidth - The width of the image
  • int $newHeight - The height of the image

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

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

Parameters
  • int $maxWidth - The maximum width of the image
  • int $maxHeight - The maximum height of the image

protected int getSizeByFixedHeight(int $newHeight)

Returns the width based on the image height

Parameters
  • int $newHeight - The height of the image

protected int getSizeByFixedWidth(int $newWidth)

Returns the height based on the image width

Parameters
  • int $newWidth - The width of the image

protected int getWidth()

Receives the image's width while respecting the exif orientation

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

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

Parameters
  • Symfony\Component\HttpFoundation\File\File $file - File instance

protected static void retainImageTransparency($img $img)

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

Parameters
  • $img $img

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

Sets an individual resizer option.

Parameters
  • string $option - Option name to set
  • mixed $value - Option value to set