Creating a reflection : Create and modify images : Graphics Image PHP Source Code


PHP Source Code » Graphics Image » Create and modify images »

 

Creating a reflection


The reflection is created by flipping the image and overlaying a gradient on it. Then both, the original image and the reflection is overlayed on a canvas. This example is created for Imagick 2.1.x but with a little tuning it should work with earlier versions.

The source image:

source

And the result:

result

<?php
/* Read the image */
$im = new Imagick"strawberry.png" );
/* Thumbnail the image */
$im->thumbnailImage200null );
/* Create a border for the image */
$im->borderImage"white"5);
/* Clone the image and flip it */
$reflection $im->clone();
$reflection->flipImage();
/* Create gradient. It will be overlayd on the reflection */
$gradient = new Imagick();
/* Gradient needs to be large enough for the image
and the borders */
$gradient->newPseudoImage$reflection->getImageWidth() + 10,
                           
$reflection->getImageHeight() + 10,
                           
"gradient:transparent-black"
                        
);
/* Composite the gradient on the reflection */
$reflection->compositeImage$gradientimagick::COMPOSITE_OVER0);
/* Add some opacity */
$reflection->setImageOpacity0.3 );
/* Create empty canvas */
$canvas = new Imagick();
/* Canvas needs to be large enough to hold the both images */
$width $im->getImageWidth() + 40;
$height = ( $im->getImageHeight() * ) + 30;
$canvas->newImage$width$height"black""png" );
/* Composite the original image and the reflection on the canvas */
$canvas->compositeImage$imimagick::COMPOSITE_OVER2010 );
$canvas->compositeImage$reflectionimagick::COMPOSITE_OVER,
                        
20$im->getImageHeight() + 10 );
/* Output the image*/
header"Content-Type: image/png" );
echo 
$canvas;
?>




HTML code for linking to this page:

Follow Navioo On Twitter

PHP Source Code

 Navioo Graphics Image
» Create and modify images