Back to Image class

Method crop

public \Joomla\CMS\Image\Image
crop
(mixed $width, mixed $height, mixed $left = null, mixed $top = null, mixed $createNew = true)
Method to crop the current image.
Parameters
  • mixed $width The width of the image section to crop in pixels or a percentage.
  • mixed $height The height of the image section to crop in pixels or a percentage.
  • int $left The number of pixels from the left to start cropping.
  • int $top The number of pixels from the top to start cropping.
  • bool $createNew If true the current image will be cloned, cropped and returned; else the current image will be cropped and returned.
Returns
  • \Joomla\CMS\Image\Image
Since
  • 2.5.0
-
  • \LogicException
Class: Image
Project: Joomla

Method crop - Source code

/**
 * Method to crop the current image.
 *
 * @param   mixed    $width      The width of the image section to crop in pixels or a percentage.
 * @param   mixed    $height     The height of the image section to crop in pixels or a percentage.
 * @param   integer  $left       The number of pixels from the left to start cropping.
 * @param   integer  $top        The number of pixels from the top to start cropping.
 * @param   boolean  $createNew  If true the current image will be cloned, cropped and returned; else
 *                               the current image will be cropped and returned.
 *
 * @return  Image
 *
 * @since   2.5.0
 * @throws  \LogicException
 */
public function crop($width, $height, $left = null, $top = null, $createNew = true)
{
    // Sanitize width.
    $width = $this->sanitizeWidth($width, $height);
    // Sanitize height.
    $height = $this->sanitizeHeight($height, $width);
    // Autocrop offsets
    if (\is_null($left)) {
        $left = round(($this->getWidth() - $width) / 2);
    }
    if (\is_null($top)) {
        $top = round(($this->getHeight() - $height) / 2);
    }
    // Sanitize left.
    $left = $this->sanitizeOffset($left);
    // Sanitize top.
    $top = $this->sanitizeOffset($top);
    // Create the new truecolor image handle.
    $handle = imagecreatetruecolor($width, $height);
    // Allow transparency for the new image handle.
    imagealphablending($handle, false);
    imagesavealpha($handle, true);
    if ($this->isTransparent()) {
        // Get the transparent color values for the current image.
        $rgba = imagecolorsforindex($this->getHandle(), imagecolortransparent($this->getHandle()));
        $color = imagecolorallocatealpha($handle, $rgba['red'], $rgba['green'], $rgba['blue'], $rgba['alpha']);
        // Set the transparent color values for the new image.
        imagecolortransparent($handle, $color);
        imagefill($handle, 0, 0, $color);
    }
    if (!$this->generateBestQuality) {
        imagecopyresized($handle, $this->getHandle(), 0, 0, $left, $top, $width, $height, $width, $height);
    } else {
        imagecopyresampled($handle, $this->getHandle(), 0, 0, $left, $top, $width, $height, $width, $height);
    }
    // If we are cropping to a new image, create a new Image object.
    if ($createNew) {
        return new static($handle);
    }
    // Swap out the current handle for the new image handle.
    $this->destroy();
    $this->handle = $handle;
    return $this;
}