A fellow called kakapo asked me to create a button with Imagick. He had an image of the button and a Photoshop tutorial but unfortunately the tutorial was in Chinese. My Chinese is a bit rusty so it will take a little longer to create that specific button
The button in this example is created after this tutorial http://xeonfx.com/tutorials/easy-button-tutorial/ (yes, I googled "easy button tutorial"). The code and the button it creates are both very simple but the effect looks really nice.
Here we go with the code:
-
<?php
-
-
/* Create a new Imagick object */
-
$im = new Imagick();
-
-
/* Create empty canvas */
-
$im->newImage( 200, 200, "white", "png" );
-
-
/* Create the object used to draw */
-
$draw = new ImagickDraw();
-
-
/* Set the button color.
-
Changing this value changes the color of the button */
-
$draw->setFillColor( "#4096EE" );
-
-
/* Create the outer circle */
-
$draw->circle( 50, 50, 70, 70 );
-
-
/* Create the smaller circle on the button */
-
$draw->setFillColor( "white" );
-
-
/* Semi-opaque fill */
-
$draw->setFillAlpha( 0.2 );
-
-
/* Draw the circle */
-
$draw->circle( 50, 50, 68, 68 );
-
-
/* Set the font */
-
$draw->setFont( "./test1.ttf" );
-
-
/* This is the alpha value used to annotate */
-
$draw->setFillAlpha( 0.17 );
-
-
/* Draw a curve on the button with 17% opaque fill */
-
) );
-
-
/* Render all pending operations on the image */
-
$im->drawImage( $draw );
-
-
/* Set fill to fully opaque */
-
$draw->setFillAlpha( 1 );
-
-
/* Set the font size to 30 */
-
$draw->setFontSize( 30 );
-
-
/* The text on the */
-
$draw->setFillColor( "white" );
-
-
/* Annotate the text */
-
$im->annotateImage( $draw, 38, 55, 0, "go" );
-
-
/* Trim extra area out of the image */
-
$im->trimImage( 0 );
-
-
/* Output the image */
-
echo $im;
-
-
?>
And here is a few buttons I created by changing the fill color value:



#1 by m0s on November 21, 2007 - 2:04 pm
Quote
Nice example
I got some fatal errors running this example on windows (php 5.2.5) though.
The third parameter for ->newImage() should not be string but should be
an instance of ImagickPixel.
The same counts for the ->setFillColor() calls.
The fourth parameter is not valid anymore. And should be set with a seperate
call to ->setImageFormat(‘png’);
Maybe it helps someone.
#2 by Mikko Koppanen on November 21, 2007 - 2:11 pm
Quote
m0s,
the newest examples I have done are for Imagick 2.1.x series. Starting from version 2.1.0 Imagick will allow a color string also. I should propably mention that there..
#3 by Thijs Lensselink on November 21, 2007 - 3:10 pm
Quote
Thanks for clarifying that!
I will check out this new version shortly. But right now still using
the static extension from the pecl repository. Need to install ImageMagick first.
It’s a great extension. Keep up the good work!
#4 by Ace on November 22, 2007 - 9:04 am
Quote
Well dude , im glad my tutorial helped you out.
Cheers.
Ace.
Pingback: links for 2007-11-22 « Stand on the shoulders of giants
Trackback: PHPDeveloper.org
Pingback: developercast.com » Mikko Koppanen’s Blog: Creating buttons with Imagick
#5 by mark on November 27, 2007 - 9:19 am
Quote
Unfortunately, selecting a ttf as shown in your example doesn’t seem to work…
#6 by Mikko Koppanen on November 27, 2007 - 12:57 pm
Quote
mark,
“does not work” never was and never will be a proper description of an error.
#7 by open source cms on November 29, 2007 - 11:32 pm
Quote
You have great articles!
#8 by kakapo on November 30, 2007 - 9:53 am
Quote
Hi, Mikko. Can you help me to built a image like this one :http://www.imagemagick.org/Usage/advanced/star_field.gif
#9 by Mark on December 14, 2007 - 10:48 am
Quote
Good call, bad description of the error…let me rephrase. Irrespective of what font I call it always ends up being arial (or whatever the default font is). I’ve debugged the path to the ttf and it’s being found by php, so I can’t figure out what the prob could be. Perhaps it’s a Vista issue (my dev env is a vista laptop) pr perhaps it’s that I’m using the latest version on php 5 and the latest version of imagemagick w/ your latest module dll. Things “mostly work”, but many don’t — example: can only draw a solid line — dotted line does work — only shows up as solid line.
#10 by Bytes on December 15, 2007 - 8:43 am
Quote
Hello, Mikko. I want to fill a round rectangle with gradient colors, is there a method?
#11 by Mikko Koppanen on December 15, 2007 - 1:11 pm
Quote
Bytes,
see this http://imagemagick.org/discourse-server/viewtopic.php?f=6&t=8943&sid=4e45d7d3485398947cdac4c96cb06f26
#12 by Bytes on December 16, 2007 - 4:19 am
Quote
Thank you, it seems that I have to realize it by myself.
#13 by Carmen on December 18, 2007 - 1:03 am
Quote
Hello Miko,
I was trying to find some help for my new project with imagick in php5 and I found your blog with great imagick examples.
I am trying to create a button with a repeating background image.
Also I have been trying for a while to write some text into an image, but nothing is happening. I was using a lot of technics but nothing worked. I can’t say whay. Do you have you any ideea?
Any hint would be greatly appreciated.
Kind regards,
Carmen
#14 by Mikko Koppanen on December 18, 2007 - 1:06 am
Quote
Carmen,
for tiling try; $im->newPseudoImage( 600, 600, “tile:test.png” );. I can’t really help you with the text issue, “nothing is happening” and “using a lot of technics” does not really tell anything about the issue or what you have tried.
#15 by M.Zeb on July 27, 2008 - 4:38 am
Quote
Hi,
Good work Mikko, well, it worked perfect on my local machine but on my live machine it gives the following error:
Fatal error: Uncaught exception ‘ImagickException’ in /home/****/public_html/gick.php:40 Stack trace: #0 /home/**/public_html/gick.php(40): Imagick->annotateimage(Object(ImagickDraw), 38, 55, 0, ‘go’) #1 {main} thrown in /home/**/public_html/gick.php on line
Can you please help me in this?
#16 by Kazelbsm on August 5, 2008 - 7:49 am
Quote
Hi webmaster!
#17 by Kazelkpn on August 5, 2008 - 8:06 am
Quote
Hi webmaster!
#18 by program forgiveness nursing loan student on January 7, 2009 - 12:03 pm
Quote
student forgiveness nursing programs loan nursing loan programs forgiveness student
Pingback: Imagick blog » Blog Archive » Creating buttons with Imagick