Merge branch 'retrojunk/master'

This commit is contained in:
Romain Neutron 2013-05-04 22:13:10 +02:00
commit ccf9f26c64
4 changed files with 52 additions and 28 deletions

View file

@ -332,9 +332,8 @@ class FFMpeg extends Binary
if ($originalHeight !== null && $originalWidth !== null) { if ($originalHeight !== null && $originalWidth !== null) {
$dimensions = $format->getComputedDimensions($originalWidth, $originalHeight); $dimensions = $format->getComputedDimensions($originalWidth, $originalHeight);
$width = $this->getMultiple($dimensions->getWidth(), $format->getModulus());
$width = $this->getMultiple($dimensions->getWidth(), 16); $height = $this->getMultiple($dimensions->getHeight(), $format->getModulus());
$height = $this->getMultiple($dimensions->getHeight(), 16);
$builder->add('-s')->add($width . 'x' . $height); $builder->add('-s')->add($width . 'x' . $height);
} }
@ -455,31 +454,15 @@ class FFMpeg extends Binary
*/ */
protected function getMultiple($value, $multiple) protected function getMultiple($value, $multiple)
{ {
$modulo = $value % $multiple; if (($value % $multiple) === 0){
return $value;
$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 ($ret < $multiple) { do {
$ret = (int) $multiple; $value++;
} } while (($value % $multiple) != 0);
return (int) $ret; return $value;
} }
/** /**

View file

@ -24,6 +24,8 @@ abstract class DefaultVideo extends DefaultAudio implements Interactive, Resampl
{ {
const RESIZEMODE_FIT = 'fit'; const RESIZEMODE_FIT = 'fit';
const RESIZEMODE_INSET = 'inset'; const RESIZEMODE_INSET = 'inset';
const RESIZEMODE_SCALE_WIDTH = 'width';
const RESIZEMODE_SCALE_HEIGHT = 'height';
protected $width; protected $width;
protected $height; protected $height;
@ -32,6 +34,7 @@ abstract class DefaultVideo extends DefaultAudio implements Interactive, Resampl
protected $videoCodec; protected $videoCodec;
protected $GOPsize = 25; protected $GOPsize = 25;
protected $kiloBitrate = 1000; protected $kiloBitrate = 1000;
protected $modulus = 16;
/** /**
* Returns the width setting. * Returns the width setting.
@ -82,9 +85,18 @@ abstract class DefaultVideo extends DefaultAudio implements Interactive, Resampl
*/ */
public function getComputedDimensions($originalWidth, $originalHeight) public function getComputedDimensions($originalWidth, $originalHeight)
{ {
$originalRatio = $originalWidth / $originalHeight;
switch ($this->getResizeMode()) { 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: case self::RESIZEMODE_INSET:
$originalRatio = $originalWidth / $originalHeight;
$targetRatio = $this->width / $this->height; $targetRatio = $this->width / $this->height;
if ($targetRatio > $originalRatio) { if ($targetRatio > $originalRatio) {
@ -119,11 +131,11 @@ abstract class DefaultVideo extends DefaultAudio implements Interactive, Resampl
*/ */
public function setResizeMode($mode) 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( throw new InvalidArgumentException(
'Resize mode `%s` is not valid , avalaible values are %s', 'Resize mode `%s` is not valid , avalaible values are %s',
$mode, $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; 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;
}
} }

View file

@ -33,4 +33,6 @@ interface Resizable extends VideoInterface
* @return Dimension A dimension * @return Dimension A dimension
*/ */
public function getComputedDimensions($originalWidth, $originalHeight); public function getComputedDimensions($originalWidth, $originalHeight);
public function getModulus();
} }

View file

@ -52,4 +52,9 @@ class X264 extends DefaultVideo
{ {
return 2; return 2;
} }
public function getModulus()
{
return 2;
}
} }