Exemplo n.º 1
0
 public function getThumb($path, $variant = 'default')
 {
     $var = $this->variants[$variant];
     if (!$var) {
         throw new \Exception('Incorrect thumb type: ' . $variant);
     }
     $filePath = $this->getThumbPath($path, $variant);
     if (file_exists($filePath) && is_file($filePath)) {
         return realpath($filePath);
     }
     try {
         $thumb = new GD($path);
     } catch (\Exception $e) {
         return false;
     }
     $operation = $var['operation'] ?: 'resize';
     $thumb->{$operation}($var['maxWidth'], $var['maxHeight']);
     $thumb->setOptions(['jpegQuality' => $var['quality']]);
     $dir = dirname($filePath);
     if (!file_exists($dir) || !is_dir($dir)) {
         mkdir($dir, 0755, true);
     }
     $thumb->save($filePath, $var['format']);
     return realpath($filePath);
 }
 public function createThumbs()
 {
     $path = Yii::getAlias('@webroot') . DIRECTORY_SEPARATOR . $this->resolvePath();
     foreach ($this->thumbs as $profile => $config) {
         $thumbPath = Yii::getAlias('@webroot') . DIRECTORY_SEPARATOR . $this->resolveThumbPath($profile);
         if (!is_file($thumbPath)) {
             /** @var GD $thumb */
             $thumb = new GD($path);
             if (isset($config['resizeUp'])) {
                 $thumb->setOptions(array('resizeUp' => $config['resizeUp']));
             }
             if (isset($config['adaptive']) && $config['adaptive'] === false) {
                 $thumb->resize($config['width'], $config['height']);
             } else {
                 //By default we do adaptiveResize
                 $thumb->adaptiveResize($config['width'], $config['height']);
             }
             @mkdir(pathinfo($thumbPath, PATHINFO_DIRNAME), 777, true);
             $thumb->save($thumbPath);
         }
     }
 }