Example #1
0
 /**
  * @param string $inFilename
  * @param Audio  $spec
  * @param string $outFilename
  *
  * @return string
  */
 public function convert($inFilename, Specification $spec, $outFilename)
 {
     $audio = $this->ffmpeg->open($inFilename);
     $format = $this->createFormat($spec->getAudioFormat());
     if ($spec->getAudioCodec()) {
         $format->setAudioCodec($spec->getAudioCodec());
     }
     if ($spec->getAudioBitrate()) {
         $format->setAudioKiloBitrate($spec->getAudioBitrate());
     }
     if ($spec->getAudioSamplerate()) {
         $audio->addFilter(new AudioResamplableFilter($spec->getAudioSamplerate()));
     }
     if ($spec->getAudioChannels()) {
         $format->setAudioChannels($spec->getAudioChannels());
     }
     $audio->save($format, $outFilename);
 }
Example #2
0
 /**
  * @param string $inFilename
  * @param Video  $spec
  * @param string $outFilename
  */
 public function convert($inFilename, Specification $spec, $outFilename)
 {
     $video = $this->ffmpeg->open($inFilename);
     $format = $this->createFormat($spec->getVideoFormat());
     $resizeMode = ResizeFilter::RESIZEMODE_FIT;
     if ($spec->getResizeMode()) {
         $resizeMode = $spec->getResizeMode();
     }
     $video->addFilter(new SynchronizeFilter());
     //if ($source->getWidth() > $spec->getWidth() || $source->getHeight() > $spec->getHeight()) {
     if ($spec->getWidth() && $spec->getHeight()) {
         $video->addFilter(new ResizeFilter(new Dimension($spec->getWidth(), $spec->getHeight()), $resizeMode));
     }
     if ($spec->getVideoCodec()) {
         $format->setVideoCodec($spec->getVideoCodec());
     }
     if ($spec->getAudioCodec()) {
         $format->setAudioCodec($spec->getAudioCodec());
     }
     if ($spec->getVideoBitrate()) {
         $format->setKiloBitrate($spec->getVideoBitrate());
     }
     if ($spec->getVideoFramerate() && $spec->getVideoGop()) {
         $video->addFilter(new FrameRateFilter(new FrameRate($spec->getVideoFramerate()), $spec->getVideoGop()));
     }
     if ($spec->getAudioBitrate()) {
         $format->setAudioKiloBitrate($spec->getAudioBitrate());
     }
     if ($spec->getAudioSamplerate()) {
         $video->addFilter(new AudioResamplableFilter($spec->getAudioSamplerate()));
     } elseif ($format instanceof Flv) {
         $video->addFilter(new AudioResamplableFilter(44100));
     }
     if ($spec->getAudioChannels()) {
         $format->setAudioChannels($spec->getAudioChannels());
     }
     $video->save($format, $outFilename);
     if ($format instanceof X264) {
         $this->mp4box->process($outFilename);
     }
 }
Example #3
0
 /**
  * @param string $inFilename
  * @param Image  $spec
  * @param string $outFilename
  */
 public function convert($inFilename, Specification $spec, $outFilename)
 {
     $image = $this->imagine->open($inFilename);
     $options = array();
     if ($spec->getFormat()) {
         $options['format'] = $spec->getFormat();
     }
     if ($spec->getQuality()) {
         if (!empty($options['format']) && $options['format'] === 'png') {
             $options['png_compression_level'] = floor($spec->getQuality() / 10);
             $options['png_compression_filter'] = $spec->getQuality() % 10;
         } elseif (!empty($options['format']) && $options['format'] === 'jpg') {
             $options['jpeg_quality'] = $spec->getQuality();
         }
     }
     if ($spec->getColorspace()) {
         $image->strip();
         switch ($spec->getColorspace()) {
             case 'cmyk':
                 $image->usePalette(new CMYK());
                 break;
             case 'grayscale':
                 $image->usePalette(new Grayscale());
                 break;
             default:
                 $image->usePalette(new RGB());
         }
     }
     if ($spec->getScale()) {
         if ($spec->getScale() === 'up') {
             // only scale up
         } elseif ($spec->getScale() === 'down') {
             // only scale down
         }
     }
     $method = $spec->getResizeMode();
     if ($method === Image::RESIZE_METHOD_WIDTH) {
         $specSize = $image->getSize()->widen($spec->getWidth());
         $image->resize($specSize);
     } elseif ($method === Image::RESIZE_METHOD_HEIGHT) {
         $specSize = $image->getSize()->heighten($spec->getHeight());
         $image->resize($specSize);
     } elseif ($method === Image::RESIZE_METHOD_EXACT) {
         $specSize = new Box($spec->getWidth(), $spec->getHeight());
         $image->resize($specSize);
     } elseif ($method === Image::RESIZE_METHOD_FIT) {
         $specSize = new Box($spec->getWidth(), $spec->getHeight());
         $image = $image->thumbnail($specSize, ImageInterface::THUMBNAIL_INSET);
     } elseif ($method === Image::RESIZE_METHOD_EXACT_FIT) {
         $specSize = new Box($spec->getWidth(), $spec->getHeight());
         $layer = $image->thumbnail($specSize, ImageInterface::THUMBNAIL_INSET);
         $layerSize = $layer->getSize();
         $palette = new RGB();
         if ($spec->getBackgroundColor()) {
             $color = $palette->color($spec->getBackgroundColor(), 100);
         } else {
             $color = $palette->color('#fff', 0);
         }
         $image = $this->imagine->create($specSize, $color);
         $image->paste($layer, new Point(floor(($specSize->getWidth() - $layerSize->getWidth()) / 2), floor(($specSize->getHeight() - $layerSize->getHeight()) / 2)));
     } elseif ($method === Image::RESIZE_METHOD_CROP) {
         $specSize = new Box($spec->getWidth(), $spec->getHeight());
         $imageSize = $image->getSize();
         if (!$specSize->contains($imageSize)) {
             $ratios = array($specSize->getWidth() / $imageSize->getWidth(), $specSize->getHeight() / $imageSize->getHeight());
             $ratio = max($ratios);
             if (!$imageSize->contains($specSize)) {
                 $imageSize = new Box(min($imageSize->getWidth(), $specSize->getWidth()), min($imageSize->getHeight(), $specSize->getHeight()));
             } else {
                 $imageSize = $imageSize->scale($ratio);
                 $image->resize($imageSize);
             }
             if ($spec->getCenterX() && $spec->getCenterY()) {
                 $point = new Point($spec->getCenterX(), $spec->getCenterY());
             } else {
                 $point = new Point(max(0, round(($imageSize->getWidth() - $specSize->getWidth()) / 2)), max(0, round(($imageSize->getHeight() - $specSize->getHeight()) / 2)));
             }
             $image->crop($point, $specSize);
         }
     }
     $image->save($outFilename, $options);
 }