Gossamer Forum
Home : Products : Gossamer Links : Discussions :

Thumbnailing + Imagemagick & Andy...

Quote Reply
Thumbnailing + Imagemagick & Andy...
Andy (or anyone else well versed with ImageMagick and Thumbnails)

I'm trying to write my own code to simply grab ONE specific field (I've several upload fields y my LSQL DB) I want to specify just one field and thumnail the image .. I simply want to allow users to upload an image and convert it on the fly, I simply don't undestand how I'm supposed to do that... Should I input the stream into ImageMagick using a file handle or I need to wait till the image uploads to a temp directory and then convert it? anyone?
Quote Reply
Re: [jaltuve] Thumbnailing + Imagemagick & Andy... In reply to
Your best bet is to upload the image first, probably with a 'PRE' hook, and then copy the image to something like small-imagename.gif, and then do your Image::Magick stuff. The reason I suggest copying it first, is because, as I found out the hard way, the original is modified/removed Frown

If I had enough time, I would come up with some code for you....but I'm trying to get everything sorted before I move to Spain on Tuesday.

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: [jaltuve] Thumbnailing + Imagemagick & Andy... In reply to
I don't use image magick, but rather the netpbm utils.

But Andy's point holds for both.

Upload the image first, store it, then modify it. If you don't want to use it, you can delete it.

I tried doing everything on upload, but to do that, you need to copy the upload buffer to a holding buffer, since if you operate on the uploaded buffer, you lose the original file when you turn it into a thumbnail. On the other hand, you can redirect the output through the netpbm filters, so you maintain your original in the buffer, and the thumbnail in the new buffer.

Again, I don't use image magick... never could get it to install properly on any of my systems, so I can't answer specific questions on that. But if you need help with the upload stuff I can help with that.

My logo program uploads and thumbnails, but I think it was set to trash the original upload since all it wanted was a logo that fit in the assigned space.


PUGDOG� Enterprises, Inc.

The best way to contact me is to NOT use Email.
Please leave a PM here.
Quote Reply
Re: [jaltuve] Thumbnailing + Imagemagick & Andy... In reply to
This is what I use - hope it helps - took me quite a time to get to grips with how ImageMagick works. This uses ImageMagick::Thumbnail which you may need to download. I delete all files after the manipulations. You'll need to declare all your variables at the start of the sub.

$in = $IN->get_hash;
$imageFile=$in->{'Photo'};
$tmpPath="..."; #your path to temp directory
$JPGorGIF=($imageFile=~/(jpg|jpeg|gif)$/)?1:0; #Only allows files to end in jpg, jpeg or gif

if($imageFile && $JPGorGIF){
@path=split(m{[\\:\/]},$imageFile);
$fileName=$path[$#path];
$tmpPath=$tmpPath.$fileName;
open(OUT,">$tmpPath") or die("Cannot open $tmpPath for writing");
binmode OUT;
while($bytesread=read ($imageFile,$data,1024))
{
$size+=$bytesread;
print OUT $data;
}
close(OUT);

($full,$i,$j) = &resize_image($tmpPath,$fileName,'450','f_');
($thumb,$x,$y) = &resize_image($tmpPath,$fileName,'100','th_');
system "rm $tmpPath";

... other bits here - inserting into database

} else {
print $IN->header();
print Links::SiteHTML::display('error', {error => "Please make sure that your image is a .gif, .jpeg or .jpg file." });
return;
}

And the resize sub I'm using is
sub resize_image {
# -------------------------------------------------------------------
my $tmpPath = shift;
my $fileName = shift;
my $size = shift;
my $prefix = shift;
my $image = new Image::Magick;
$image->Read("$tmpPath");
my ($photo,$i,$j) = Image::Magick::Thumbnail::create($image,$size);
$photo->Write('/tmp/Photos/'.$prefix.$fileName);
my $newphoto = GT::SQL::File->open('/tmp/Photos/'.$prefix.$fileName);
system "rm /tmp/Photos/$prefix$fileName";
return ($newphoto,$i,$j);
}
The UK High Street
Quote Reply
Re: [pugdog] Thumbnailing + Imagemagick & Andy... In reply to
Hmm.. Andy suggest a PRE hook and playing with the images, why not a POST hook with a plugin that gets the ID of the newly added record and then looks up the file using the foreigncolkey in the Links_Files table? I think it would be cleaner. and I can do a simply system() to use the convert command on the path I can get from looking up the file path from Links_Files. I can even selectively apply conversion to the fields I choose in the plugin.

Haven't started yet, I'm just making absolutely sure this will work, I don't like to reinvent wheels ;)
Quote Reply
Re: [jaltuve] Thumbnailing + Imagemagick & Andy... In reply to
Sorry, I mean't a POST hook ... Blush

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: [jaltuve] Thumbnailing + Imagemagick & Andy... In reply to
This SUB can be used in the add success page. It accepts $ID as an argument and it will manipulate
the images based on the magick.cfg file located in the Plugins/ directory

Code for the Global

Code:
sub {
use Image::Magick;
my $cfg = do "$CFG->{admin_root_path}/Plugins/magick.cfg";
my $opts = shift || '0';
my $type = shift || 'Links'; if ($opts) {
my ($file,$width,$height,$bgcolor,$image,$quality);
# Read the Hash and iterate
while ( my ($field, $value) = each %$cfg) { $file = $DB->table($type)->file_info($field, $opts);
if ($file) { $width = $cfg->{$field}{width} || '100';
$height = $cfg->{$field}{height} || '100';
$bgcolor = $cfg->{$field}{bgcolor} || 'FFFFFF';
$quality = $cfg->{$field}{quality} || '60'; $image = Image::Magick->new;
$image->Read($file); # Image options...
$image->Scale(width=>$width,height=>$height);
$image->Set(quality=>$quality);
$image->Transparent(color=>$bgcolor); $image->Write($file);
undef $image;
}
}
}
return;
}


Contents of a sample magick.cfg file with two image fields (Image_Thumb and Image_Big)

Code:
{
'Image_Thumb' => {
'width' => 140,
'height' => 100,
'bgcolor' => 'FFFFFF',
'quality' => '60',
},
'Image_Big' => {
'width' => 800,
'height' => 600,
'bgcolor' => 'FFFFFF',
'quality' => '80',
},
}



This requires the Image Magick perl module installed....

Enjoy...

Last edited by:

jaltuve: Aug 18, 2003, 3:06 PM
Quote Reply
Re: [jaltuve] Thumbnailing + Imagemagick & Andy... In reply to
How and where can I call this sub in add_success?
Thanks a lot for your help,
Christian

Quote Reply
Re: [cwschroeder] Thumbnailing + Imagemagick & Andy... In reply to
First, create a global with the name
image_magick and use the code from the first sniplet in my last post. (the first sub)

Save the global. Now add to your success template the following tag:
<%image_magick($ID,'Links')%>

Also make sure a Plugins/magick.cfg file exists in your plugin directory with the contents I provided in my last post also.

Please note that the:
'Image_Thumb' and 'Image_Big' are the image field names I use in my LSQL Instalation, you will have to change the magick.cfg with your fields (you can have as many as you want)

That should do the trick.

Last edited by:

jaltuve: Sep 25, 2003, 4:10 AM
Quote Reply
Re: [jaltuve] Thumbnailing + Imagemagick & Andy... In reply to
Fine, taht works great, also my admin-preview works with that (doesn't work with the standards).

Is there a possibility to conserve the ratio of a picture and have the with and height as a maximum?

Example:
ratio_height = heigt / height_max
ratio_width = width / width_max
ratio_new = max(ratio_width,ratio_heigth)
height = height / ratio
width = width / ratio

How can I define an invidible background-color?
Thanks a lot for your help,
Christian

Quote Reply
Re: [cwschroeder] Thumbnailing + Imagemagick & Andy... In reply to
Hmmm.. for that I guess you'll need to modify the global (which is really straight forward) you can get all the methods the Image::Magick library uses by going here:

http://www.imagemagick.org/www/perl.html

Last edited by:

jaltuve: Sep 25, 2003, 1:12 PM
Quote Reply
Re: [jaltuve] Thumbnailing + Imagemagick & Andy... In reply to
Because of the way things go, it would be better to add the link, validate all the data, and when the "success" page is shown, to ask if they would like to add an image or logo. At that point, you start a new set of routines, you have the ID, and you are only trying to attach an image to an existing link. You can more easily hook into a payment or other validation system.

If you are only allowing links with images, it's easier to purge a link with "unvalidated" status that does not have a valid attachment via cron or other trigger, than it is to try to upload data and images in the same chunk/process reliably every time. There is a *lot* of overhead, and could/should only be done correctly using a table to hold interim data.


PUGDOG� Enterprises, Inc.

The best way to contact me is to NOT use Email.
Please leave a PM here.