/** * * @return Horde_Image */ protected function _create() { if ($GLOBALS['conf']['image']['squaremini']) { $generator = Ansel_ImageGenerator::factory('SquareThumb', array('width' => min(50, $this->_dimensions['width']), 'height' => min(50, $this->_dimensions['height']), 'image' => $this->_image, 'style' => $this->_params['style'])); return $generator->create(); } else { $this->_image->resize(min(50, $this->_dimensions['width']), min(50, $this->_dimensions['height']), true); if ($GLOBALS['conf']['thumbnail']['unsharp'] && Ansel::isAvailable('Unsharpmask')) { try { $this->_image->addEffect('Unsharpmask', array('radius' => $GLOBALS['conf']['thumbnail']['radius'], 'threshold' => $GLOBALS['conf']['thumbnail']['threshold'], 'amount' => $GLOBALS['conf']['thumbnail']['amount'])); $this->_image->applyEffects(); } catch (Horde_Image_Exception $e) { throw new Ansel_Exception($e); } } return $this->_image->getHordeImage(); } }
/** * Get a list of available, currently usable thumbnail styles. * * @return array An array of Classnames => titles */ protected function _thumbStyles() { // Iterate all available thumbstyles: $dir = ANSEL_BASE . '/lib/ImageGenerator'; $files = scandir($dir); $thumbs = array(); foreach ($files as $file) { if (substr($file, -9) == 'Thumb.php') { try { $generator = Ansel_ImageGenerator::factory(substr($file, 0, -4), array('style' => Ansel::getStyleDefinition('ansel_default'))); $thumbs[substr($file, 0, -4)] = $generator->title; } catch (Ansel_Exception $e) { } } } return $thumbs; }
/** * Returns the key image for this gallery. * * @param Ansel_Style $style Force the use of this style, if it's available * otherwise use whatever style is choosen for * this gallery. If prettythumbs are not * available then we always use ansel_default * style. * * @return mixed The image_id of the key image or false. */ public function getKeyImage(Ansel_Style $style = null) { if (is_null($style)) { $style = $this->getStyle(); } if ($style->keyimage_type != 'Thumb') { $thumbstyle = $style->keyimage_type; $styleHash = $style->getHash($thumbstyle); // First check for the existence of a key image in the specified style if ($this->get('default_prettythumb')) { $thumbs = @unserialize($this->get('default_prettythumb')); } if (!isset($thumbs) || !is_array($thumbs)) { $thumbs = array(); } if (!empty($thumbs[$styleHash])) { return $thumbs[$styleHash]; } // Don't already have one, must generate it. $params = array('gallery' => $this, 'style' => $style); try { $iview = Ansel_ImageGenerator::factory($style->keyimage_type, $params); $img = $iview->create(); // Note the gallery_id is negative for generated stacks $iparams = array('image_filename' => $this->get('name'), 'image_caption' => $this->get('name'), 'data' => $img->raw(), 'image_sort' => 0, 'gallery_id' => -$this->id); $newImg = new Ansel_Image($iparams); $newImg->save(); $prettyData = serialize(array_merge($thumbs, array($styleHash => $newImg->id))); $this->set('default_prettythumb', $prettyData, true); // Make sure the hash is saved since it might be different then // the gallery's $GLOBALS['injector']->getInstance('Ansel_Storage')->ensureHash($styleHash); return $newImg->id; } catch (Ansel_Exception $e) { // Might not support the requested style...try ansel_default // but protect against infinite recursion. Horde::log($e->getMessage(), 'ERR'); if ($style->keyimage_type != 'plain') { return $this->getKeyImage(Ansel::getStyleDefinition('ansel_default')); } } } else { // We are just using an image thumbnail. if ($this->countImages()) { if ($default = $this->get('default')) { return $default; } $keys = $this->listImages(); $this->set('default', $keys[count($keys) - 1]); $this->set('default_type', 'auto'); $this->save(); return $keys[count($keys) - 1]; } if ($this->hasSubGalleries()) { // Fall through to a key image of a sub gallery. try { $galleries = $GLOBALS['injector']->getInstance('Ansel_Storage')->listGalleries(array('parent' => $this->id, 'all_levels' => false)); foreach ($galleries as $gallery) { if ($default_img = $gallery->getKeyImage($style)) { return $default_img; } } } catch (Ansel_Exception $e) { return false; } } } // Could not find a key image return false; }
/** * Creates and caches the given view. * * @param string $view Which view to create. * @param Ansel_Style $style A style object * * @throws Ansel_Exception */ public function createView($view, Ansel_Style $style = null, $watermark = '') { // Default to the gallery's style if (empty($style)) { $style = $GLOBALS['injector']->getInstance('Ansel_Storage')->getGallery($this->gallery)->getStyle(); } // Get the VFS info. $vfspath = $this->getVFSPath($view, $style); if ($GLOBALS['injector']->getInstance('Horde_Core_Factory_Vfs')->create('images')->exists($vfspath, $this->getVFSName($view))) { return; } try { $data = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Vfs')->create('images')->read($this->getVFSPath('full'), $this->getVFSName('full')); } catch (Horde_Vfs_Exception $e) { Horde::log($e, 'ERR'); throw new Ansel_Exception($e); } // Force screen images to ALWAYS be jpegs for performance/size if ($view == 'screen' && $GLOBALS['conf']['image']['type'] != 'jpeg') { $originalType = $this->_image->setType('jpeg'); } else { $originalType = false; } $vHash = $this->getViewHash($view, $style); $this->_image->loadString($data); if ($view == 'thumb') { $viewType = $style->thumbstyle; } else { // Screen, Mini $viewType = ucfirst($view); } try { $iview = Ansel_ImageGenerator::factory($viewType, array('image' => $this, 'style' => $style)); } catch (Ansel_Exception $e) { // It could be we don't support the requested effect, try // ansel_default before giving up. if ($view == 'thumb' && $viewType != 'Thumb') { $iview = Ansel_ImageGenerator::factory('Thumb', array('image' => $this, 'style' => Ansel::getStyleDefinition('ansel_default'))); } else { // If it wasn't a thumb, then something else must be wrong throw $e; } } // generate the view $iview->create(); // Cache the data from the new ImageGenerator try { $this->_data[$vHash] = $this->_image->raw(); } catch (Horde_Image_Exception $e) { throw new Ansel_Exception($e); } // ...and put it in Horde_Image obejct, then save $this->_image->loadString($this->_data[$vHash]); $this->_loaded[$vHash] = true; $GLOBALS['injector']->getInstance('Horde_Core_Factory_Vfs')->create('images')->writeData($vfspath, $this->getVFSName($vHash), $this->_data[$vHash], true); // Watermark if (!empty($watermark)) { $this->watermark($view); // Cache the data again try { $this->_data[$vHash] = $this->_image->raw(); } catch (Horde_Image_Exception $e) { throw new Ansel_Exception($e); } $GLOBALS['injector']->getInstance('Horde_Core_Factory_Vfs')->create('images')->writeData($vfspath, $this->getVFSName($view), $this->_data[$vHash]); } // Revert any type change if ($originalType) { $this->_image->setType($originalType); } }