PHP图像缩放 - 指数刻度

时间:2015-05-21 23:37:05

标签: php image math zoom exponential

我试图计算12张图像之间的缩放效果。每张图片都比之前的图片大100%。它的接近完美,但在图像之间的转换只有一个问题。它不是每个图像之间的流体缩放。 请参阅视频:http://youtu.be/dUBbDjewpO0

我认为Exponential表达式pow()由于某种原因不是cooct。 这是PHP脚本,但我找不到问题:

 <?php
    $imageFiles=array(
     '1.jpg',
     '2.jpg',
     '3.jpg',        
     '4.jpg');
 $targetFrameRate=$targetDuration='18';
 $imageCount = count($imageFiles);
 $totalFrames        = ($targetFrameRate*$targetDuration);
 $sourceIndex  = 0;
 $firstIndex   = 1;
 $lastIndex    = $totalFrames; //==total frames
 $currentScale = 1;//image scaling for first scale 
 $deltaScale   = ((($imageCount-1)*($scaleFactor-$currentScale))/$totalFrames);

  for ($i=$firstIndex; $i<=$lastIndex; $i++) {

// prepare filename

$filename = createImageFilename($i, $imageType);

// determine source..
if ($i == $firstIndex) {
    $newSourceIndex = 0;
}
else if ($i == $lastIndex) {
    $newSourceIndex = ($imageCount-1);
}
else {
     $newSourceIndex = intval(($i*($imageCount-1))/$totalFrames);

}
// create frame..
if ($newSourceIndex != $sourceIndex) {
    $sourceIndex  = $newSourceIndex;
    $currentScale = pow($scaleFactor, $sourceIndex);
    $nextScale    = pow($scaleFactor, ($sourceIndex+1));
    $deltaScale   = ((($imageCount-1)*($nextScale-$currentScale))/$totalFrames);

    copyImage($imageFiles[$sourceIndex], 
              sprintf('%s/%s', $outputDir, $filename), 
              $imageWidth, 
              $imageHeight, 
              $imageType);
}
else {
    createImage($imageFiles[$sourceIndex], 
                sprintf('%s/%s', $outputDir, $filename), 
                ($currentScale/pow($scaleFactor, $sourceIndex)),
                $imageWidth, 
                $imageHeight, 
                $imageType);
}

//DEBUG: buffer some values for optional debug-output
if (isDebugOutputEnabled()) {
    $debug_idx[$i] = $filename;
    $debug_inf[$i] = sprintf('sourceIndex=%d , scale=%01.2f<br />', $sourceIndex, $currentScale);
}
// advance..
$currentScale += $deltaScale;
 }


 ?>

渲染很好

  shell_exec('ffmpeg -f image2 -i /var/www/htdocs/image2/i%d.jpg -s 1280x720 -movflags faststart -b:v 5500k -r 18 output.flv');

1 个答案:

答案 0 :(得分:1)

问题来自这样一个事实,即您要为您的比例添加增量而不是每帧增加一个恒定量:

$currentScale += $deltaScale;

指数变焦意味着您在给定的恒定时间内通过常量因子(非差异)增加变焦,因此您需要将该行更改为:

$currentScale *= $deltaScale;

并以不同方式计算$deltaScale

$deltaScale = pow($nextScale / $currentScale, ($imageCount-1) / $totalFrames);

这将计算图像之间的比例差异的分数幂,以便当您将其乘以$currentScale$totalFrames / ($imageCount-1)次(您在当前比例和下一个比例之间渲染的帧数)比例),结果将增加$nextScale / $currentScale

简化:

因为整个动画的缩放速度是恒定的,$deltaScale一直是常量,所以你可以在循环外计算它,如下所示:

$deltaScale = pow($scaleFactor, ($imageCount-1) / $totalFrames);
相关问题