From d7c7e74c255ff36d69dc7c53c33f1bab982fb842 Mon Sep 17 00:00:00 2001 From: zboyet Date: Tue, 16 Apr 2013 11:18:54 -0700 Subject: [PATCH 1/4] Added ability to choose between resolution modulus --- src/FFMpeg/FFMpeg.php | 33 ++++++++------------------------- 1 file changed, 8 insertions(+), 25 deletions(-) diff --git a/src/FFMpeg/FFMpeg.php b/src/FFMpeg/FFMpeg.php index 9caeff3..fd0a4e2 100644 --- a/src/FFMpeg/FFMpeg.php +++ b/src/FFMpeg/FFMpeg.php @@ -332,9 +332,8 @@ class FFMpeg extends Binary if ($originalHeight !== null && $originalWidth !== null) { $dimensions = $format->getComputedDimensions($originalWidth, $originalHeight); - - $width = $this->getMultiple($dimensions->getWidth(), 16); - $height = $this->getMultiple($dimensions->getHeight(), 16); + $width = $this->getMultiple($dimensions->getWidth(), $format->getModulus()); + $height = $this->getMultiple($dimensions->getHeight(), $format->getModulus()); $builder->add('-s')->add($width . 'x' . $height); } @@ -455,31 +454,15 @@ class FFMpeg extends Binary */ protected function getMultiple($value, $multiple) { - $modulo = $value % $multiple; - - $ret = (int) $multiple; - - $halfDistance = $multiple / 2; - if ($modulo <= $halfDistance) - $bound = 'bottom'; - else - $bound = 'top'; - - switch ($bound) { - default: - case 'top': - $ret = $value + $multiple - $modulo; - break; - case 'bottom': - $ret = $value - $modulo; - break; + if (($value % $multiple) === 0){ + return $value; } - if ($ret < $multiple) { - $ret = (int) $multiple; - } + do { + $value++; + } while (($value % $multiple) != 0); - return (int) $ret; + return $value; } /** From 59eb6f35413937103928ecdef1bf2696c81980d6 Mon Sep 17 00:00:00 2001 From: zboyet Date: Tue, 16 Apr 2013 11:22:49 -0700 Subject: [PATCH 2/4] Added resolution modulus and width or height scale Added the ability to select the resolution modulus and a scale mode based on width or height --- src/FFMpeg/Format/Video/DefaultVideo.php | 40 ++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/src/FFMpeg/Format/Video/DefaultVideo.php b/src/FFMpeg/Format/Video/DefaultVideo.php index 3df5d73..47b0210 100644 --- a/src/FFMpeg/Format/Video/DefaultVideo.php +++ b/src/FFMpeg/Format/Video/DefaultVideo.php @@ -24,6 +24,8 @@ abstract class DefaultVideo extends DefaultAudio implements Interactive, Resampl { const RESIZEMODE_FIT = 'fit'; const RESIZEMODE_INSET = 'inset'; + const RESIZEMODE_SCALE_WIDTH = 'width'; + const RESIZEMODE_SCALE_HEIGHT = 'height'; protected $width; protected $height; @@ -32,6 +34,7 @@ abstract class DefaultVideo extends DefaultAudio implements Interactive, Resampl protected $videoCodec; protected $GOPsize = 25; protected $kiloBitrate = 1000; + protected $modulus = 16; /** * Returns the width setting. @@ -82,9 +85,18 @@ abstract class DefaultVideo extends DefaultAudio implements Interactive, Resampl */ public function getComputedDimensions($originalWidth, $originalHeight) { + $originalRatio = $originalWidth / $originalHeight; + switch ($this->getResizeMode()) { + case self::RESIZEMODE_SCALE_WIDTH: + $height = $this->height; + $width = round($originalRatio * $this->height); + break; + case self::RESIZEMODE_SCALE_HEIGHT: + $width = $this->width; + $height = round($this->width / $originalRatio); + break; case self::RESIZEMODE_INSET: - $originalRatio = $originalWidth / $originalHeight; $targetRatio = $this->width / $this->height; if ($targetRatio > $originalRatio) { @@ -119,11 +131,11 @@ abstract class DefaultVideo extends DefaultAudio implements Interactive, Resampl */ public function setResizeMode($mode) { - if ( ! in_array($mode, array(self::RESIZEMODE_FIT, self::RESIZEMODE_INSET))) { + if ( ! in_array($mode, array(self::RESIZEMODE_FIT, self::RESIZEMODE_INSET, self::RESIZEMODE_SCALE_WIDTH, self::RESIZEMODE_SCALE_HEIGHT))) { throw new InvalidArgumentException( 'Resize mode `%s` is not valid , avalaible values are %s', $mode, - implode(', ', array(self::RESIZEMODE_FIT, self::RESIZEMODE_INSET)) + implode(', ', array(self::RESIZEMODE_FIT, self::RESIZEMODE_INSET, self::RESIZEMODE_SCALE_WIDTH, self::RESIZEMODE_SCALE_HEIGHT)) ); } @@ -230,4 +242,26 @@ abstract class DefaultVideo extends DefaultAudio implements Interactive, Resampl { return 1; } + + /** + * Used to determine what resolutions sizes are valid. + * + * @param int $value + */ + public function setModulus($value) + { + if(!in_array($value, array(2, 4, 8, 16))){ + throw new InvalidArgumentException('Wrong modulus division value. Valid values are 2, 4, 8 or 16'); + } + + $this->modulus = $value; + } + + /** + * @return int + */ + public function getModulus() + { + return $this->modulus; + } } From cb51cfafb32129d39ebc42b89e373f61761a0074 Mon Sep 17 00:00:00 2001 From: zboyet Date: Tue, 16 Apr 2013 11:23:51 -0700 Subject: [PATCH 3/4] Added ability to choose between resolution modulus --- src/FFMpeg/Format/Video/Resizable.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/FFMpeg/Format/Video/Resizable.php b/src/FFMpeg/Format/Video/Resizable.php index 63289d6..aabedd9 100644 --- a/src/FFMpeg/Format/Video/Resizable.php +++ b/src/FFMpeg/Format/Video/Resizable.php @@ -33,4 +33,6 @@ interface Resizable extends VideoInterface * @return Dimension A dimension */ public function getComputedDimensions($originalWidth, $originalHeight); + + public function getModulus(); } From a4e205253d75822f8bd9a377feb600fee40c3491 Mon Sep 17 00:00:00 2001 From: zboyet Date: Tue, 16 Apr 2013 11:24:29 -0700 Subject: [PATCH 4/4] Added ability to choose between resolution modulus --- src/FFMpeg/Format/Video/X264.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/FFMpeg/Format/Video/X264.php b/src/FFMpeg/Format/Video/X264.php index fd57231..f2d4018 100644 --- a/src/FFMpeg/Format/Video/X264.php +++ b/src/FFMpeg/Format/Video/X264.php @@ -52,4 +52,9 @@ class X264 extends DefaultVideo { return 2; } + + public function getModulus() + { + return 2; + } }