Created
December 10, 2008 14:50
-
-
Save mgng/34339 to your computer and use it in GitHub Desktop.
GD + Imagick = morphing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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