public function read(MediaInterface $media)
 {
     $ret = [];
     $mimeType = $media->getFile()->getMimeType();
     foreach (['getWidth' => 'TfWidth', 'getHeight' => 'TfHeight', 'getChannels' => 'TfChannels', 'getColorDepth' => 'TfBits', 'getDuration' => 'TfDuration'] as $method => $tag) {
         $classname = 'Alchemy\\Phrasea\\Metadata\\Tag\\' . $tag;
         if (method_exists($media, $method)) {
             $ret[] = new Metadata(new $classname(), new MonoValue(call_user_func([$media, $method])));
         }
     }
     if ($mimeType == 'application/pdf' && null !== $this->pdfToText) {
         try {
             $text = $this->pdfToText->getText($media->getFile()->getRealPath());
             if (trim($text)) {
                 $ret[] = new Metadata(new PdfText(), new MonoValue($text));
             }
         } catch (XPDFException $e) {
         }
     }
     $ret[] = new Metadata(new TfMimetype(), new MonoValue($mimeType));
     $ret[] = new Metadata(new TfSize(), new MonoValue($media->getFile()->getSize()));
     $ret[] = new Metadata(new TfBasename(), new MonoValue(pathinfo($media->getFile()->getFileName(), PATHINFO_BASENAME)));
     $ret[] = new Metadata(new TfFilename(), new MonoValue(pathinfo($media->getFile()->getFileName(), PATHINFO_FILENAME)));
     $ret[] = new Metadata(new TfExtension(), new MonoValue(pathinfo($media->getFile()->getFileName(), PATHINFO_EXTENSION)));
     return $ret;
 }
 public function execute(SpecificationInterface $spec, MediaInterface $source, $dest)
 {
     if (!$spec instanceof Flash) {
         throw new SpecNotSupportedException('SwfTools only accept Flash specs');
     }
     $tmpDest = $this->tmpFileManager->createTemporaryFile(self::TMP_FILE_SCOPE, 'pdf2swf');
     try {
         if ($source->getFile()->getMimeType() != 'application/pdf') {
             $this->container['unoconv']->transcode($source->getFile()->getPathname(), Unoconv::FORMAT_PDF, $tmpDest);
         } else {
             copy($source->getFile()->getPathname(), $tmpDest);
         }
         $this->container['swftools.pdf-file']->toSwf($tmpDest, $dest);
         $this->tmpFileManager->clean(self::TMP_FILE_SCOPE);
     } catch (BinaryAdapterException $e) {
         $this->tmpFileManager->clean(self::TMP_FILE_SCOPE);
         throw new RuntimeException('Unable to transmute flash to image due to Binary Adapter', $e->getCode(), $e);
     } catch (UnoconvException $e) {
         $this->tmpFileManager->clean(self::TMP_FILE_SCOPE);
         throw new RuntimeException('Unable to transmute document to flash due to Unoconv', null, $e);
     } catch (SwfToolsException $e) {
         $this->tmpFileManager->clean(self::TMP_FILE_SCOPE);
         throw new RuntimeException('Unable to transmute document to flash due to SwfTools', null, $e);
     } catch (RuntimeException $e) {
         $this->tmpFileManager->clean(self::TMP_FILE_SCOPE);
         throw $e;
     }
 }
