Created
August 1, 2013 14:06
-
-
Save proweb/6131675 to your computer and use it in GitHub Desktop.
Joomla 3 thumbnails function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Method to create thumbnails from the current image and save them to disk. It allows creation by resizing | |
* or croppping 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 croppping | |
* @param string $thumbsFolder destination thumbs folder. null generates a thumbs folder in the image folder | |
* | |
* @return array | |
* | |
* @throws LogicException | |
* @throws InvalidArgumentException | |
* | |
* @since 12.2 | |
*/ | |
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 = array(); | |
if ($thumbs = $this->generateThumbs($thumbSizes, $creationMethod)) | |
{ | |
// Parent image properties | |
$imgProperties = self::getImageFileProperties($this->getPath()); | |
foreach ($thumbs as $thumb) | |
{ | |
// Get thumb properties | |
$thumbWidth = $thumb->getWidth(); | |
$thumbHeight = $thumb->getHeight(); | |
// Generate thumb name | |
$filename = pathinfo($this->getPath(), PATHINFO_FILENAME); | |
$fileExtension = pathinfo($this->getPath(), PATHINFO_EXTENSION); | |
$thumbFileName = $filename . '_' . $thumbWidth . 'x' . $thumbHeight . '.' . $fileExtension; | |
// Save thumb file to disk | |
$thumbFileName = $thumbsFolder . '/' . $thumbFileName; | |
if ($thumb->toFile($thumbFileName, $imgProperties->type)) | |
{ | |
// Return JImage object with thumb path to ease further manipulation | |
$thumb->path = $thumbFileName; | |
$thumbsCreated[] = $thumb; | |
} | |
} | |
} | |
return $thumbsCreated; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment