PHP 图像缩放 - 指数比例

3
我正在尝试计算12张图片之间的缩放效果。每张图片比前一张大100%。它接近完美,但在图像之间的过渡处存在问题。每个图像之间的缩放不流畅。请观看视频:http://youtu.be/dUBbDjewpO0 我认为指数表达式pow()由于某种原因不正确。这是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');

视频已离线... - Bergrebell
这是一个缩放效果 https://www.youtube.com/watch?v=76nR2oAlIWI&t=3s 我已经用After Effects解决了所有问题。 - dazzafact
你可以查看这个fiddle链接: https://codepen.io/osublake/pen/VMyeqZ (这不是我的,但我可以想象这可能是你在寻找的内容) - Bergrebell
1个回答

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);

你能联系我吗?你在为钱工作吗?我需要在下面代码中实现贝塞尔函数(平滑进入和平滑退出)。 - dazzafact

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接