Esempio n. 3
0
 public function execute(SpecificationInterface $spec, MediaInterface $source, $dest)
 {
     if (!$spec instanceof Audio) {
         throw new SpecNotSupportedException('FFMpeg Adapter only supports Audio specs');
     }
     try {
         $audio = $this->container['ffmpeg.ffmpeg']->open($source->getFile()->getPathname());
     } catch (FFMpegException $e) {
         throw new RuntimeException('Unable to transmute audio to audio due to FFMpeg', null, $e);
     }
     /* @var $spec \MediaAlchemyst\Specification\Audio */
     $format = $this->getFormatFromFileType($dest);
     if ($spec->getAudioCodec()) {
         $format->setAudioCodec($spec->getAudioCodec());
     }
     if ($spec->getAudioSampleRate()) {
         $audio->addFilter(new AudioResamplableFilter($spec->getAudioSampleRate()));
     }
     if ($spec->getAudioKiloBitrate()) {
         $format->setAudioKiloBitrate($spec->getAudioKiloBitrate());
     }
     try {
         $audio->save($format, $dest);
     } catch (FFMpegException $e) {
         throw new RuntimeException('Unable to transmute audio to audio due to FFMpeg', null, $e);
     }
 }
 /**
  * @param MediaInterface $media
  * @param int $maxWidth
  * @param int $maxHeight
  * @return bool
  */
 protected function shouldResize($media, $maxWidth, $maxHeight)
 {
     if (!$media instanceof Image && !$media instanceof Video) {
         return false;
     }
     return $media->getWidth() > $maxWidth || $media->getHeight() > $maxHeight;
 }
 public function execute(SpecificationInterface $spec, MediaInterface $source, $dest)
 {
     if (!$spec instanceof Animation) {
         throw new SpecNotSupportedException('Imagine Adapter only supports Image specs');
     }
     if ($source->getType() !== MediaInterface::TYPE_VIDEO) {
         throw new SpecNotSupportedException('Imagine Adapter only supports Images');
     }
     try {
         $movie = $this->container['ffmpeg.ffmpeg']->open($source->getFile()->getPathname());
         $duration = $source->getDuration();
         $time = $pas = Max(1, $duration / 11);
         $files = array();
         while (ceil($time) < floor($duration)) {
             $files[] = $tmpFile = $this->tmpFileManager->createTemporaryFile(self::TMP_FILE_SCOPE, 'ffmpeg', 'jpg');
             $frame = $movie->frame(TimeCode::fromSeconds($time));
             $frame->filters()->fixDisplayRatio();
             $frame->save($tmpFile);
             $time += $pas;
         }
         foreach ($files as $file) {
             $image = $this->container['imagine']->open($file);
             if ($spec->getWidth() && $spec->getHeight()) {
                 $box = $this->boxFromSize($spec, $image->getSize()->getWidth(), $image->getSize()->getHeight());
                 if ($spec->getResizeMode() == Animation::RESIZE_MODE_OUTBOUND) {
                     /* @var $image \Imagine\Gmagick\Image */
                     $image = $image->thumbnail($box, ImageInterface::THUMBNAIL_OUTBOUND);
                 } else {
                     $image = $image->resize($box);
                 }
             }
             $image->save($file, array('quality' => $spec->getQuality(), 'resolution-units' => $spec->getResolutionUnit(), 'resolution-x' => $spec->getResolutionX(), 'resolution-y' => $spec->getResolutionY()));
             unset($image);
         }
         $image = $this->container['imagine']->open(array_shift($files));
         foreach ($files as $file) {
             $image->layers()->add($this->container['imagine']->open($file));
         }
         $image->save($dest, array('animated' => true, 'animated.delay' => 800, 'animated.loops' => 0));
         $this->tmpFileManager->clean(self::TMP_FILE_SCOPE);
     } catch (FFMpegException $e) {
         $this->tmpFileManager->clean(self::TMP_FILE_SCOPE);
         throw new RuntimeException('Unable to transmute video to animation due to FFMpeg', null, $e);
     } catch (ImagineException $e) {
         $this->tmpFileManager->clean(self::TMP_FILE_SCOPE);
         throw new RuntimeException('Unable to transmute video to animation due to Imagine', null, $e);
     } catch (RuntimeException $e) {
         $this->tmpFileManager->clean(self::TMP_FILE_SCOPE);
         throw $e;
     }
     return $this;
 }
Esempio n. 6
0
 public function execute(SpecificationInterface $spec, MediaInterface $source, $dest)
 {
     if (!$spec instanceof Video) {
         throw new SpecNotSupportedException('FFMpeg Adapter only supports Video specs');
     }
     try {
         $video = $this->container['ffmpeg.ffmpeg']->open($source->getFile()->getPathname());
     } catch (FFMpegException $e) {
         throw new RuntimeException('Unable to transmute video to video due to FFMpeg', null, $e);
     }
     /* @var $spec \MediaAlchemyst\Specification\Video */
     $format = $this->getFormatFromFileType($dest);
     $resizeMode = ResizeFilter::RESIZEMODE_FIT;
     if ($spec->getResizeMode()) {
         $resizeMode = $spec->getResizeMode();
     }
     $video->addFilter(new SynchronizeFilter());
     $video->addFilter(new ResizeFilter(new Dimension($spec->getWidth(), $spec->getHeight()), $resizeMode));
     if ($spec->getAudioCodec()) {
         $format->setAudioCodec($spec->getAudioCodec());
     }
     if ($spec->getVideoCodec()) {
         $format->setVideoCodec($spec->getVideoCodec());
     }
     if ($spec->getAudioSampleRate()) {
         $video->addFilter(new AudioResamplableFilter($spec->getAudioSampleRate()));
     }
     if ($spec->getAudioKiloBitrate()) {
         $format->setAudioKiloBitrate($spec->getAudioKiloBitrate());
     }
     if ($spec->getKiloBitrate()) {
         $format->setKiloBitrate($spec->getKiloBitrate());
     }
     if ($spec->getFramerate() && $spec->getGOPSize()) {
         $video->addFilter(new FrameRateFilter(new FrameRate($spec->getFramerate()), $spec->getGOPSize()));
     }
     try {
         $video->save($format, $dest);
         if ($format instanceof X264) {
             $this->container['mp4box']->process($dest);
         }
     } catch (FFMpegException $e) {
         throw new RuntimeException('Unable to transmute video to video due to FFMpeg', null, $e);
     } catch (MP4BoxException $e) {
         throw new RuntimeException('Unable to transmute video to video due to MP4Box', null, $e);
     }
     return $this;
 }
 public function execute(SpecificationInterface $spec, MediaInterface $source, $dest)
 {
     if (!$spec instanceof Image) {
         throw new SpecNotSupportedException('Document2Image only accept Image specs');
     }
     $tmpDest = $this->tmpFileManager->createTemporaryFile(self::TMP_FILE_SCOPE, 'unoconv');
     try {
         if ($source->getFile()->getMimeType() != 'application/pdf') {
             $this->container['unoconv']->transcode($source->getFile()->getPathname(), Unoconv::FORMAT_PDF, $tmpDest, '1-1');
         } else {
             copy($source->getFile()->getPathname(), $tmpDest);
         }
         $tmpDestSinglePage = $this->tmpFileManager->createTemporaryFile(self::TMP_FILE_SCOPE, 'unoconv-single');
         $this->container['ghostscript.transcoder']->toPDF($tmpDest, $tmpDestSinglePage, 1, 1);
         $image = $this->container['imagine']->open($tmpDestSinglePage);
         $options = array('quality' => $spec->getQuality(), 'resolution-units' => $spec->getResolutionUnit(), 'resolution-x' => $spec->getResolutionX(), 'resolution-y' => $spec->getResolutionY(), 'disable-alpha' => $spec->isFlatten());
         if ($spec->getWidth() && $spec->getHeight()) {
             $box = $this->boxFromSize($spec, $image->getSize()->getWidth(), $image->getSize()->getHeight());
             if (null !== $box) {
                 if ($spec->getResizeMode() == Image::RESIZE_MODE_OUTBOUND) {
                     /* @var $image \Imagine\Gmagick\Image */
                     $image = $image->thumbnail($box, ImageInterface::THUMBNAIL_OUTBOUND);
                 } else {
                     $image = $image->resize($box);
                 }
             }
         }
         $image->save($dest, $options);
         unset($image);
         $this->tmpFileManager->clean(self::TMP_FILE_SCOPE);
     } catch (GhostscriptException $e) {
         $this->tmpFileManager->clean(self::TMP_FILE_SCOPE);
         throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
     } catch (UnoconvException $e) {
         $this->tmpFileManager->clean(self::TMP_FILE_SCOPE);
         throw new RuntimeException('Unable to transmute document to image due to Unoconv', null, $e);
     } catch (ImagineException $e) {
         $this->tmpFileManager->clean(self::TMP_FILE_SCOPE);
         throw new RuntimeException('Unable to transmute document to image due to Imagine', null, $e);
     } catch (MediaVorusException $e) {
         $this->tmpFileManager->clean(self::TMP_FILE_SCOPE);
         throw new RuntimeException('Unable to transmute document to image due to MediaVorus', null, $e);
     } catch (RuntimeException $e) {
         $this->tmpFileManager->clean(self::TMP_FILE_SCOPE);
         throw $e;
     }
 }
