Created
August 28, 2013 13:00
-
-
Save teslitsky/6365775 to your computer and use it in GitHub Desktop.
Image perspective transformation with Imagick
This file contains hidden or 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 | |
| /* Main image part */ | |
| $main = new Imagick(DIR_IMAGE . $old_image); | |
| $main->setImageMatte(true); | |
| $main->cropimage($main->getImageWidth() * 0.9, $main->getImageHeight(), $main->getImageWidth() * 0.1, 0); | |
| $main->setimagebackgroundcolor("#ffffff"); | |
| $main->setImageVirtualPixelMethod(imagick::VIRTUALPIXELMETHOD_BACKGROUND); | |
| $pointsMain = array( | |
| 0, 0, 0, 0, | |
| 0, $main->getImageHeight() - 20, | |
| 0, $main->getImageHeight() - 5, | |
| $main->getImageWidth() - 10, 10, | |
| $main->getImageWidth() - 10, 20, | |
| $main->getImageWidth() - 10, $main->getImageHeight() * 0.9, | |
| $main->getImageWidth() - 10, $main->getImageHeight() * 0.9 | |
| ); | |
| $main->distortimage(Imagick::DISTORTION_PERSPECTIVE, $pointsMain, true); | |
| /* Left image part, 5% of width */ | |
| $left = new Imagick(DIR_IMAGE . $old_image); | |
| $left->setImageMatte(true); | |
| $left->cropimage($main->getImageWidth() * 0.1, $main->getImageHeight(), 0, 0); | |
| $left->setimagebackgroundcolor("#ffffff"); | |
| $left->setImageVirtualPixelMethod(imagick::VIRTUALPIXELMETHOD_BACKGROUND); | |
| $pointsLeft = array( | |
| // left top | |
| 0, 0, 0, $left->getImageHeight() * 0.05, | |
| // right top | |
| $left->getImageWidth(), 0, | |
| $left->getImageWidth(), 0, | |
| // left bottom | |
| 0, $left->getImageHeight(), | |
| 0, $left->getImageHeight() - $left->getImageHeight() * 0.05, | |
| // right bottom | |
| $left->getImageWidth(), $left->getImageWidth(), | |
| $left->getImageWidth(), $left->getImageWidth() | |
| ); | |
| $left->scaleimage(0, $main->getImageHeight() - 2); | |
| $left->distortimage(Imagick::DISTORTION_PERSPECTIVE, $pointsLeft, true); | |
| /* Merge image parts */ | |
| $final = new Imagick(); | |
| $final->addimage($left); | |
| $final->addimage($main); | |
| $final->resetiterator(); | |
| $final->setImageMatte(true); | |
| $combined = $final->appendimages(false); | |
| $combined->scaleimage($width, 0); | |
| $combined->writeimage(DIR_IMAGE . $new_image); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment