Re: Resize image width & height...
by adrianh (Chancellor) on Jun 28, 2003 at 08:26 UTC
|
Image::Shoehorn, amongst others, can be used to resize images. Take a look around the Image:: modules in CPAN.
| [reply] |
Re: Resize image width & height...
by steves (Curate) on Jun 28, 2003 at 12:41 UTC
|
It may be overkill, but ImageMagick or GraphicsMagick would do this and almost anything else you can imagine to an image. The Perl interfaces they supply are good. Same code base for both; basically different development/release philosophies if I understand the recent split correctly.
| [reply] |
Re: Resize image width & height...
by dash2 (Hermit) on Jun 28, 2003 at 10:28 UTC
|
You also probably want to look at the GD:: modules.
| A massive flamewar beneath your chosen depth has not been shown here |
|
| [reply] |
Re: Resize image width & height...
by oakbox (Chaplain) on Jun 28, 2003 at 15:35 UTC
|
I wrote this for an online image gallery. I wanted the 'long' dimension to be no larger than 450 pixels, so my script needed to do the resizing before commiting the file to disk and I needed to write down the height and width attributes so that I could modify the HTML to display the image correctly. Here's my code:
# write the file to a temporary location
my $tmp_loc = $TEMP_IMG_DIR . $image_name;
my $whan;
open($whan,">$tmp_loc") || die "Cannot write $tmp_loc $!";
print $whan $temp_file_var;
close($whan);
# Resize image and write it to the appropriate location
use Image::Thumbnail;
my $t = new Image::Thumbnail(
size => 450,
create => 1,
inputpath => $tmp_loc,
outputpath => $main_loc
);
# Get the new size of the main image
use Image::Info qw(image_info dim);
my $info = image_info($main_loc);
my ($w, $h) = dim($info);
$temp_file_var is holding the image.
At the end, the new image is written to the path in $main_loc and I have the width $w and the height $h of the new image.
I hope that you can modify this for your use!
oakbox | [reply] [d/l] |
Re: Resize image width & height...
by skx (Parson) on Jun 28, 2003 at 14:35 UTC
|
Others have already pointed you at some image modules, but the way I read the question is that you have people inputing an URL for an image which you're then including inline.
If that is the correct interpretation then you aren't going to have any joy with the modules; if the image isn't upon your server then you can't easily get it's size - you'd have to download a copy from the remote site and work on that.
If this is for a forum or script of some kind you might be better allowing the user to upload an image to your site and then using that local copy. That way you could filter the image on size/dimensions.
If you want to go this way make sure you use strict, use taint, and look at the CGI module for file uploads.
Steve
---
steve.org.uk
| [reply] |
Re: Resize image width & height...
by Petras (Friar) on Jun 28, 2003 at 13:49 UTC
|
It may make me look like an evil anti-perl (I can feel the downvotes coming!), but you could actually do the whole thing with JavaScript. screen.width and screen.height return int values for with width and height of the screen in pixels. Depending on the user's resolution you could proportionately scale down from the original image size.
But a real monk would take the time to learn to do it in Perl! (that's my anti-downvote disclaimer ;)
Perlway or the highway,
Petras
Don't worry about people stealing your ideas. If your ideas are any good, you'll have to ram them down people's throats.
-Howard Aiken
| [reply] |
|
|
| [reply] |
|
|
Um, yeah, if the goal is to change the raw graphic file then someone with a lot more experience than me would have to post something! But the SOP read like the goal was to change the size of the graphic based on the users screen settings. I might have misinterpretted. The (*gulp for non-Perl*) js option would automate the munging. I'll look the SOP over again and see where I read wrong c",)
Cheers, P
Don't worry about people stealing your ideas. If your ideas are any good, you'll have to ram them down people's throats.
-Howard Aiken
| [reply] |
Re: Resize image width & height...
by tcf22 (Priest) on Jun 28, 2003 at 17:44 UTC
|
I normally use LWP::Simple to grab the contents of the image, from the remote system. You can then use Image::Size to get the size of the image, then shrink it down based on some desired height/width, then use the width/height properties of the IMG tag to display it smaller. I've used something similiar to this before.
#! /usr/bin/perl
use strict;
use warnings;
use Image::Size;
use LWP::Simple;
use CGI;
my $cgi = new CGI;
my $image = $cgi->param('image');
my $contents = get($image);
my ($width, $height, $error) = imgsize(\$contents);
# attributes.
if (($width > 300) || ($height > 300)) {
if ($width > $height) {
# Image is wider that it is tall
($width,$height) = (300, int((300 * $height) / $width));
}else{
# Image is taller than it is wide
($width,$height) = (int((300 * $width) / $height), 300);
}
}
#output image
print "<IMG src=\"$image\" width=\"$width\" height=\"$height\">";
| [reply] [d/l] |
Re: Resize image width & height...
by sgifford (Prior) on Jun 28, 2003 at 17:01 UTC
|
If you're just concerned about how the page looks and not the download time for the image, you might be able to get away with just using the height and width attributes of img.
| [reply] |
|
|
I saw that NY Times uses to create thumbnails from cropping images. But sometimes they say very little of the whole image.
I think that it is best to specify a standard (html: width and height) square (130 x 130) with the real image to be seen after clicking on it.
A square might not represent the real image well but at small proportions, one wouldn't be able to recognize well the difference and you gain some client speed at preloading the real image.
| [reply] |
|
|
I'm no hard-core web developer, but I do recall readingsomewhere that using the width/height properties varies in results on different browsers. Some might squeeze the image differently than others. If this is an intranet site that will have homogenous users (everyone on IE:5 or something), it's no biggie then. But if it's open to the world, I would suggest going the Image::* routes mentioned above, as it will ensure a consistent face for all your users.
| [reply] |