Esempio n. 8
0
 public function substitute(\record_adapter $record, $name, MediaInterface $media)
 {
     $newfilename = $record->get_record_id() . '_0_' . $name . '.' . $media->getFile()->getExtension();
     $subdef_def = false;
     if ($name == 'document') {
         $baseprefs = $record->get_databox()->get_sxml_structure();
         $pathhd = \p4string::addEndSlash((string) $baseprefs->path);
         $filehd = $record->get_record_id() . "_document." . strtolower($media->getFile()->getExtension());
         $pathhd = \databox::dispatch($this->fs, $pathhd);
         $this->fs->copy($media->getFile()->getRealPath(), $pathhd . $filehd, true);
         $subdefFile = $pathhd . $filehd;
         $meta_writable = true;
     } else {
         $type = $record->isStory() ? 'image' : $record->get_type();
         $subdef_def = $record->get_databox()->get_subdef_structure()->get_subdef($type, $name);
         if ($record->has_subdef($name) && $record->get_subdef($name)->is_physically_present()) {
             $path_file_dest = $record->get_subdef($name)->get_pathfile();
             $record->get_subdef($name)->remove_file();
             $record->clearSubdefCache($name);
         } else {
             $path = \databox::dispatch($this->fs, $subdef_def->get_path());
             $this->fs->mkdir($path, 0750);
             $path_file_dest = $path . $newfilename;
         }
         try {
             $this->alchemyst->turnInto($media->getFile()->getRealPath(), $path_file_dest, $subdef_def->getSpecs());
         } catch (MediaAlchemystException $e) {
             return;
         }
         $subdefFile = $path_file_dest;
         $meta_writable = $subdef_def->meta_writeable();
     }
     $this->fs->chmod($subdefFile, 0760);
     $media = $this->mediavorus->guess($subdefFile);
     \media_subdef::create($this->app, $record, $name, $media);
     $record->delete_data_from_cache(\record_adapter::CACHE_SUBDEFS);
     if ($meta_writable) {
         $record->write_metas();
     }
     if ($name == 'document') {
         $record->rebuild_subdefs();
     }
     $this->dispatcher->dispatch(RecordEvents::MEDIA_SUBSTITUTED, new RecordMediaSubstitutedEvent($record));
 }
Esempio n. 9
0
 public function execute(SpecificationInterface $spec, MediaInterface $source, $dest)
 {
     if (!$spec instanceof Image) {
         throw new SpecNotSupportedException('SwfTools only accept Image specs');
     }
     $tmpDest = $this->tmpFileManager->createTemporaryFile(self::TMP_FILE_SCOPE, 'swfrender');
     try {
         // swfrender may change the output filename given the format
         $tmpDest = $this->container['swftools.flash-file']->render($source->getFile()->getPathname(), $tmpDest);
         $this->tmpFileManager->add($tmpDest, self::TMP_FILE_SCOPE);
         $image = $this->container['imagine']->open($tmpDest);
         if ($spec->getWidth() && $spec->getHeight()) {
             $box = $this->boxFromSize($spec, $image->getSize()->getWidth(), $image->getSize()->getHeight());
             if (null !== $box) {
                 if ($spec->getResizeMode() == Image::RESIZE_MODE_OUTBOUND) {
                     /* @var $image \Imagine\Gmagick\Image */
                     $image = $image->thumbnail($box, ImageInterface::THUMBNAIL_OUTBOUND);
                 } else {
                     $image = $image->resize($box);
                 }
             }
         }
         $options = array('quality' => $spec->getQuality(), 'resolution-units' => $spec->getResolutionUnit(), 'resolution-x' => $spec->getResolutionX(), 'resolution-y' => $spec->getResolutionY(), 'disable-alpha' => $spec->isFlatten());
         $image->save($dest, $options);
         unset($image);
         $this->tmpFileManager->clean(self::TMP_FILE_SCOPE);
     } catch (BinaryAdapterException $e) {
         $this->tmpFileManager->clean(self::TMP_FILE_SCOPE);
         throw new RuntimeException('Unable to transmute flash to image due to Binary Adapter', $e->getCode(), $e);
     } catch (SwfToolsException $e) {
         $this->tmpFileManager->clean(self::TMP_FILE_SCOPE);
         throw new RuntimeException('Unable to transmute flash to image due to SwfTools', $e->getCode(), $e);
     } catch (ImagineException $e) {
         $this->tmpFileManager->clean(self::TMP_FILE_SCOPE);
         throw new RuntimeException('Unable to transmute flash to image due to Imagine', $e->getCode(), $e);
     } catch (MediaVorusException $e) {
         $this->tmpFileManager->clean(self::TMP_FILE_SCOPE);
         throw new RuntimeException('Unable to transmute flash to image due to MediaVorus', $e->getCode(), $e);
     } catch (RuntimeException $e) {
         $this->tmpFileManager->clean(self::TMP_FILE_SCOPE);
         throw $e;
     }
 }
Esempio n. 10
0
 public function execute(SpecificationInterface $spec, MediaInterface $source, $dest)
 {
     if (!$spec instanceof Image) {
         throw new SpecNotSupportedException('FFMpeg Adapter only supports Video specs');
     }
     /* @var $spec \MediaAlchemyst\Specification\Image */
     $tmpDest = $this->tmpFileManager->createTemporaryFile(self::TMP_FILE_SCOPE, 'ffmpeg', 'jpg');
     $time = (int) ($source->getDuration() * $this->parseTimeAsRatio(static::$time));
     try {
         $frame = $this->container['ffmpeg.ffmpeg']->open($source->getFile()->getPathname())->frame(TimeCode::fromSeconds($time));
         $frame->filters()->fixDisplayRatio();
         $frame->save($tmpDest);
         $image = $this->container['imagine']->open($tmpDest);
         if ($spec->getWidth() && $spec->getHeight()) {
             $box = $this->boxFromSize($spec, $image->getSize()->getWidth(), $image->getSize()->getHeight());
             if ($spec->getResizeMode() == Image::RESIZE_MODE_OUTBOUND) {
                 /* @var $image \Imagine\Gmagick\Image */
                 $image = $image->thumbnail($box, ImageInterface::THUMBNAIL_OUTBOUND);
             } else {
                 $image = $image->resize($box);
             }
         }
         $options = array('quality' => $spec->getQuality(), 'resolution-units' => $spec->getResolutionUnit(), 'resolution-x' => $spec->getResolutionX(), 'resolution-y' => $spec->getResolutionY());
         $image->save($dest, $options);
         $image = null;
         $this->tmpFileManager->clean(self::TMP_FILE_SCOPE);
     } catch (FFMpegException $e) {
         $this->tmpFileManager->clean(self::TMP_FILE_SCOPE);
         throw new RuntimeException('Unable to transmute video to image due to FFMpeg', null, $e);
     } catch (ImagineException $e) {
         $this->tmpFileManager->clean(self::TMP_FILE_SCOPE);
         throw new RuntimeException('Unable to transmute video to image due to Imagine', null, $e);
     } catch (MediaVorusException $e) {
         $this->tmpFileManager->clean(self::TMP_FILE_SCOPE);
         throw new RuntimeException('Unable to transmute video to image due to Mediavorus', null, $e);
     } catch (RuntimeException $e) {
         $this->tmpFileManager->clean(self::TMP_FILE_SCOPE);
         throw $e;
     }
 }
Esempio n. 11
0
 /**
  * Returns the MediaVorus File related to the document
  *
  * @return \MediaVorus\File
  */
 public function getFile()
 {
     return $this->media->getFile();
 }
Esempio n. 12
0
 public function execute(SpecificationInterface $spec, MediaInterface $source, $dest)
 {
     if (!$spec instanceof Image) {
         throw new SpecNotSupportedException('Imagine Adapter only supports Image specs');
     }
     if ($source->getType() !== MediaInterface::TYPE_IMAGE) {
         throw new SpecNotSupportedException('Imagine Adapter only supports Images');
     }
     try {
         if (static::$lookForEmbeddedPreview) {
             $tmpFile = $this->extractEmbeddedImage($source->getFile()->getPathname());
             if ($tmpFile instanceof MediaInterface) {
                 $source = $tmpFile;
             }
         }
         if ($source->getFile()->getMimeType() === 'application/illustrator') {
             $tmpFile = $this->tmpFileManager->createTemporaryFile(self::TMP_FILE_SCOPE, 'gs_transcoder');
             $this->container['ghostscript.transcoder']->toImage($source->getFile()->getRealPath(), $tmpFile);
             if (file_exists($tmpFile)) {
                 $source = $this->container['mediavorus']->guess($tmpFile);
             }
         } elseif ($source->getFile()->getMimeType() === 'image/tiff') {
             $image = $this->container['imagine']->open($source->getFile()->getRealPath());
             $layers = array();
             foreach ($image->layers() as $layer) {
                 $tmpFile = $this->tmpFileManager->createTemporaryFile(self::TMP_FILE_SCOPE, 'imagine-tiff-layer', pathinfo($dest, PATHINFO_EXTENSION));
                 $layer->save($tmpFile);
                 $layers[] = $tmpFile;
             }
             uasort($layers, function ($layer1, $layer2) {
                 $size1 = filesize($layer1);
                 $size2 = filesize($layer2);
                 if ($size1 == $size2) {
                     return 0;
                 }
                 return $size1 > $size2 ? -1 : 1;
             });
             $source = $this->container['mediavorus']->guess(array_shift($layers));
         } elseif (preg_match('#(application|image)\\/([a-z0-9-_\\.]*)photoshop([a-z0-9-_\\.]*)#i', $source->getFile()->getMimeType())) {
             $image = $this->container['imagine']->open($source->getFile()->getRealPath());
             foreach ($image->layers() as $layer) {
                 $tmpFile = $this->tmpFileManager->createTemporaryFile(self::TMP_FILE_SCOPE, 'imagine-photoshop-layer', 'jpg');
                 $layer->save($tmpFile);
                 if (file_exists($tmpFile)) {
                     $source = $this->container['mediavorus']->guess($tmpFile);
                 }
                 break;
             }
         }
         $image = $this->container['imagine']->open($source->getFile()->getPathname());
         if ($spec->getWidth() && $spec->getHeight()) {
             $box = $this->boxFromSize($spec, $image->getSize()->getWidth(), $image->getSize()->getHeight());
             if (null !== $box) {
                 if ($spec->getResizeMode() == Image::RESIZE_MODE_OUTBOUND) {
                     /* @var $image \Imagine\Gmagick\Image */
                     $image = $image->thumbnail($box, ImageInterface::THUMBNAIL_OUTBOUND);
                 } else {
                     $image = $image->resize($box);
                 }
             }
         }
         if (static::$autorotate && null === $spec->getRotationAngle()) {
             $image = $image->rotate($source->getOrientation());
         } elseif (null !== ($angle = $spec->getRotationAngle())) {
             $image = $image->rotate($angle);
         }
         $image->usePalette($this->palette);
         if (true == $spec->getStrip()) {
             $image = $image->strip();
         }
         $options = array('flatten' => $spec->getFlatten() && strtolower(pathinfo($dest, PATHINFO_EXTENSION)) !== 'gif', 'quality' => $spec->getQuality(), 'resolution-units' => $spec->getResolutionUnit(), 'resolution-x' => $spec->getResolutionX(), 'resolution-y' => $spec->getResolutionY());
         $image->save($dest, $options);
         $this->tmpFileManager->clean(self::TMP_FILE_SCOPE);
     } catch (MediaVorusException $e) {
         $this->tmpFileManager->clean(self::TMP_FILE_SCOPE);
         throw new RuntimeException('Unable to transmute image to image due to Mediavorus', null, $e);
     } catch (PHPExiftoolException $e) {
         $this->tmpFileManager->clean(self::TMP_FILE_SCOPE);
         throw new RuntimeException('Unable to transmute image to image due to PHPExiftool', null, $e);
     } catch (ImagineException $e) {
         $this->tmpFileManager->clean(self::TMP_FILE_SCOPE);
         throw new RuntimeException('Unable to transmute image to image due to Imagine', null, $e);
     } catch (RuntimeException $e) {
         $this->tmpFileManager->clean(self::TMP_FILE_SCOPE);
         throw $e;
     }
 }
