Created
August 7, 2012 16:06
-
-
Save joomlapro/3286700 to your computer and use it in GitHub Desktop.
JImage Method for resize
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
<?php | |
/** | |
* @package Vehicles | |
* @subpackage com_vehicles | |
* @copyright Copyright (C) 2012 AtomTech, Inc. All rights reserved. | |
* @license GNU General Public License version 2 or later; see LICENSE.txt | |
*/ | |
// No direct access | |
defined('_JEXEC') or die; | |
jimport('joomla.image.image'); | |
jimport('joomla.filesystem.folder'); | |
jimport('joomla.filesystem.file'); | |
/** | |
* Image component helper. | |
* | |
* @package Hello | |
* @subpackage com_hello | |
* @since 2.5 | |
*/ | |
abstract class ImageHelper | |
{ | |
/** | |
* Function to create thumb | |
* | |
* @param string $image The path to full image | |
* @param array $sizes The thumb sizes. Example: array('50x50','120x250'); | |
* @param string $folder The thumbs destination folder | |
* @param integer $method The thumbnail creation method. | |
* | |
* @return array of generated thumbnails | |
* | |
* @since 2.5 | |
*/ | |
public static function createThumb($image, $sizes, $folder, $method = 2) | |
{ | |
if (!empty($sizes) && JFile::exists($image)) | |
{ | |
// Accept a single size string as parameter | |
if (!is_array($sizes)) | |
{ | |
$sizes = array($sizes); | |
} | |
// Check | try to creaate folder | |
if (JFolder::exists($folder) || JFolder::create($folder)) | |
{ | |
$generated = array(); | |
// Process thumbs | |
foreach ($sizes as $size) | |
{ | |
// Get thumb size | |
$size = explode('x', strtolower($size)); | |
if (count($size) != 2) | |
{ | |
return false; | |
} | |
$width = $size[0]; | |
$height = $size[1]; | |
// Source object | |
$sourceImage = new JImage($image); | |
$srcHeight = $sourceImage->getHeight(); | |
$srcWidth = $sourceImage->getWidth(); | |
$properties = JImage::getImageFileProperties($image); | |
// Generate thumb name | |
$filename = JFile::getName($image); | |
$extension = JFile::getExt($filename); | |
$thumbname = str_replace('.' . $extension, '_' . $width . 'x' . $height . '.' . $extension, $filename); | |
// Try to generate the thumb | |
if ($method == 4) | |
{ | |
// Auto crop centered coordinates | |
$left = round(($srcWidth - $width) / 2); | |
$top = round(($srcHeight - $height) / 2); | |
// Crop image | |
$thumb = $sourceImage->crop($width, $height, $left, $top, true); | |
} | |
else | |
{ | |
// Resize image | |
$thumb = $sourceImage->resize($width, $height, true, $method); | |
} | |
if ($properties->type == 3) | |
{ | |
$quality = 9; | |
} | |
elseif ($properties->type == 2) | |
{ | |
$quality = 60; | |
} | |
$thumbname = $folder . '/' . $thumbname; | |
if (!JFile::exists($thumbname)) | |
{ | |
$thumb->toFile($thumbname, $properties->type, array('quality' => $quality)); | |
$generated[] = $thumbname; | |
} | |
else | |
{ | |
$generated[] = $thumbname; | |
} | |
} | |
return $generated; | |
} | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment