Skip to content

Instantly share code, notes, and snippets.

@mgng
Created December 10, 2008 14:50
Show Gist options
  • Save mgng/34339 to your computer and use it in GitHub Desktop.
Save mgng/34339 to your computer and use it in GitHub Desktop.
GD + Imagick = morphing
<?php
/**
* GD+Imagick = morphing
*
* @param string $fromPath
* @param string $toPath
* @param string $outPath
* @param integer $frame OPTIONAL
* @param integer $delay OPTIONAL
*/
function morph($fromPath, $toPath, $outPath, $frame=10, $delay=1) {
$im = new Imagick();
$gf = imagecreatefromjpeg($fromPath);
$gt = imagecreatefromjpeg($toPath);
$sz = getimagesize($toPath);
$wkImg = '__work__.jpg';
for($i=1; $i<=$frame; $i++) {
imagecopymerge($gf, $gt, 0,0, 0,0, $sz[0],$sz[1], (100/$frame)*$i);
imagejpeg($gf, $wkImg);
$tmp = new Imagick($wkImg);
$tmp->setFormat('gif');
$tmp->setImageDelay($delay*100);
$im->addImage($tmp);
$tmp->destroy();
unlink($wkImg);
}
$im->writeImages($outPath, true);
$im->destroy();
imagedestroy($gf);
imagedestroy($gt);
}
// ex
morph('a.jpg', 'b.jpg', 'out.gif', 10, 0.1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment