/**
* Method to create thumbnails from the current image and save them to disk. 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
* @param string $thumbsFolder destination thumbs folder. null generates a thumbs folder in the image folder
*
* @return array
*
* @since 2.5.0
* @throws \LogicException
* @throws \InvalidArgumentException
*/
public function createThumbs($thumbSizes, $creationMethod = self::SCALE_INSIDE, $thumbsFolder = null)
{
// Make sure the resource handle is valid.
if (!$this->isLoaded()) {
throw new \LogicException('No valid image was loaded.');
}
// No thumbFolder set -> we will create a thumbs folder in the current image folder
if (\is_null($thumbsFolder)) {
$thumbsFolder = \dirname($this->getPath()) . '/thumbs';
}
// Check destination
if (!is_dir($thumbsFolder) && (!is_dir(\dirname($thumbsFolder)) || !@mkdir($thumbsFolder))) {
throw new \InvalidArgumentException('Folder does not exist and cannot be created: ' . $thumbsFolder);
}
// Process thumbs
$thumbsCreated = [];
if ($thumbs = $this->generateThumbs($thumbSizes, $creationMethod)) {
// Parent image properties
$imgProperties = static::getImageFileProperties($this->getPath());
// Get image filename and extension.
$pathInfo = pathinfo($this->getPath());
$filename = $pathInfo['filename'];
$fileExtension = $pathInfo['extension'] ?? '';
foreach ($thumbs as $thumb) {
// Get thumb properties
$thumbWidth = $thumb->getWidth();
$thumbHeight = $thumb->getHeight();
// Generate thumb name
$thumbFileName = $filename . '_' . $thumbWidth . 'x' . $thumbHeight . '.' . $fileExtension;
// Save thumb file to disk
$thumbFileName = $thumbsFolder . '/' . $thumbFileName;
if ($thumb->toFile($thumbFileName, $imgProperties->type)) {
// Return Image object with thumb path to ease further manipulation
$thumb->path = $thumbFileName;
$thumbsCreated[] = $thumb;
}
}
}
return $thumbsCreated;
}