I’ve been coding Imagick for some time now and I have no idea how people are using it. It’s time to hear about you. I would like to hear how you are using Imagick, how well does it work in our scenario and what examples you would like to see in my blog.
Leave comments and help me to help you (sounds like a cliche, I know)
#1 by Imran Nazar on September 30, 2007 - 5:16 pm
Quote
I’ve been using Imagick for a good while, primarily in places where speed is an important factor. GD isn’t generally up to the job of, for example, rendering a translucent masked image very quickly; Imagick cleans things up and does it quickly.
If you could throw some examples of simple operations in both gd and Imagick online, that’d be great. (Of course, you’d have to learn gd, which is awful messy)
#2 by Mark on October 2, 2007 - 8:46 pm
Quote
Hey Mikko,
at this moment i use imagick for image transformation (converting into other formats, resizing, resampling …). I could say, i really enjoy your work.
I’m glad to see that this (your) website is the only i could find which shows some samples for the imagick php5 module.
I would like to see some more documentation in general. It would be very nice to see the constants documented. The php.net docs are enough to get an overview about imagicks (imagemagicks) features and your implimentation in the module, but to get a feeling about the handling it’s not enough.
Thank you for your work.
#3 by Mikko Koppanen on October 2, 2007 - 8:59 pm
Quote
Hey Marc,
you are absolutely right. We really need to improve the documentation on php.net. The constants are currently documented in http://fi2.php.net/manual/en/imagick.constants.php.
We still need to add links to constants from each method that uses a constant. It is a slow process but we are getting there
#4 by Devo on October 9, 2007 - 5:10 am
Quote
Hey Mikko,
I’ve been converting all of my old GD toys over to Imagick. I use it for things like creating thumbnails, cropping images, displaying weather data (current conditions graphic as the background, wind direction compass and weather info overlaid), dashboard gauges to display numerical information, GIS map data plotting, creating charts, and creating id badges.. to name a few just glancing over my projects directory.
All of those things just use basic functions in different ways, nothing really complicated, just a lot of little things.
Next, I’d like to try gif animation, but so far I have not figured it out on my own.
If you have time, would you blog about creating animated gifs from scratch? And maybe an example for using morphImages?
Thanks for your time,
-Z
#5 by zaadjis on October 9, 2007 - 10:35 am
Quote
An example using fonts would be great, a CAPTCHA perhaps (something more than rotated fonts, random backgrounds and silly curves)?
#6 by Edwin Siebel on October 10, 2007 - 1:08 am
Quote
Nothing to do with Imagick, but it’s not ‘Propably’, but it’s ‘Probably’. Just a small correction.
#7 by sven tietje on October 10, 2007 - 9:18 am
Quote
hi mikko,
could you give an example for creating a glow effect.
with imagick 0.9..x we do http://sven-tietje.de/imagick/merge.jpg bei combinging two images. the rounded corner can be done with imagick 2.0 very easy now, but what about the outside “glowing”.
would be great to se an example!
thanks for imagick 2.0 -> we will switch all our applications now.
#8 by Lars van den Noort on October 15, 2007 - 1:19 am
Quote
Hi Mikko,
Is it possible to use an imagickdraw instance to paint greyscale pixel values directly onto the alpha layer to create a mask? To project this on your ‘ImagickDraw, more complex drawing’ with the flower class on the gradient background: can an imagedraw instance, instead of drawing the white circle, draw a (white?) circle directly on the alpha layer causing the middle of the flower not to be opaque white but to be completely transparent? In the finished image this would make the gradient background show through in the middle of the flower. Can this be done?
Pingback: Mikko’s blog » Blog Archive » Pretty thumbnails
#9 by Shelley on November 18, 2007 - 5:48 pm
Quote
I’m introducing IMagick with a few examples for a book on web graphics I’m creating for O’Reilly.
Can you recommend a good how-to or tutorial on installing? Suggestions for Windows users?
#10 by w miller on December 1, 2007 - 2:57 am
Quote
ditto on the req from zaadjison from october
> #
> # zaadjison 09 Oct 2007 at 10:35 am
>
> An example using fonts would be great, a CAPTCHA perhaps (something > more than rotated fonts, random backgrounds and silly curves)?
thanks muchly for you blog – i just discovered it, and it is great to see examples and what they do.
wmm
#11 by Anders on December 7, 2007 - 12:54 pm
Quote
Hi..
Have been trying to use the function distort perspective to make a perspective of an image but no luck. You might want to try..
//Anders
#12 by Feckinotter on January 4, 2008 - 7:39 pm
Quote
If it wasn’t for this blog I think I would have given up on using Imagick, the resources for trying to learn it are very slim indeed.
Following the examples in here really helped but I am currently having problems replicating this functionality
http://www.imagemagick.org/Usage/layers/#flatten
I can use compositeImage to sequentially composite a new image other another, but specifying all the images at once then flattening them would seem to be cleaner and perhaps faster.
Any ideas about how I should go about this? Adding images to an Imagick objects is simple enough, but trying to alter their position on the canvas using setPage doesn’t seem to be doing anything, the resulting image from flattenImages is blank.
#13 by rodrigo moraes on February 9, 2008 - 3:11 pm
Quote
Hey,
One question that could be an example, or maybe just a tip.
How to make rounded corners with a border? I’ve done it by making a slightly bigger rounded image with the desired border color, and then adding the original image over it.
The result is fine, except that it seems that the border misses anti-aliasing.
#14 by Mikko Koppanen on February 9, 2008 - 3:27 pm
Quote
rodrigo,
are you looking to do rounded borders? You could create a rounded rectangle and draw that on the image.
#15 by rodrigo moraes on February 9, 2008 - 5:13 pm
Quote
hey, thanks.
I haven’t noticed roundRectangle() before and indeed, it seems much easier.
now, and sorry for the newbie questions… if i draw a rounded retangle over a image, how do I remove what is outside of the rectangle? basically I want everything else to be transparent, or better saying, I want to “crop” the original image using the new rectangle as model.
#16 by rodrigo moraes on February 9, 2008 - 6:33 pm
Quote
hey,
never mind. i figured out. i added a rounded rectangle over a image which already had rounded borders. no need to crop. so obvious, but first i tried the not-so-simple way.
thank you.
#17 by Gtwin on February 10, 2008 - 12:53 pm
Quote
hi , first of all, thank you for those great example that helps me understand imagick.
I was wandering if there were a way to do like the arc function of GD. I would like to draw an arc and join the the start point and the end point to another point. As the gd imagefilledarc() function. Thanks for any advises. (as you probably understood i am trying to make a 3d pie plot);
Best regards,
Gtwin.
#18 by Liviu Sandulache on April 4, 2008 - 6:32 pm
Quote
Is it possible to add transparency to an animated gif with transparent background ?
#19 by martin on July 22, 2008 - 6:49 am
Quote
TKS Mikko ,you’r job is great! my web site is use imagick now.The imagick is very good!!! I like it!
thank you!
#20 by MeDevil on October 22, 2008 - 5:34 pm
Quote
Hi Mikko! Thanks for your work… i’m writing a website that uses imagick for image processing (and also uses php cairo to render some text) and would like to thank you for all the examples you provided (PHP Imagick documentation isn’t very clear!).
Now, a little question: with imagick i’ve opened a jpeg, saved it as jpeg and noticed that the colours were not the same as the original image (the original colors were much brighter than the saved one). Do you know how can i solve this?
Also, i think that imagick should implement a more powerfull interface to handle text graphics: it is missing character spacing, line spacing (for multiline text), word wrapping, antialias type (gray/subpixel/normal)…
#21 by Phoenix on November 21, 2008 - 12:30 pm
Quote
Hi Mikko!
Just detecting a problem with the convolution function here is my code
$image->convolveImage(
array(
-1,-1, 1,
-1, 0, 1,
0, 1, 1
)
);
Sometimes it works (emboss), sometimes not (dark image). I suspect a multithreading problem during the image generation can you confirm.
#22 by Johan on March 20, 2009 - 1:29 pm
Quote
Hi, Mikko,
Many thanks for your wonderful library!
One little problem I have while creating animated gifs with transparency, is I don’t find a way to set the dispose option (to “Previous”).
Johan
PS: My testing code:
$animation = new Imagick();
$animation->setFormat( ‘gif’ );
$draw = new ImagickDraw();
$draw->setFont( ‘Helvetica’ );
$draw->setStrokeColor( ‘#FFFFCC’ );
$draw->setFillColor( ‘#FFFFCC’ );
$draw->setFontSize(30.0);
$string = ‘Hi there!’;
$steps = 10;
for ( $i = $steps; $i >= 0; $i– )
{
$animation->newImage( 400, 40, ‘#00000000′ );
$animation->setOption(‘dispose’, ‘Previous’); // !!! does not work
$animation->annotateImage($draw, $i*400/$steps,30,0,$string );
$animation->setImageDelay( 10 );
}
header( ‘Content-Type: image/gif’ );
echo $animation->getImagesBlob();
#23 by Michael on March 23, 2009 - 9:03 pm
Quote
Hi Mikko,
Thank you for all the great work you do in your blog! You bring some clarity to actually putting imagick to use in real world examples. I have a situation where I am dealing with directories of images that have a clipping path defined in order to remove the background, but it is the inverse of what I need for ImageMagick. I was able to google enough and through trial and error get a command line execuion to work with this:
convert ‘x.jpg’
-matte
-fill none
-colorspace rgb
-draw “color 0 0 reset”
-clip
-colorspace rgb
-draw “Image Copy 0,0 0,0 ‘x.jpg’”
y.png
(line 0returns added for clarity)
What I want to figure out now is the php code to accomplish the same result so that I can add the additional processing I need to the images.
Thanks,
Michael
#24 by Axeia on April 1, 2009 - 10:27 am
Quote
Hello,
Started to use the PHP5 functions recently, and the lack of examples on there makes it really hard to figure out pretty much anything. I added a couple of examples in the comments on the PHP site for the things I did figure out.
Which pretty much means it’s something I was interested in and is on this site as it seems to be the only place with examples.
So please if you can, do provide more examples. Especially of basic operations like drawing a graph for those looking to replace GD2. I’m really fond of the anti-aliasing as it ‘just works’ unlike GD2 where it only seems to work on lines on a non-transparent background.
Tried to use imagick for converting a SVG, but it was to resource intensive to consider using on a production server.. it would take only a few visitors to bring it to its knees.
#25 by Alan Grey on July 6, 2009 - 6:48 am
Quote
Hi Mikko,
we have recently refactored a photo display script that adds an overlay to close to the top right of the image, however, this has caused us massive cpu load issues compared to using the gd libraries.
Most things I have seen written indicate the image magick stuff should be faster and less cpu intensive, yet clearly, this is not the case here…any suggestions about what we are doing wrong?
Code we are using is below
getImageWidth();
$photo_y = $photo->getImageWidth();
// Detect Thumb nail needing dynamic resize
if ($photo_x > 145 && $photo_x SetReturnType(‘object’);
$photo = $I->Resize($photo, NewWidth, $NewHeight);
$photo_x = $NewWidth;
$photo_y = $NewHeight;
}
if ($photo_x > 250)
$OverlayFile = PROJECT_PATH.’www/images/NewStarDeepBig.png’;
else
$OverlayFile = PROJECT_PATH.’www/images/NewStarDeep.png’;
$Overlay = new Imagick($OverlayFile);
// Calc Offset for top right
$overlay_x = $Overlay->getImageWidth();
$overlay_y = $Overlay->getImageHeight();
if ($overlay_y >$photo_y)
{
$overlay_y = $photo_y;
$Overlay->cropImage($overlay_x, $photo_y ,0,0);
}
if($Thumbnail)
{
$I = new Image();
$I->SetReturnDataType(“object”);
$Overlay = $I->Resize($Overlay, 70, 40);
//imagecopyresampled($photo,$overlay,($photo_x-($overlay_x/3)), 0, 0, 0, ($overlay_x/3), ($overlay_y/3), $overlay_x, $overlay_y);
$dst_x = ($photo_x-($overlay_x/3));
$dst_y = 0;
$src_x = $overlay_x/3;
$src_y = $overlay_y/3;
$photo->compositeImage($Overlay, Imagick::COMPOSITE_OVER, $dst_x, $dst_y);
}
else
{
//imagecopyresampled($photo,$overlay,($photo_x-$overlay_x), 0, 0, 0, $overlay_x, $overlay_y, $overlay_x, $overlay_y);
$dst_x = $photo_x-$overlay_x;
$dst_y = 0;
$src_x = 0;
$src_y = 0;
$photo->compositeImage($Overlay, Imagick::COMPOSITE_OVER, $dst_x, $dst_y);
}
Header (“Content-Type: “.$FileType);
Header (“Content-Disposition:filename=”.$FileName);
switch($FileType)
{
case “image/gif”:
$photo->setImageFormat(‘gif’);
break;
case “image/png”:
$photo->setImageFormat(‘png’);
break;
case “image/jpeg”:
$photo->setImageFormat(‘jpg’);
$photo->setCompressionQuality(100);
break;
default: break;
}
echo $photo;
class ImageClass
{
public function Resize($Src, $MaxWidth, $MaxHeight)
{
if ($Src)
$this->Imagick = $this->LoadImage($Src);
$FitbyWidth = ( ($MaxWidth/$this->Imagick->getImageWidth() ) Imagick->getImageHeight())) ? true : false;
// Aspect ratio preserved if one param is 0
if($FitbyWidth)
{
$this->Imagick->thumbnailImage($MaxWidth, 0, false);
}
else
{
$this->Imagick->thumbnailImage(0, $MaxHeight, false);
}
if ($Src)
return $this->ReturnHandler();
}
public function LoadImage($Src)
{
if ($Src instanceof Imagick)
{
// echo “Treating data as Imagick \n”;
return $Src;
}
elseif (strlen($Src)readImage($Src);
return $I;
}
else /* Image Data */
{
// echo “Treating data as blob \n”;
$I = new Imagick;
$I->readImageBlob($Src);
return $I;
}
return null;
}
public function ReturnHandler()
{
if (!is_object($this->Imagick))
return false;
$this->Imagick->setImageFormat($this->ReturnImageType);
if ($this->ReturnImageType == ‘jpg’)
$this->Imagick->setCompressionQuality($this->ImageQuality);
switch ($this->ReturnType)
{
case ‘object’:
return $this->Imagick;
break;
case ‘path’:
case ‘file’:
$FileName = ‘/tmp/’.uniqid(‘img_’).$this->ReturnImageType;
$this->Imagick->writeImage($FileName);
return $FileName;
break;
default:
case ‘data’:
return $this->Imagick->getImageBlob();
break;
}
// Never executed
return false;
}
public function SetReturnDataType($Type)
{
$this->ReturnType = strtolower($Type);
}
}
#26 by Mikko Koppanen on July 6, 2009 - 6:58 am
Quote
@Alan,
can you please use a pastebin?
#27 by AdrianJ on August 6, 2009 - 2:35 am
Quote
Hi Mikko,
I am using IMagick in a Coppermine image gallery because I wanted to disable exec. It is working brilliantly, except I can’t figure out how to make a transparent background when converting SVG to PNG. I am still getting a white background.
$imagickImage = new Imagick();
$imagickImage->readImage($path_to_svg);
$imagickImage->setImageBackgroundColor(new ImagickPixel(“transparent”));
$imagickImage->setImageFormat(“png32″);
$imagickImage->writeImage($path_to_png);
$imagickImage->clear();
$imagickImage->destroy();
I have tested the following which creates a new image and it works fine, so it has something to do with creating a new image versus reading in an SVG, but I can’t figure out what.
$imagickImage = new Imagick();
$imagickImage->newImage(100, 100, new ImagickPixel(“orange”));
$imagickImage->setBackgroundColor(new ImagickPixel(“transparent”));
$imagickImage->waveImage(15, 200);
$imagickImage->trimImage(0);
$imagickImage->setImageFormat(“png32″);
$imagickImage->writeImage($path_to_png);
$imagickImage->clear();
$imagickImage->destroy();
Thanks for any ideas,
Adrian
#28 by AdrianJ on August 7, 2009 - 10:54 pm
Quote
Nevermind – turns out I needed to use setBackgroundColor instead and put it before the ReadImage() – like so:
$imagickImage = new Imagick();
$imagickImage->setBackgroundColor(new ImagickPixel(â€transparentâ€));
$imagickImage->readImage($path_to_svg);
Another question I do have though it scaling of stroke thickness when using setResolution. Is there any way to turn off scaling of the stroke? I don’t want a hairline stroke to be really thick when sizing up an SVG.
Thanks,
Adrian
#29 by Cody on August 10, 2009 - 10:15 pm
Quote
Hey Mikko!
Love Imagick for php, using it to create dynamic images for my online website builder. One question for you. I want to create complex gradients such as the examples for commandline imagemagick (http://www.imagemagick.org/Usage/canvas/#sparse-color). Is there a way to do this with the php version of Imagick?
Thanks =)
#30 by Michael on December 29, 2009 - 11:12 pm
Quote
Dear Mikko,
Imagick is great. Thank you for that.
There is an issue, that I was not able to solve: When generating Thumbnails with round corners and shadows as in your example a .png is output. However, png files are a lot larger than .jpg, so I would like to convert it to jpg before output. The background of the website is white, so there would be no problem with the shadow not being “really” transparent.
However, when converting back with $shadow->setImageFormat(“jpg”); the soft shadow disappears and becomes a plain, black area.
Can you please tell me a way to output the image as a jpeg with a smooth shadow (on white background)? I think this is of interest for many people, since it shortens server traffic and speeds up the site.
Kind regards and thank you
Michael
Pingback: Imagick blog » Blog Archive » Requested examples: Animating GIF images
#31 by robert on January 19, 2010 - 3:01 pm
Quote
dear mikko,
i’m trying to learn to work with ImageMagick and Imagick in PHP.
but the problem is now, everytime i try something, sometimes it give some kind of error.
the most recent was “No such file or directory”.
and i tried one with rounded corners and it says “Call to undefined method Imagick::roundCorners()”
and some more errors with other functions/methods
kind regard
Robert
#32 by nicofrand on May 27, 2010 - 5:24 pm
Quote
I use it to resize pictures and apply some effects. But I have no idea how to deal with masks like here http://www.imagemagick.org/Usage/photos/#tilt_shift for example.
Else, that’s really a wonderful and powerful tool, thank you !
#33 by yannux on June 9, 2010 - 12:31 pm
Quote
Thanks for Imagick ! It’s a great tool I love to use !!
#34 by Cirrilu on July 1, 2010 - 4:12 pm
Quote
i´m quite new to imagick but i really really like it a lot! Thanks for sharing it with us
#35 by Cristian on August 26, 2010 - 5:46 pm
Quote
hello Mikko
I must say I love Imagick class using it a lot.
On my current project I am using vector graphics (SVG) in the idea of being able to resize to any dimensions without loosing quality.
Is there any way of doing that in Imagick?
Thank you for reading this.