Exemplo n.º 1
1
 /**
  * Create a thumbnail for the given file with the given width and height.
  *
  * @param string $name name of the image to create the thumbnail of
  * @param string $fullPath absolute path to the image to create the thumbnail of
  * @param int $width width to resize to (Set to 0 to preserve aspect ratio)
  * @param int $height height to resize to (Set to 0 to preserve aspect ratio)
  * @param bool $exact This will preserve aspect ratio by using a crop after the resize
  * @return string path where the thumbnail was created
  * @throws phMagickException
  * @throws Zend_Exception
  */
 public function createThumbnailFromPath($name, $fullPath, $width, $height, $exact = true)
 {
     /** @var SettingModel $settingModel */
     $settingModel = MidasLoader::loadModel('Setting');
     $provider = $settingModel->getValueByName(MIDAS_THUMBNAILCREATOR_PROVIDER_KEY, $this->moduleName);
     $useThumbnailer = $settingModel->getValueByName(MIDAS_THUMBNAILCREATOR_USE_THUMBNAILER_KEY, $this->moduleName);
     $preprocessedFormats = array('mha', 'nrrd');
     $ext = strtolower(substr(strrchr($name, '.'), 1));
     if ($useThumbnailer === 1 && $provider === 'phmagick' && in_array($ext, $preprocessedFormats)) {
         // pre-process the file to get a temporary JPEG file and then feed it to ImageMagick later.
         $preprocessedJpeg = $this->preprocessByThumbnailer($name, $fullPath);
         if (isset($preprocessedJpeg) && file_exists($preprocessedJpeg)) {
             $fullPath = $preprocessedJpeg;
             $ext = strtolower(substr(strrchr($preprocessedJpeg, '.'), 1));
         }
     }
     // create destination
     $tmpPath = UtilityComponent::getTempDirectory('thumbnailcreator');
     $format = $settingModel->getValueByName(MIDAS_THUMBNAILCREATOR_FORMAT_KEY, $this->moduleName);
     if ($format === 'jpeg') {
         $format = MIDAS_THUMBNAILCREATOR_FORMAT_JPG;
     }
     /** @var RandomComponent $randomComponent */
     $randomComponent = MidasLoader::loadComponent('Random');
     $destination = $tmpPath . '/' . $randomComponent->generateInt() . '.' . $format;
     while (file_exists($destination)) {
         $destination = $tmpPath . '/' . $randomComponent->generateInt() . '.' . $format;
     }
     $pathThumbnail = $destination;
     if ($provider === 'phmagick') {
         $imageMagickPath = $settingModel->getValueByName(MIDAS_THUMBNAILCREATOR_IMAGE_MAGICK_KEY, $this->moduleName);
         switch ($ext) {
             case 'pdf':
             case 'mpg':
             case 'mpeg':
             case 'mp4':
             case 'm4v':
             case 'avi':
             case 'mov':
             case 'flv':
             case 'rm':
                 // If this is a video, we have to have the file extension, so symlink it
                 if (function_exists('symlink') && symlink($fullPath, $fullPath . '.' . $ext)) {
                     $p = new phmagick('', $pathThumbnail);
                     $p->setImageMagickPath($imageMagickPath);
                     $p->acquireFrame($fullPath . '.' . $ext, 0);
                     if ($exact) {
                         // preserve aspect ratio by performing a crop after the resize
                         $p->resizeExactly($width, $height);
                     } else {
                         $p->resize($width, $height);
                     }
                     unlink($fullPath . '.' . $ext);
                 }
                 break;
             default:
                 // Otherwise it is just a normal image
                 $p = new phmagick($fullPath, $pathThumbnail);
                 $p->setImageMagickPath($imageMagickPath);
                 if ($exact) {
                     // preserve aspect ratio by performing a crop after the resize
                     $p->resizeExactly($width, $height);
                 } else {
                     $p->resize($width, $height);
                 }
                 break;
         }
         // delete temporary file generated in pre-process step
         if (isset($preprocessedJpeg) && file_exists($preprocessedJpeg)) {
             unlink($preprocessedJpeg);
         }
     } elseif ($provider === MIDAS_THUMBNAILCREATOR_PROVIDER_GD || $provider === MIDAS_THUMBNAILCREATOR_PROVIDER_IMAGICK) {
         try {
             $manager = new \Intervention\Image\ImageManager(array('driver' => $provider));
             $image = $manager->make($fullPath);
             if ($height === 0) {
                 $image->widen($width);
             } elseif ($width === 0) {
                 $image->heighten($height);
             } else {
                 $image->resize($width, $height);
             }
             $image->save($pathThumbnail);
         } catch (\RuntimeException $exception) {
             Zend_Registry::get('logger')->err($exception->getMessage());
             throw new Zend_Exception('Thumbnail creation failed');
         }
     } else {
         throw new Zend_Exception('No thumbnail provider has been selected');
     }
     return $pathThumbnail;
 }