Gossamer Forum
Home : General : Perl Programming :

Image::Magick

Quote Reply
Image::Magick
Hi. I'm trying to get a basic image resizing functionality in a script I'm writing. I have the following code so far;

Code:
#!/usr/bin/perl

use Image::Magick;
use CGI::Carp qw(fatalsToBrowser);
use CGI;
$IN = new CGI;

my $ReadImage = "/home/linkssql/public_html/dev/6/86-Payments-small.gif";
my $WriteImage = "/home/linkssql/public_html/dev/6/smaller-Payments.gif";

my($image, $x);

$image = Image::Magick->new;
$image->Read($ReadImage) || die "1: $! ";

$image->Scale(geometry=>$_PercentageSmaller) || die "2: $! ";

$image->Write(filename=>$WriteImage) || die "3: $! ";

print $IN->header();
print "done";

However, when running this it gives a 500 IS Error (via browser), or the following error message via SSH;

Quote:
jailshell-2.05a$ perl test.cgi
perl: constitute.c:2015: ReadImage: Assertion `image_info->signature == 0xabacadab' failed.
Aborted
jailshell-2.05a$

Is this a boo-boo in the install/configuration of Image::Magick, or my script?

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Image::Magick In reply to
use strict;

Where is $_PercentageSmaller being defined?

- wil
Quote Reply
Re: [Wil] Image::Magick In reply to
I'd rewrite your code something like this:

Code:
#!/usr/bin/perl

use strict;

use Image::Magick;
use CGI;
my $q = CGI->new();

my $readimage = "/home/linkssql/public_html/dev/6/86-Payments-small.gif";
my $writeimage = "/home/linkssql/public_html/dev/6/smaller-Payments.gif";

my ($image,$x);

$image = Image::Magick->new();

$x = $image->Read(filename=>"$readimage");
die $x if $x;

# set your resized image here. look up the 'geometry' function and you'll realize that
# 150 x 75 is the maximum available size set for the image canvas. That means, that the
# resized image will be resized propotionally (same as your percentage) down to fit within
# my contraints. It will not just resize it set at 150 x 75.

$x = $image->Resize(geometry=>'150x75');
die $x if $x;

$x = $image->Write(filename=>"$writeimage");
die $x if $x;

print $q->header;

print qq|Done.|;

- wil
Quote Reply
Re: [Wil] Image::Magick In reply to
Whoops Blush

I've changed that line to this now;

$image->Scale(geometry=>'50%') || die "2: $! ";

... but it still gives me the error;

Quote:
perl: constitute.c:2015: ReadImage: Assertion `image_info->signature == 0xabacadab' failed.
Aborted

Thanks for the reply. I'm starting to wonder if it is Image::Magick that isn't correctly installed. The code from my Rand_AddImage plugin doesn't work either (gives 500), yet it works find on all the other sites I have tried).

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Image::Magick In reply to
Try Image::Magick from the command line. It produces much more friendly and imformative error messages. The first suspect in every case with me is a coruppted GIF file.

- wil
Quote Reply
Re: [Andy] Image::Magick In reply to
Why do you need to use image magick just to make a smaller payment gif...use photoshop =)
Quote Reply
Re: [Paul] Image::Magick In reply to
In Reply To:
Why do you need to use image magick just to make a smaller payment gif...use photoshop =)

Because I'm doing batch stuff, and not just the one image Wink

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Wil] Image::Magick In reply to
In Reply To:
I'd rewrite your code something like this:

Still gives me the Assertion error :(

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Image::Magick In reply to
Tried it with various other GIFs? Tried it from the command line? Tried upgrading Image::Magick?

- wil
Quote Reply
Re: [Wil] Image::Magick In reply to
Tried it with 4 diffeent GIF's, 2 JPG's and even some PNG's.

>>>Tried it from the command line? <<<

Wouldn't know how to do that :(

>>>Tried upgrading Image::Magick? <<<

I've emailed the support guys, and asked them to upgrade.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Image::Magick In reply to
There's good documentation available at www.imagemagick.org that will get you started on the command line.

- wil
Quote Reply
Re: [Wil] Image::Magick In reply to
Even command line stuff doesn't seem to be working :|

jailshell-2.05a$ convert -size 640x480 /home/linkssql/www/dev/test.jpg
convert: Missing an image file name.


Unsure

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Image::Magick In reply to
That looks like you didn't follow the instructions more than anything.
Quote Reply
Re: [Paul] Image::Magick In reply to
That is the exact command they supply on the site. All I did was change the name of the file :|

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Image::Magick In reply to
im really curious what you guys find on this issue. i could use a working code snippet like the broken one you listed above, but one that actually works...
Quote Reply
Re: [mozkill] Image::Magick In reply to
This is some code I use to make thumbnail images;

Code:
use Image::Magick;

my $_ImageField = $cfg->{'ImageFields'};
my $_PercentageSmaller = $cfg->{'Smaller'};

my @ImageFields = split(",",$_ImageField);

print $IN->header();

foreach (@ImageFields) {

# get rid of newlines...
chomp;

# only run this code if they actually uploaded a file...
if ($IN->param($_)) {

# grab the image name (i.e test.gif)
my ($img) = $IN->param($_) =~ m|([^\\/]+)$|; # grab the image name...and then assign the path...

# get the actual path to where the file is/will be saved...
my $schema = $DB->table('Links')->cols;
my $path = $schema->{$_}->{'file_save_in'};
$path =~ s,/$,,; # get rid of trailing / at end of $path

# get the highest ID in lsql_Links...
my $highest_id = $DB->table('Links')->select( 'MAX(ID)' )->fetchrow;

# now we have the highest ID, lets get the image stuff for this link.
my $col = $_; # column name where file is held
my $tbl = $DB->table( 'Links' );
my $fh = $tbl->file_info( $col, $highest_id ); # return a glob reference to the file that you can print <$fh> if you want
my $rel_path = $fh->File_RelativePath;
undef $fh; # explicit close of the filehandle for good measure

# create the variable for where the small image will be held...
my @img_create = split("/",$rel_path);
my $count = $#img_create;

my $rel_path_small = undef; # make sure we have this variable as an undef, otherwise we get probs...
for (my $i = 0; $i < $count; $i++) { $rel_path_small .= $img_create[$i] . "/"; }
$rel_path_small .= "small-" . $img_create[$count]; # create the small image name...

my $ReadImage = $path . $rel_path;
my $WriteImage = $path . $rel_path_small;

# copy the full image as a .tmp file. So we can rename it after...
# and get the original...
my $tmp = $ReadImage . ".tmp";
copy($ReadImage,$tmp);

my($image, $x);

$image = Image::Magick->new;
$x = $image->Read($ReadImage);
if ($x) { print "Error: $x"; exit; }

$x = $image->Scale(geometry=>$_PercentageSmaller);
if ($x) { print "Error: $x"; exit; }

$x = $image->Write(filename=>$WriteImage);
if ($x) { print "Error: $x"; exit; }

# rename .tmp backup, so we get the original too...
rename($tmp,$ReadImage);

} # end 'if'

}


Obviously it will need to be edited to acomidate your values, file names etc, but it should hopefully get you on the right track Smile

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Image::Magick In reply to
yes, thats enought for me to go on... thanks. Cool