PDA

View Full Version : PERL Image::Magick


the_pm
November 19th, 2004, 06:15
Hey everyone,

I had a hosting customer contact me today with questions about font annotation within the PERL Image::Magick module. I have absolutely no experience with it whatsoever, and after conferring with the techs over at The Planet, it seems no preloaded fonts are available on the machines to do the job. Their answer was for him to install them on his own, which sounds deceptively easy. Has anyone here ever played around with this feature or have any stories of friends or customers who have? What was your/their experience with it?

I've considered looking into this type of mechanism for use in submission verification scripts...

By the way, I gave my customer the link to the forums, so in the event he's reading this, let's not go publicly bashing your friendly neighborhood admin today ;)

sonicgroup
November 19th, 2004, 13:15
I've never had any experience working with Perl, but installing fonts is easy (the following example assumes a RHEL system using fontconfig).

Copy the font to 1) /usr/share/fonts/local/ or 2) ~/.fonts/ and then run fc-cache /path/to/font/dir/ to add it to the system.

BigBison
November 19th, 2004, 15:11
Perl, ugh. The "GD" (or is it "GTK") library in PHP does this, too. Not to mention, there are a plethora of commercial and open-source solutions, so don't feel you have to use Image::Magick. Whatever code you choose, the technology is the same and it's nothing to fear, taking a vector-based graphic (font) and bitmapping it is cake.

"Reverse Turing Tests" check to see if someone is human, I'll have one on the signup page for my CMS, but not soon enough for your needs I'm afraid. Personally, I think they're a great idea for anti-fraud and anti-spam purposes.

the_pm
November 19th, 2004, 15:24
My understanding is that GD2 and Image Magick are comparable modules in terms of rendering quality and speed. I've seen the commercial options, but why not use what's already available if it'll do the trick? What's so bad about Perl anyway? People seem to shun it for some reason. I've only learn enough to be able to understand what I'm seeing, but I can't see any reason why it should be avoided. It's a very handy language, and CGI is a very handy platform...

BigBison
November 19th, 2004, 16:30
Then you're ahead of me. I have never, don't now, and never will grok Perl. If you want PHP to run as slowly as Perl, you can install it as CGI instead of as a module, CGI isn't tied to any one language.

the_pm
November 19th, 2004, 17:30
Certainly you can program nearly anything in CGI from what I understand. I think it's the multitude of Perl/CGI modules available that make it so handy. Again, my understanding of the technology is limited to code recognition and the ability to interpret code into English at a basic level.

kc8wvj
November 19th, 2004, 21:17
Hi everyone. I'm the hosting customer that "the_pm" spoke about. Let me give you a scenario and see what your thoughts are.

Ultimately I'd like to (on the fly) create a .GIF that is transparent and is annotated only with an e-mail address. I'll bet you can see where I'm going with this ... Using this graphic instead of displaying the address in ascii is a deterrent to spam harvesting.

I'm a Perl programmer (since 1998 - learned on my own) that is just now getting into PHP. I've programmed enough PHP so far to be dangerous (MySQL database backend, automated tasks, working with sessions, etc.). If you were going to program this in PHP, how would you go about it? I'm not set completely on Image::Magic -- I just know it's installed on the server and I've used it in the past to process digital camera images.

Any code snippets would be greatly appreciated, but if you could give me general pointers, or even a favorite tutorial URL, that would be great too.

Thanks in advance,
Mike

p.s. KC8WVJ, my userid, is my Amateur Radio callsign. Didn't want you guys to think I created my userid just by typing random characters. :-)

BigBison
November 19th, 2004, 21:33
Sorry, I won't really know how to do it until I've done it. Here's a quick link:

http://pear.php.net/package/Image_Text

That package is required by this one, which does the reverse-Turing:

http://pear.php.net/package/Text_CAPTCHA/

sonicgroup
November 20th, 2004, 00:51
Here's a decent intro to GD - http://www.phpfreaks.com/tutorials/105/0.php

My understanding is that GD2 and Image Magick are comparable modules in terms of rendering quality and speed. I've seen the commercial options, but why not use what's already available if it'll do the trick? What's so bad about Perl anyway? People seem to shun it for some reason. I've only learn enough to be able to understand what I'm seeing, but I can't see any reason why it should be avoided. It's a very handy language, and CGI is a very handy platform...

GD2 and ImageMagick are comparable as far as features are concerned, but for rendering quality, ImageMagick wins.

As for Perl, 1) It wasn't designed with the web in mind, 2) It's like a swiss army knife crossed with swiss cheese (does a bit of everything - with holes everywhere), 3) It's slow for web use because it runs as a CGI binary, 4) Have you ever actually tried to use CPAN?

Lastly, I'm not going to debate how useful the language is - a lot of my sysadmin scripts are written in Perl. But that's where Perl's strengths lie - on the system for administration and general utility programming.

Digiscape
November 20th, 2004, 01:02
Hi folks (first post)
The following is using the GD library and requires PHP 4.3.9 as GIF support has been reintroduced (it was removed due to licencing) if this version is not available look at using jpeg or png http://php.net/gd

Save the following as a php file (eg emailimg.php)

<?php
header("Content-type: image/gif");
//create a new image - specify width and hieght
$image = imagecreate(200,40);
//create white (for background );
$bgc = imagecolorallocate($image, 255, 255, 255);
//define the transparent background
$tbg = imagecolortransparent($image, $bgc);
//create blue for text
$blue = imagecolorallocate($image , 0,0,255);
//create our transparent background
imagefilledrectangle($image ,0,0,200,40,$tbg);
//display the email address in blue
imagestring($image,10,15,10,'jason@jason.com',$blu e);
//output image to browser as a gif
imagegif($image);
//destroying image
imagedestroy($image);
?>

Use the above file in an img tag
<img src="emailimg.php">

Hope this helps

Regards
Jason

the_pm
November 20th, 2004, 01:13
Thanks for that, Jason, though I wouldn't cry over not having .gif support when 9/10 times .pngs are a more compact alternative anyway.

Welcome to IWDN! It's great to see you here after seeing your considerable skill at work on your site :)

It's like a swiss army knife crossed with swiss cheese (does a bit of everything - with holes everywhere)
:lol: Did you come up with that all on your own?

sonicgroup
November 20th, 2004, 05:02
Actually I did. ;) I was trying to think up something really clever, and that's what popped out. So, is it really clever? :D

the_pm
November 20th, 2004, 05:04
Yeah, it's awesome. I'll have to use it with my friends and claim I came up with it on my own ;)

kc8wvj
November 20th, 2004, 18:22
Got GD/PHP to work. Here's the code that I used to render a transparent .GIF with an e-mail address in it. Of course, I'll want to send this function an SQL id, then use MySQL to look up the address associated with that ID. That way, no hint at all of the address shows up in the HTML code for Spam Harvesting tools to steal it.

The code below creates a GIF image where black is the text color and white is the color that is made to be transparent.

<?

header ("Content-type: image/gif");
$img_handle = ImageCreate (230, 15) or die ("Cannot Create image");
$back_color = ImageColorAllocate ($img_handle, 255, 255, 255);
$txt_color = ImageColorAllocate ($img_handle, 0, 0, 0);
ImageTTFText ($img_handle, 10, 0, 0, 10, $txt_color, "/path/to/TTF/fonts/some_font.ttf", "me@mydomain.org");
ImageColorTransparent($img_handle, $back_color);
ImageGif ($img_handle);

?>

GD is actually quite simple to use and, as far as I can tell, has all the same functionality as Image::Magick does with Perl. I won't get into a holy war over Perl vs. PHP. Both have their places, but I agree that Perl is best suited for sysadmin stuff (daily stuff run by cron, etc.) and PHP is streamlined for HTTP. Thanks for all your help, guys. You helped me get another item off my punchlist. :-)