PHP Image Zoom - 指数级

标签 php image math zooming exponential

我正在尝试计算 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');

最佳答案

问题来自这样一个事实,即您在比例尺上添加了一个增量,而不是每帧将其乘以一个常数:

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

关于PHP Image Zoom - 指数级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30386117/

相关文章:

image - 隐藏的Matlab TIFF 品牌?

java - 我应该怎么做才能使用 Imageio JAI 加载 DicomImage?

java - 如何创建视差图?

php - RedBean PHP 不插入两个表

while循环中的PHP mysqli_multi_query事务

php - 将数据插入到具有外键字段的php表中

java - 通用类型扩展 Number、计算

math - 使用自定义(大圆)距离的 Voronoi 图

math - float 学有问题吗?

移动站点后 PHP 404