Back to Image class

Method generateThumbs

public array
generateThumbs
(mixed $thumbSizes, mixed $creationMethod = self::SCALE_INSIDE)
Method to generate thumbnails from the current image. It allows creation by resizing or cropping the original image.
Parameters
  • mixed $thumbSizes String or array of strings. Example: $thumbSizes = array('150x75','250x150');
  • int $creationMethod 1-3 resize $scaleMethod | 4 create cropping | 5 resize then crop
Returns
  • array
Since
  • 2.5.0
-
  • \LogicException
  • \InvalidArgumentException
Class: Image
Project: Joomla

Method generateThumbs - Source code

/**
 * Method to generate thumbnails from the current image. It allows creation by resizing or cropping the original image.
 *
 * @param   mixed    $thumbSizes      String or array of strings. Example: $thumbSizes = array('150x75','250x150');
 * @param   integer  $creationMethod  1-3 resize $scaleMethod | 4 create cropping | 5 resize then crop
 *
 * @return  array
 *
 * @since   2.5.0
 * @throws  \LogicException
 * @throws  \InvalidArgumentException
 */
public function generateThumbs($thumbSizes, $creationMethod = self::SCALE_INSIDE)
{
    // Make sure the resource handle is valid.
    if (!$this->isLoaded()) {
        throw new \LogicException('No valid image was loaded.');
    }
    // Accept a single thumbsize string as parameter
    if (!\is_array($thumbSizes)) {
        $thumbSizes = [$thumbSizes];
    }
    // Process thumbs
    $generated = [];
    if (!empty($thumbSizes)) {
        foreach ($thumbSizes as $thumbSize) {
            // Desired thumbnail size
            $size = explode('x', strtolower($thumbSize));
            if (\count($size) != 2) {
                throw new \InvalidArgumentException('Invalid thumb size received: ' . $thumbSize);
            }
            $thumbWidth = $size[0];
            $thumbHeight = $size[1];
            switch ($creationMethod) {
                case self::CROP:
                    $thumb = $this->crop($thumbWidth, $thumbHeight, null, null, true);
                    break;
                case self::CROP_RESIZE:
                    $thumb = $this->cropResize($thumbWidth, $thumbHeight, true);
                    break;
                default:
                    $thumb = $this->resize($thumbWidth, $thumbHeight, true, $creationMethod);
                    break;
            }
            // Store the thumb in the results array
            $generated[] = $thumb;
        }
    }
    return $generated;
}