Horde_Image_Base::setType PHP Method

setType() public method

Sets the output image type.
public setType ( string $type ) : string
$type string An image type (png, jpg, etc.)
return string The previous image type.
    public function setType($type)
    {
        // We only want the extension, not the full mimetype.
        if (strpos($type, 'image/') !== false) {
            $type = substr($type, 6);
        }
        $old = $this->_type;
        $this->_type = $type;
        return $old;
    }

Usage Example

Example #1
0
 /**
  * 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);
     }
 }
All Usage Examples Of Horde_Image_Base::setType