PHP Image Zoom - Exponential Scale

I am trying to calculate the effect of scaling between 12 images. Each image is 100% larger than before. It's getting closer to perfection, but there is only a problem when switching between images. This is not fluid scaling between each image. Watch the video: http://youtu.be/dUBbDjewpO0

I think the exponent expression pow () is not a coorect for some reason. Here is a PHP script, but I cannot find the problem:

 <?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;
 }


 ?>

      

rendering good

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

      

+3


source to share


1 answer


The problem comes from the fact that you are adding the delta to the scale instead of multiplying it by a constant sum in each frame:

$currentScale += $deltaScale;

      

Exponential zoom means that you are increasing the zoom by a constant factor (not a difference) for a given constant time, so you need to change this line to:

$currentScale *= $deltaScale;

      

and also calculate $deltaScale

differently:



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

      

This will calculate the fractional power of the scale difference between the images, so when you multiply it by the value $currentScale

$totalFrames / ($imageCount-1)

(the number of frames you show between the current scale and the next scale), the result is an increase in $nextScale / $currentScale

.

Relief:

Since scaling has a constant speed for the entire animation, it $deltaScale

is constant all the time, so you can calculate it outside of the loop like this:

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

      

+1


source







All Articles