I want to apply a curl or peel effect on an image using PHP and ImageMagick.
I tried various libraries in PHP, but only ImageMagick may do the same. I want the same output as here:
I want to apply a curl or peel effect on an image using PHP and ImageMagick.
I tried various libraries in PHP, but only ImageMagick may do the same. I want the same output as here:
In Fred Weinhaus' collection of advanced ImageMagick scripts there is the pagecurl script.
Out of the box, it creates the following effect on the IM-builtin wizard:
test image:
pagecurl wizard: wiz-curled.png
The script applies the effect on the lower right corner, but it can be parametrized in a number of ways:
pagecurl:
USAGE: pagecurl [-a amount] [-m mode] [-c color] [-b bgcolor] [-e ellipticity] [-x xcoord] [-y ycoord] [-g gcontrast] [-d dcontrast] infile [bgfile] outfile
USAGE: pagecurl [-help]
OPTIONS:
-a amount amount of pagecurl expressed as percent of image
width; integer>=0; default=50
-m mode mode of shading curl; plain, grad or doublegrad;
default=grad
-c color color to apply to curl; any valid IM color;
default=white
-b bgcolor background color to apply to image where curled away;
any valid IM color; default=none (transparent)
-e ellipticity curl flattening from circle to ellipse;
0<=float<1; default=0 for circle; recommended value
other 0 is 0.5 for ellipse shape
-x xcoord x coordinate for apex of curl;
default=right image edge
-y ycoord y coordinate for apex of curl;
default=upper image edge
-g gcontrast contrast adjustment for mode=grad; 0<=integer<=100;
increases contrast only; default=15
-d dcontrast contrast adjustment for mode=doublegrad;
-100<=integer<=100; positive increase contrast;
negative decreases contrast; default=0
NAME: PAGECURL
PURPOSE: Applies a pagecurl effect to the lower right corner of an image.
DESCRIPTION: PAGECURL Applies a pagecurl effect to the lower right corner
of an image. The apex of the curl is nominally in the upper right corner of
the image, but can be adjusted. The curl is always right to left. The curl
can be shaded and/or colored. The removed area can be colored, transparent
or filled with an optional same size background image if provided. Note
that this is a 2D simulation and not a true 3D effect.
OPTIONS:
-a amount ... AMOUNT of pagecurl expressed as percent of image width.
Values are in range integer>=0. The default=50.
Caution: values below about 5 may fail.
-m mode ... MODE shading on the curl.
Choices are: plain (or p), grad (or g) for gradient, or
doublegrad (or d) for double gradient. Default=grad.
-c color ... COLOR is the color to apply to curl.
Any valid IM color is allowed. The default=white.
-b bgcolor ... BGCOLOR is the color to apply to curled away part of the image.
Any valid IM color is allowed. The default=none for transparent.
If a background file is provided, bgcolor must be none.
-e ellipticity ... ELLIPTICITY is the amount of curl flattening from a circle
to an ellipse.
Values are in range 0<=float<1. The default=0 for circle.
Recommended value other 0 is 0.5 for ellipse shape.
-x xcoord ... XCOORD is the X coordinate for the apex of the curl.
Values are 0<integers<width.
The default is the right edge of the image.
-y ycoord ... YCOORD is the Y coordinate for the apex of the curl.
Values are integers.
The default is the upper edge of the image.
-g gcontrast ... GCONTRAST is the contrast adjustment for mode=grad.
Values are in range 0<=integer<=100.
This increases contrast only. The default=15
-d dcontrast ... DCONTRAST is the contrast adjustment for mode=doublegrad.
Values are in range -100<=integer<=100.
Positive values increase contrast.
Negative values decrease contrast. The default=0
Thanks to Anthony Thyssen for critiqing the original version and for
several useful suggestions for improvement.
CAVEAT: No guarantee that this script will work on all platforms,
nor that trapping of inconsistent parameters is complete and
foolproof. Use At Your Own Risk.
To get the the curling effect in the upper right corner you could do something like these:
convert wizard: -flip -frame 1 miff:- \
| pagecurl -a 15 -m grad -e 0.5 -y 510 -x 479 -c lightgray -g 33 - miff:- \
| convert - -flip curl2.png
convert wizard: -flip -frame 1 miff:- \
| pagecurl -a 20 -m grad -e 0.7 -y 490 -x 479 -c lightgray -g 5 - miff:- \
| convert - -flip curl3.png
Then the results look like this:
I would apply this effect using an overlay image (probably a PNG with alpha) - this could be done either using ImageMagick or in the rendering layer (e.g. HTML/CSS etc)
For PHP/Imagick extension, the Imagick::mergeImageLayers
method is probably useful.