Esempio n. 13
0
 public function execute(SpecificationInterface $spec, MediaInterface $source, $dest)
 {
     if (!$spec instanceof Video) {
         throw new SpecNotSupportedException('FFMpeg Adapter only supports Video specs');
     }
     try {
         $video = $this->container['ffmpeg.ffmpeg']->open($source->getFile()->getPathname());
     } catch (FFMpegException $e) {
         throw new RuntimeException('Unable to transmute video to video due to FFMpeg', null, $e);
     }
     /* @var $spec \MediaAlchemyst\Specification\Video */
     $format = $this->getFormatFromFileType($dest);
     $resizeMode = ResizeFilter::RESIZEMODE_FIT;
     if ($spec->getResizeMode()) {
         $resizeMode = $spec->getResizeMode();
     }
     if (true === static::$autorotate && method_exists($source, 'getOrientation')) {
         switch ($source->getOrientation()) {
             case MediaVorusVideo::ORIENTATION_90:
                 $video->addFilter(new RotateFilter(RotateFilter::ROTATE_90));
                 break;
             case MediaVorusVideo::ORIENTATION_270:
                 $video->addFilter(new RotateFilter(RotateFilter::ROTATE_270));
                 break;
             case MediaVorusVideo::ORIENTATION_180:
                 $video->addFilter(new RotateFilter(RotateFilter::ROTATE_180));
                 break;
             default:
                 break;
         }
     }
     $video->addFilter(new SynchronizeFilter());
     if ($source->getWidth() > $spec->getWidth() || $source->getHeight() > $spec->getHeight()) {
         $video->addFilter(new ResizeFilter(new Dimension($spec->getWidth(), $spec->getHeight()), $resizeMode));
     }
     if ($spec->getAudioCodec()) {
         $format->setAudioCodec($spec->getAudioCodec());
     }
     if ($spec->getVideoCodec()) {
         $format->setVideoCodec($spec->getVideoCodec());
     }
     if ($spec->getAudioSampleRate()) {
         $video->addFilter(new AudioResamplableFilter($spec->getAudioSampleRate()));
     }
     if ($spec->getAudioKiloBitrate()) {
         $format->setAudioKiloBitrate($spec->getAudioKiloBitrate());
     }
     if ($spec->getKiloBitrate()) {
         $format->setKiloBitrate($spec->getKiloBitrate());
     }
     if ($spec->getFramerate() && $spec->getGOPSize()) {
         $video->addFilter(new FrameRateFilter(new FrameRate($spec->getFramerate()), $spec->getGOPSize()));
     }
     try {
         $video->save($format, $dest);
         if ($format instanceof X264) {
             $this->container['mp4box']->process($dest);
         }
     } catch (FFMpegException $e) {
         throw new RuntimeException('Unable to transmute video to video due to FFMpeg', null, $e);
     } catch (MP4BoxException $e) {
         throw new RuntimeException('Unable to transmute video to video due to MP4Box', null, $e);
     }
     return $this;
 }
Esempio n. 14
0
 public function substitute_subdef($name, MediaInterface $media, Application $app, $adapt = true)
 {
     $newfilename = $this->record_id . '_0_' . $name . '.' . $media->getFile()->getExtension();
     if ($name == 'document') {
         $baseprefs = $this->get_databox()->get_sxml_structure();
         $pathhd = p4string::addEndSlash((string) $baseprefs->path);
         $filehd = $this->get_record_id() . "_document." . strtolower($media->getFile()->getExtension());
         $pathhd = databox::dispatch($app['filesystem'], $pathhd);
         $app['filesystem']->copy($media->getFile()->getRealPath(), $pathhd . $filehd, true);
         $subdefFile = $pathhd . $filehd;
         $meta_writable = true;
     } else {
         $type = $this->isStory() ? 'image' : $this->get_type();
         $subdef_def = $this->get_databox()->get_subdef_structure()->get_subdef($type, $name);
         if ($this->has_subdef($name) && $this->get_subdef($name)->is_physically_present()) {
             $path_file_dest = $this->get_subdef($name)->get_pathfile();
             $this->get_subdef($name)->remove_file();
             $this->clearSubdefCache($name);
         } else {
             $path = databox::dispatch($app['filesystem'], $subdef_def->get_path());
             $app['filesystem']->mkdir($path, 0750);
             $path_file_dest = $path . $newfilename;
         }
         if ($adapt) {
             try {
                 $app['media-alchemyst']->turnInto($media->getFile()->getRealPath(), $path_file_dest, $subdef_def->getSpecs());
             } catch (\MediaAlchemyst\Exception\ExceptionInterface $e) {
                 return $this;
             }
             $subdefFile = $path_file_dest;
         } else {
             $app['filesystem']->copy($media->getFile()->getRealPath(), $path_file_dest);
             $subdefFile = $path_file_dest;
         }
         $meta_writable = $subdef_def->meta_writeable();
     }
     $app['filesystem']->chmod($subdefFile, 0760);
     $media = $app->getMediaFromUri($subdefFile);
     $subdef = media_subdef::create($app, $this, $name, $media);
     $subdef->set_substituted(true);
     $this->delete_data_from_cache(self::CACHE_SUBDEFS);
     if ($meta_writable) {
         $this->write_metas();
     }
     if ($name == 'document' && $adapt) {
         $this->rebuild_subdefs();
     }
     return $this;
 }
Esempio n. 15
0
 public static function create(Application $app, record_Interface $record, $name, MediaInterface $media)
 {
     $databox = $record->get_databox();
     $connbas = $databox->get_connection();
     $path = $media->getFile()->getPath();
     $newname = $media->getFile()->getFilename();
     $params = [':path' => $path, ':file' => $newname, ':width' => 0, ':height' => 0, ':mime' => $media->getFile()->getMimeType(), ':size' => $media->getFile()->getSize(), ':dispatched' => 1];
     if (method_exists($media, 'getWidth') && null !== $media->getWidth()) {
         $params[':width'] = $media->getWidth();
     }
     if (method_exists($media, 'getHeight') && null !== $media->getHeight()) {
         $params[':height'] = $media->getHeight();
     }
     try {
         $sql = 'SELECT subdef_id FROM subdef
                 WHERE record_id = :record_id AND name = :name';
         $stmt = $connbas->prepare($sql);
         $stmt->execute([':record_id' => $record->get_record_id(), ':name' => $name]);
         $row = $stmt->fetch(PDO::FETCH_ASSOC);
         $stmt->closeCursor();
         if (!$row) {
             throw new \Exception_Media_SubdefNotFound('Require the real one');
         }
         $sql = "UPDATE subdef\n              SET path = :path, file = :file\n                  , width = :width , height = :height, mime = :mime\n                  , size = :size, dispatched = :dispatched, updated_on = NOW()\n              WHERE subdef_id = :subdef_id";
         $params[':subdef_id'] = $row['subdef_id'];
     } catch (\Exception_Media_SubdefNotFound $e) {
         $sql = "INSERT INTO subdef\n              (record_id, name, path, file, width\n                , height, mime, size, dispatched, created_on, updated_on)\n              VALUES (:record_id, :name, :path, :file, :width, :height\n                , :mime, :size, :dispatched, NOW(), NOW())";
         $params[':record_id'] = $record->get_record_id();
         $params[':name'] = $name;
     }
     $stmt = $connbas->prepare($sql);
     $stmt->execute($params);
     $stmt->closeCursor();
     $subdef = new self($app, $record, $name);
     $subdef->delete_data_from_cache();
     if ($subdef->get_permalink() instanceof media_Permalink_Adapter) {
         $subdef->get_permalink()->delete_data_from_cache();
     }
     unset($media);
     return $subdef;
 }