PictureUtils

Image manipulation utils

package

Default

Methods

Performs maximum jpg optimization possible by calling the jpegtran command line tool.

compressJpgPicture(string $imagePath, string $outputPath = '') : boolean
static

jpegtran is an open source library tool to compress jpg images as much as possible, that normally comes bundled with linux distributions. If not available, we must install it on our machine.

Arguments

$imagePath

string

Full file system path to the image file we want to compress

$outputPath

string

Leave it empty (default value) to override the source image or specify a full system path (including the destination filename) where the compressed image will be stored. Warning: Do not set the output path to the same value as the source path or an error will happen. To override the source, simply leave this parameter empty.

Response

boolean

True if compression was performed or false if something failed

Performs maximum jpg optimization possible by calling the jpegtran command line tool for all files on the specified folder.

compressJpgPictureFolder(string $imagesPath) : boolean
static

This method will apply the compression to all files, so the folder shall contain only images to prevent errors. IMPORTANT: Files are replaced by the optimized version.

Arguments

$imagesPath

string

Full file system path to a folder containing images that will be replaced by a compressed version

Response

boolean

True if processing was performed or false if something failed

Method that generates a thumbnail from the specified picture binary data.

thumbnailGenerate(string $source, integer $width, integer $height, string $destination = '', string $mode = 'crop', \org\turbocommons\src\main\php\utils\number $quality = 87, string $bgColor = '000000', string $focalCenter = '') : resource
static

Requires GD php extension enabled to work.

Arguments

$source

string

The binary string containing the source picture

$width

integer

The thumbnail destination width (If empty (''), resize mode will become 'fit' automatically with a fixed height)

$height

integer

The thumbnail destination height (If empty (''), resize mode will become 'fit' automatically with a fixed width)

$destination

string

The path where thumbnail will be saved (including the filename). If not specified, thumb won't be saved to disk

$mode

string

The thumb resize and scale mode:

  • stretch: Generate the thumb by distorting it if necessary to fill the specified destination size.
  • fit: Resize the source to fit the specified width and height, but mantaining aspect ratio. This may lead to a thumb with different size than the desired one.
  • crop: (The default one) Resize the thumb to fit the full target size by cutting image sides if necessary.
  • pad: Same as fit but painting the empty spaces of the thumb with the specified bg color to make sure the full destination size is obtained.

$quality

\org\turbocommons\src\main\php\utils\number

The jpg quality of the generated thumb, from 0 to 100. 87 by default

$bgColor

string

The thumb background color as an HEX string. Only used with the pad scale mode. 000000 by default (black)

$focalCenter

string

The point of the original picture that retains the maximum interest (for example 32x100), so when cropping, the thumb will be centered as much as possible in relation to this point. (Optional and only used with crop mode).

Response

resource

The generated thumb image GD resource, so we can manipulate it directly in memory, output it to the browser with imagejpeg($returnedResource) or store it in a binary string variable with ob_start(); imagejpeg($returnedResource); $binaryString = ob_get_contents(); ob_end_flush();