PhpThumbFactory::create PHP Method

create() public static method

This function returns the correct thumbnail object, augmented with any appropriate plugins. It does so by doing the following: - Getting an instance of PhpThumb - Loading plugins - Validating the default implemenation - Returning the desired default implementation if possible - Returning the GD implemenation if the default isn't available - Throwing an exception if no required libraries are present
public static create ( string $filename = null, $options = [], $isDataStream = false ) : GdThumb
$filename string The path and file to load [optional]
return GdThumb
    public static function create($filename = null, $options = array(), $isDataStream = false)
    {
        // map our implementation to their class names
        $implementationMap = array('imagick' => 'ImagickThumb', 'gd' => 'GdThumb');
        // grab an instance of PhpThumb
        $pt = PhpThumb::getInstance();
        // load the plugins
        $pt->loadPlugins(self::$pluginPath);
        $toReturn = null;
        $implementation = self::$defaultImplemenation;
        // attempt to load the default implementation
        if ($pt->isValidImplementation(self::$defaultImplemenation)) {
            $imp = $implementationMap[self::$defaultImplemenation];
            $toReturn = new $imp($filename, $options, $isDataStream);
        } elseif ($pt->isValidImplementation('gd')) {
            $imp = $implementationMap['gd'];
            $implementation = 'gd';
            $toReturn = new $imp($filename, $options, $isDataStream);
        } else {
            throw new Exception('You must have either the GD or iMagick extension loaded to use this library');
        }
        $registry = $pt->getPluginRegistry($implementation);
        $toReturn->importPlugins($registry);
        return $toReturn;
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * Create a thumbnail of an image and returns relative path in webroot
  * the options array is an associative array which can take the values
  * quality (jpg quality) and method (the method for resizing)
  *
  * @param int $width
  * @param int $height
  * @param string $img
  * @param array $options
  * @return string $path
  */
 public static function create($width, $height, $img, $options = null)
 {
     if (!file_exists($img)) {
         throw new CException('Image not found');
     }
     // Defaults for options
     $thumbDir = '.tmb';
     $jpegQuality = 80;
     $resizeMethod = 'adaptiveResize';
     if ($options) {
         extract($options, EXTR_IF_EXISTS);
     }
     $pathinfo = pathinfo($img);
     $thumbName = 'thumb_' . $resizeMethod . '_' . $width . '_' . $height . '_' . $pathinfo['basename'];
     $thumbPath = $pathinfo['dirname'] . '/' . $thumbDir . '/';
     if (!file_exists($thumbPath)) {
         mkdir($thumbPath);
     }
     if (!file_exists($thumbPath . $thumbName) || filemtime($thumbPath . $thumbName) < filemtime($img)) {
         Yii::import('vendors.image.phpThumb.PhpThumbFactory');
         $options = array('jpegQuality' => $jpegQuality);
         $thumb = PhpThumbFactory::create($img, $options);
         $thumb->{$resizeMethod}($width, $height);
         $thumb->save($thumbPath . $thumbName);
     }
     return '/' . $thumbPath . $thumbName;
 }
All Usage Examples Of PhpThumbFactory::create
PhpThumbFactory