[How-To] Code for ImageMagick
1 Attachment(s)
Although vBulletin supports ImageMagick it is not widely supported in the mod community. In fact, when I ran into a need to support it for Egg Avatar I could not find another mod that did so to learn from.
After a lot of reading and poking around I discovered that, although the vBulletin image class that supports ImageMagick is far to specific to be useful, it does have one rather useful member: fetch_im_exec(). fetch_im_exec takes care of finding and calling ImageMagick for you, all you need to do is pass the proper command line. Of course, I didn't want to fill my code with if/else statements every time I wanted to do something with an image, so it made sense to create my own class to abstract the process. Step 1: Creating an Abstract Base Class The idea here is that we are basically creating an 'interface'. A common set of methods that can be called without having to worry about whether GD or ImageMagick is the image processor. PHP Code:
Step 2: Create a 'factory class' A factory class is a simple little class that checks some values and creates the proper real class for you. In this case I check to see if vbulletin is set to use GD or ImageMagick and return an instance of either tk_Image_GD or tk_Image_ImageMagick. PHP Code:
The full code of each of these classes can be seen in the attached zip, but we're going to take a look at a stripped down version of each that implements the 'meat' method of an image processing, that is: fetch_image. We'll look at the GD example first, because it's simpler. PHP Code:
ImageMagick is in someways simple, but also trickier. It has no concept of working in memory or 'handles'. Everything is done with files on disk. So we have to 'fake' the process by creating an array of filenames we are working with and return positions in that array as the 'handles'. Since the ImageMagick calls we will make later will directly alter the image, we need to make a temporary copy instead, to preserve the original (at least, for my purposes I did not want to alter the original). PHP Code:
ImageMagick consists of two main 'executables', identify and covert. identify gathers information about an image, and convert alters it. Here I use identify to find out what type of image I'm working with to properly give the temp file an extensions of 'gif' or 'jpg' or whatever. The second post in this article contains a full usage example of the zipped class I've provided. The class won't cover all your needs since it was written for mine, but it should be easy for you to adapt or add to it for whatever your needs are. |
1 Attachment(s)
Usage example:
PHP Code:
|
Very very useful info, thanks for spending the time getting this posted :D
S-MAN |
wow.. useful! thanks!
|
Very nice and helpful, thanks.
NB: There are small bugs in the zip file - a) the output function has different numbers of arguments for the gd and imagemagick classes - I assume that the imagemagick version is what was intended. b) the imagemagick code does not work correctly if filenames contain spaces |
All times are GMT. The time now is 02:23 PM. |
Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
More Information | |
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|