What is the best way to call IM from a webpage?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
tdan

What is the best way to call IM from a webpage?

Post by tdan »

I am using ImageMagick to create user-customizable circles, rectangles, starbursts, text objects, and formatted photos to show in a web browser.

I have looked at several ways of running ImageMagick from our servers to do this:
  • C++ API: Magick++
    Perl API: PerlMagick
    Command Line
At first I considered C++ - it seemed fast.
Then I considered PerlMagick since it seemed to be better integrated into ImageMagick and was easier to code than C++.
But after all that, I'm beginning to wonder, why not just use the command line?

Isn't it the fastest?
It is definitely the easiest.
Is it any less secure?
And the main question: does it lose much functionality at all? What does it lose?

Why use an API at all?
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: What is the best way to call IM from a webpage?

Post by magick »

For web-pages, PerlMagick or MagickWand for PHP are recommended. They are both light-weight compared to invoking ImageMagick from the command line.
tdan

Re: What is the best way to call IM from a webpage?

Post by tdan »

What if I am doing relatively simple manipulations like taking a single image, resizing it, adding a colored border, and rotating it over a transparent background?
Are there any benchmarks for performance?
I wouldn't mind using an API, but I keep running into differences in how things are done.

For instance, using the command line I did this very simply:

Code: Select all

convert rose: -resize 200x200 -mattecolor red -frame 5x5 -background none -rotate 10 roseOut.png
The result was beautiful.

But try making it look nice in PerlMagick:

Code: Select all

#!/usr/bin/perl

use Image::Magick;
use strict;

my $image = new Image::Magick;

my $x = $image->Read( "rose.png" );
warn "$x" if "$x";

$x = $image->Resize( width=>200, height=>200 );
warn "$x" if "$x";

$x = $image->Set( mattecolor => "red" );
warn "$x" if "$x";

$x = $image->Frame( geometry=>"5x5+0+0" );
warn "$x" if "$x";

$x = $image->Rotate( degrees=>10, color=>"none");	# color sets background color
warn "$x" if "$x";

$x = $image->Write( "roseOut.png" );
warn "$x" if "$x";
The image is not happy.
The rotated image has a non-transparent background and the resize operation creates lines through the image.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: What is the best way to call IM from a webpage?

Post by magick »

The Resize() width and height do not respect the aspect ratio of the original image. Use a geometry instead:
  • $image->Resize( "200x200" );
ridera

Re: What is the best way to call IM from a webpage?

Post by ridera »

I'm with you tdan, keep it simple and one less thing to go wrong, use command line.

I use for php: exec(convert command, $response_array, $response_code); The array is to capture error exceptions and I test the response code to make certain the operation was successful, though you can't count on IM being faithful about this code.

I include a timer in all my IM functions. On the virtual server, my sites are on, resizing jpgs takes about .15sec and making thumbnails, with a pretty frame and a label, takes about .35sec.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: What is the best way to call IM from a webpage?

Post by Bonzo »

I use php with exec; there is so much you can do with it when incorperating IM into the php code. I think you also have more control.

There is a link below in my profile to my site with some php and IM examples; I think I need to go over and update some of them as they are quite old now.
Scott.Prog

Re: What is the best way to call IM from a webpage?

Post by Scott.Prog »

In my humble opinion, although the APIs for both PHP and Perl have gotten marginally better, the command line represents the best option simply because of stability. The APIs don't appear to be maintained by the ImageMagick team and have a wide level of discrepancy between usability, incorporated feature sets, and ongoing maintenance confidence.

I view the APIs as I do a beta version of Linux. Fun to play with at home but not ready for a production environment.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: What is the best way to call IM from a webpage?

Post by magick »

The API is fully supported and is in lock-step with the command line. The command-line is simply a wrapper around the MagickCore API. MagickWand and PerlMagick are wrappers of the MagickCore API as well. Anything you can do from the command-line you can do from the API. If a functionality is missing, let us know and we will be sure to expose it typically within a day or two.
tdan

Re: What is the best way to call IM from a webpage?

Post by tdan »

Magick - thanks for the tip on using geometry instead of width and height.
That took care of the lines through the image, but I still don't have a transparent background for the whole image.

The image looks fine when I assign an opaque color to the background but when I try to make the background transparent, it has black blotches in it.

Is it because my version of PerlMagick doesn't match my version of ImageMagick?
ImageMagick v6.3.2
PerlMagick v6.0.7

My boss installed the latest RPM he found.
It looks like there aren't any RPM's for PerlMagick that are up to version 6.3.
Can the older RPM's be used?
Scott.Prog

Re: What is the best way to call IM from a webpage?

Post by Scott.Prog »

magick wrote: The API is fully supported and is in lock-step with the command line. The command-line is simply a wrapper around the MagickCore API. MagickWand and PerlMagick are wrappers of the MagickCore API as well. Anything you can do from the command-line you can do from the API. If a functionality is missing, let us know and we will be sure to expose it typically within a day or two.


Good to know, thanks for correcting my ignorance. Knowing what you've said, I'll certainly take a harder look at MagickWand.
Post Reply