I am allowing users to upload images to go with their auction items. On the list I allow them to have a thumbnail picture of the item (check eBay for an example)
I have already hacked it with Image Magic to create the thumbnail but need to be able to size it within a pre-defined sized cell.
I am looking for a script that either scales the image down again so that it either fills the cell horizontally or vertically and keeps perspective whilst not pushing the cell out of size.
Has anybody any ideas on who this could be implemented or know of any software that I could possibly purchase.
Many Thanks In advance
If you're using ImageMagick (actually PerlMagick?) to make the thumb, why not tell it to make it sized appropriately?
Maybe something like:
----------------------------------------------------------------
#$filename points to the user's uploaded image
#open it....
open(DATA, $filename);
$image->Read(file=>DATA);
close(DATA);
# Get the image's current height and width
$width = $image->Get('columns');
$height = $image->Get('rows');
#Say you wanted to resize if it's wider than 160 pixels
#And no taller than 120 pixels
if ($width > 160) {
$image->Scale(geometry=>'160x120');
}
if ($height > 120) {
$image->Scale(geometry=>'160x120');
}
# Open a file again, in this case I'm overwriting the current one
open(DATA, ">$filename");
$image->Write(file=>DATA, filename=>$filename);
close(DATA);
----------------------------------------------------------------
That help out at all?
I am not 110% confident with Perl/Image Magick, so if my code looks sloppy its because it is, and because it is part written by somebody else who was working with me on the project.
At the moment it is just a re-size and I use HTML to define the sizes to create the thumb, I am obviously not having good results with this as the image is distorting.
I'm looking to restrict its size to 50 pixels height or width depending on its original shape.
The sub for re-sizing and opening/closing the image is as follows:
------------------------------------------------------------
sub image_resize {
my $file_path = $_[0];
my $file_name = $_[1];
my $image_width = $_[2];
my $image_height = $_[3];
my $size = $_[4];
my $size_by = $_[5];
my $thumb_type = $_[6];
my $new_h = $image_height;
my $new_w = $image_width;
my $ratio;
my $thumb_path;
#####Is it at this point I have to define the size of the image?
if ($size_by eq "w") {
if ( $image_width > $size ) {
$ratio= $image_height / $image_width;
$new_h= $size * $ratio;
$new_w= $size;
}
}
if ($size_by eq "h") {
if ( $image_height > $size ) {
my $ratio= $image_width / $image_height;
$new_w= $size * $ratio;
$new_h= $size;
}
}
my $new_size = $new_w . "x" . $new_h;
##### In this block of information?
# get the file name
if (int($thumb_type) == 1) {
$thumb_path = {'image_upload_dir'} . "/tn2_" . $file_name;
}
else {
$thumb_path = {'image_upload_dir'} . "/tn_" . $file_name;
}
my $result = `/usr/local/bin/convert -size $size $file_path -resize $size +profile '*' $thumb_path`;
}
sub get_thumbnail {
my $images1 = $_[0];
my $image_type = $_[1];
my $thumb = "";
my $thumb_name;
my $db = &MainCore::mysql_connect;
my ($itemnumber)=$db->selectrow_array("SELECT itemnumber from Item WHERE images1=\"$images1\"");
&MainCore::mysql_disconnect($db);
my $extension = (split(/\./,$images1))[-1];
if ($image_type == 1) {
$thumb_name = "tn2_" . $itemnumber . "_1." . $extension;
}
else {
$thumb_name = "tn_" . $itemnumber . "_1." . $extension;
}
$thumb = $config{'image_upload_url'} . "/" . $thumb_name;
return $thumb;
}
------------------------------------------------------------
Is there an easy way to implement this?
What part of this do I have to change?
I can see myself being way over my head here.
Sorry for your trouble.
Just do a (this is an example - you need error checking)
system("convert -geometry ${x}x$y $infile $outfile");
This will leave an image in $outfile that does not exceed $x by $y in size.
Let convert do the calculations. This is not something you are going to do in every hit on the site, so an external program is probably not going to be a disaster in the short term. Later you can go back and make it better.
René
I have tried to change my existing code as posted above but i'm pulling 500 errors all the way. I'm at a loss here.
This helps but i'm going to have to try and implement it in this code. Any ideas or anybody know of an URL for a site for independant script writers?
Cheers
I'm pretty sure you should be able to install it through CPAN. I suggest that you get it installed if you don't have it already and visit:
[imagemagick.org...]
That's the Install/How To page. It has a few good code examples of how to use PerlMagick. I'm actually not familar with the regular imagemagick syntax, so a lot of what you are trying to do looks a tad foreign to me, but if there is a "scale" command you should try using that, as it *should* perserve your image's aspect ratio.
~Kenta~