drop shadow

PHP Image Magick Drop Shadow

Want to add a simple drop shadow on any image? well here’s how to do it on the fly via PHP and Image Magick.

< ?php $img = new Imagick('images/image.jpg'); $img->setImageFormat('png');
    $shadowed = $img->clone();
    $drop_shadow = $img->clone();
    $drop_shadow->setImageBackgroundColor( new ImagickPixel( 'black' ) );
    $drop_shadow->shadowImage( 80, 3, 5, 5 );
    $drop_shadow->compositeImage( $img, Imagick::COMPOSITE_OVER, 0, 0 );
    header( "Content-Type: image/jpeg" );
    echo $drop_shadow; 
?>

Here’s how to do same via PHP and Image Magick but writing the file out to a second image leaving your original alone.

<?php $filename_shdw = 'images/image_shadow.jpg'; if (!file_exists($filename_shdw)) { $img = new Imagick('images/image.jpg'); $img->setImageFormat('png');
    $shadowed = $img->clone();
    $drop_shadow = $img->clone();
    $drop_shadow->setImageBackgroundColor( new ImagickPixel( 'black' ) );
    $drop_shadow->shadowImage( 80, 3, 5, 5 );
    $drop_shadow->compositeImage( $img, Imagick::COMPOSITE_OVER, 0, 0 );
    $drop_shadow->writeImage( 'images/image_shadow.jpg' );
}
    echo ''; 
?>

these snippets are very simple, you can expand them, include them in your own code, randomize the drop shadow etc. I will expand this in another post.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *