/** * Resize an image and save to given destination. * * @param string $source Path to source image. * @param string $dest Path to the location where resized image will be stored. * @param array $size Either array('width' => new_width, 'height' => new_height) or array(0 => new_width, 1 => new_height). * @param string $name Custom name for resized image file. * * @return string Path to resized image file on success, original source image path on failure. */ public static function resize($source, $dest, $size, $name = '') { // Initialize variables if (is_string($size) and preg_match('/\\d+x\\d+/i', $size)) { // Parse user defined image dimension $size = explode('x', strtolower($size), 2); } $width = intval(isset($size['width']) ? $size['width'] : (isset($size[0]) ? $size[0] : 0)); $height = intval(isset($size['height']) ? $size['height'] : (isset($size[1]) ? $size[1] : 0)); $resize = $width > 0 or $height > 0; // Do resize if possible if ($resize) { // Check current image size $imgSize = getimagesize($source); if ($imgSize[0] != $width and $imgSize[1] != $height) { $image = new Ace_Media_Image($source); $image->resize($width, $height, $height > 0 and $height > 0 ? false : ($width > 0 ? 'W' : 'H')); if (JFolder::create($dest) and $image->save($dest = "{$dest}/" . (empty($name) ? basename($source) : $name), 90)) { return $dest; } } } return $source; }
// Initialize variables if (!isset($_REQUEST['src']) or !isset($_REQUEST['width']) or !isset($_REQUEST['dest'])) { jexit(JText::_('JSN_MOBILIZE_INVALID_REQUEST')); } $src = $_REQUEST['src']; $width = $_REQUEST['width']; $dest = $_REQUEST['dest']; // Load necessary Joomla libraries jimport('joomla.filesystem.file'); jimport('joomla.filesystem.folder'); // Load image manipulation library require_once JPATH_ROOT . DS . 'plugins' . DS . 'system' . DS . 'jsnmobilize' . DS . 'libraries' . DS . '3rd-party' . DS . 'ace-media-image' . DS . 'image.php'; // Resize the original image to requested width $image = new Ace_Media_Image($_SERVER['DOCUMENT_ROOT'] . DS . str_replace('/', DS, $src)); $image->resize((int) $width, null, 'W'); if (JFolder::create(JPATH_ROOT . DS . $dest . DS . $width) and $image->save(JPATH_ROOT . DS . $dest . DS . $width . DS . basename($src), 90)) { if (isset($_REQUEST['return']) and $_REQUEST['return'] == 'uri') { // Get link to optimized image $link = str_replace('/plugins/system/jsnmobilize/libraries/joomlashine/response/image/', '', JURI::root()) . str_replace(str_replace(array('/', '\\'), array('/', '/'), JPATH_ROOT), '', str_replace(array('/', '\\'), array('/', '/'), JPATH_ROOT . DS . $dest . DS . $width . DS . basename($src))); jexit($link); } else { // Send the requisite header information header('Content-Type: ' . $image->getMimeType()); header('Content-Length: ' . filesize(JPATH_ROOT . DS . $dest . DS . $width . DS . basename($src))); // Read image content then send back to the client broswer if ($image = JFile::read(JPATH_ROOT . DS . $dest . DS . $width . DS . basename($src))) { jexit($image); } else { jexit(JText::_('JSN_MOBILIZE_READ_IMAGE_FAIL')); } }