This is a function to create thumbnail images from a full image file in Joomla!. It uses the powerful JImage library included in the Joomla! Platform.
Edited 17/07/2012 - Added creationMethod parameter to choose the thumbnail creation method. Includes a creation method using crop.
// required libraries jimport('joomla.image.image'); jimport('joomla.filesystem.folder'); jimport('joomla.filesystem.file'); /** * Function to create thumbs from one image * @author Roberto Segura - Digital Disseny, S.L. * @version 16/07/2012 * * @param string $imgPath - path to full image * @param string/array $thumbSizes - desired thumb sizes. Example: array('50x50','120x250'); * @param string $thumbsFolder - thumbs destination folder * @param integer $creationMethod - thumbnail creation method * * @return array of generated thumbnails */ function createThumbs($imgPath, $thumbSizes, $thumbsFolder, $creationMethod = 2) { if (!empty($thumbSizes) && JFile::exists($imgPath)) { // accept a single thumbsize string as parameter if (!is_array($thumbSizes)) { $thumbSizes = array($thumbSizes); } // check | try to creaate thumbsfolder if (JFolder::exists($thumbsFolder) || JFolder::create($thumbsFolder)) { $generated = array(); // process thumbs foreach ($thumbSizes as $thumbSize) { // get thumb size $size = explode('x',strtolower($thumbSize)); if (count($size) != 2) { return false; } $thumbWidth = $desiredWidth = $size[0]; $thumbHeight = $desiredHeight = $size[1]; // source object $sourceImage = new JImage($imgPath); $srcHeight = $sourceImage->getHeight(); $srcWidth = $sourceImage->getWidth(); $imgProperties = JImage::getImageFileProperties($imgPath); // generate thumb name $filename = JFile::getName($imgPath); $fileExtension = JFile::getExt($filename); $thumbFileName = str_replace('.' . $fileExtension, '_' . $desiredWidth .'x' . $desiredHeight . '.' . $fileExtension, $filename); // try to generate the thumb if ($creationMethod == 4) { // auto crop centered coordinates $left = round(($srcWidth - $thumbWidth) / 2); $top = round(($srcHeight - $thumbHeight) / 2); // crop image $thumb = $sourceImage->crop($thumbWidth, $thumbHeight, $left, $top, true); } else { // resize image $thumb = $sourceImage->resize($thumbWidth, $thumbHeight, true, $creationMethod); } $thumbFileName = $thumbsFolder . DIRECTORY_SEPARATOR . $thumbFileName; $thumb->toFile($thumbFileName, $imgProperties->type); if (JFile::exists($thumbFileName)) { $generated[] = $thumb; } } return $generated; } } return false; }
Example uses:
$thumbSizes = array( '50x50', '250x100' ); $thumbsFolder = dirname($destFile) . DIRECTORY_SEPARATOR . 'thumbs'; // create thumbs resizing with forced proportions $createdThumbs = createThumbs($destFile, $thumbSizes, $thumbsFolder, 1); // create thumbs resizing proportionally scale inside $createdThumbs = createThumbs($destFile, $thumbSizes, $thumbsFolder, 2); // create thumbs resizing proportionally scale outside $createdThumbs = createThumbs($destFile, $thumbSizes, $thumbsFolder, 3); // create thumbs cropping a centered subimage $createdThumbs = createThumbs($destFile, $thumbSizes, $thumbsFolder, 4);
This will create a thumbs folder in the same folder as the source image containing 2 thumbnails