FastSitePHP\Media\Image

Allow an image file to be opened, manipulated, and saved. This class provides basic functionally and is not full featured with filters or advanced features. It is intended to provide needed core functionally when working with image file uploads and for thumbnail generation.

For additional PHP image libraries see the following projects:

Source Code

GitHub

Exemple de Code

Open and Edit Images Files

// Use the Media Image Class to open an image. If the image is invalid or the
// file extension doesn't match the file type then an exception will be thrown.
// Supported file extensions = [jpg, jpeg, gif, png, webp]
$img = new \FastSitePHP\Media\Image();
$img->open($file_path);

// Generate a Thumbnail or Resize the Image to a specified max width and height.
//
// When both width and height are specified the image will be sized to the
// smaller of the two values so it fits. If only width or only height are
// specified then image will be sized proportionally to the value.
$max_width = 200; // Pixels
$max_height = 200;
$img->resize($max_width, $max_height);

// Images can also be cropped to a specific dimension.
// This can be used with JavaScript or App cropping libraries to allow users
// to generate thumbnails from a full uploaded image. For example allow
// a user to crop an uploaded image to a profile thumbnail.
$left = 50;
$top = 40;
$width = 120;
$height = 80;
$target_width = $width * 2; // Optional
$target_height = $height * 2; // Optional
$img->crop($left, $top, $width, $height, $target_width, $target_height);

// Images can be rotated which is useful for sites that allow users to upload
// images because images can often upload with incorrect rotation depending on
// the mobile device or a user may simply want to change the rotation.
$degrees = 180;
$img->rotateLeft();
$img->rotateRight();
$img->rotate($degrees);

// Save Quality (0 to 100) can be specified when saving JPG or WEBP images.
// And Compression-Level (0 to 9) can be specified when saving PNG files.
$img->saveQuality(90);   // Default Quality
$img->pngCompression(6); // Default Compression-Level

// Overwrite an existing image by simply calling [save()] without
// a path or save to a new file by specifying a full file path.
$img->save($save_path);

// Optionally close the image to free memory when finished working with it.
// This happens automatically when the variable is no longer used.
$img->close();

Methods

__construct($image_path = null)

Class Constructor - Optionally open an image

__destruct()

Class Destructor - Free memory by closing image when done.

open($image_path)

Open a image file (jpg, png, gif, or webp). Image type is determined by the file extension. If the file contains an invalid image then an E_WARNING error will be triggered causing an ErrorException to be thrown when using the default error setup. Opening a [*.webp] image requires PHP 5.4 or greater.

Returns: $this

resize($max_width = null, $max_height = null)

Resize an image to a specified max width and height.

When both width and height are specified the image will be sized to the smaller of the two values so it fits.

If only width or only height are specified then image will be sized to that value.

Returns: $this

crop($left, $top, $width, $height, $target_width = null, $target_height = null)

Resize an image to a specified max width and height.

This can be used with JavaScript or App cropping libraries to allow users to generate thumbnails from a full uploaded image. For example allow a user to crop an uploaded image to a profile thumbnail.

Target Width and Height are optional and can be used to force images to a specific size. For example if you want to allow users to generate a 50x50 profile thumbnail on every crop then use target width and height.

Returns: $this

rotateLeft()

Rotate an image 90 degrees left (counter-clockwise).

Returns: $this

rotateRight()

Rotate an image 90 degrees right (clockwise).

Returns: $this

rotate($degrees)

Rotate an image by the given angle.

Returns: $this

saveQuality($new_value = null)

Getter / Setter Property

Specify the save quality (0 to 100) to be used when saving JPG or WEBP files.

Defaults to 90.

Returns: int | $this

pngCompression($new_value = null)

Getter / Setter Property

Specify the save PNG save compression-level (0 to 9). Defaults to 6.

For more on this topic refer to the links:

Returns: int | $this

save($file_name = null)

Save the image. If no file name is specified then the name of the opened file will be overwritten otherwise if only a file name is specified then a new file will be saved in the same directory/folder. Additionally full paths can be specified to save to a different directory/folder.

Saving a [*.webp] image requires PHP 5.4 or greater, however using PHP 7 or later is recommended if saving WEBP because saving might silently fail on old versions of PHP.

Returns: $this

close()

Close Image to free memory