Using Imager module to resample images
Using Imager module to resample images
am 29.01.2008 12:08:11 von Jason Carlton
I've been forcing my visitors to resample their images manually to
keep them below 50kb, but this has only had so-so results. A lot of my
visitors don't understand how to resample their image, so I'm getting
high-resolution images that are something like 20px x 20px, but still
take up 50kb!
So, I've been thinking about resampling the images on the fly. The
images would be restricted to JPG, JPEG, GIF, and PNG.
I was planning to use a combination of Image::Resize and Imager, which
I've never used before, but since the load on the server is ALWAYS
high, I had a few questions first:
1. Will the use of Imager take up a lot of CPU resources?
2. How do I recognize an animated GIF from a static GIF?
3. Does Imager compress JPG images automatically? I didn't see an
option to set a compression level in the docs, but it may be called
something else that I'm not recognizing.
4. I assume that I'll need to use Image::Resize to get the current
width and height, and if it exceeds the maximum that I input then I'll
use Imager to resample it. Is there a smarter (read: simpler and/or
less resource-heavy) option that this?
TIA,
Jason
Re: Using Imager module to resample images
am 29.01.2008 13:41:21 von zentara
On Tue, 29 Jan 2008 03:08:11 -0800 (PST), Jason Carlton
wrote:
>I've been forcing my visitors to resample their images manually to
>keep them below 50kb, but this has only had so-so results. A lot of my
>visitors don't understand how to resample their image, so I'm getting
>high-resolution images that are something like 20px x 20px, but still
>take up 50kb!
>
>So, I've been thinking about resampling the images on the fly. The
>images would be restricted to JPG, JPEG, GIF, and PNG.
>
>I was planning to use a combination of Image::Resize and Imager, which
>I've never used before, but since the load on the server is ALWAYS
>high, I had a few questions first:
>
>1. Will the use of Imager take up a lot of CPU resources?
Never noticed an excessive cpu usage, but check it yourself.
>2. How do I recognize an animated GIF from a static GIF?
I believe it's safe to assume a static gif begins with GIF87, and
an animated with GIF89. But I'm not sure it's an absolute.
>3. Does Imager compress JPG images automatically? I didn't see an
>option to set a compression level in the docs, but it may be called
>something else that I'm not recognizing.
>
>4. I assume that I'll need to use Image::Resize to get the current
>width and height, and if it exceeds the maximum that I input then I'll
>use Imager to resample it. Is there a smarter (read: simpler and/or
>less resource-heavy) option that this?
Your best bet is to read
perldoc Imager::Files for the jpegquality parameter
perldoc Imager::Transformations for converting and scaling
perldoc Imager::ImageTypes
perldoc Imager will give an overview
Also http://groups.google.com will have many examples.
Search for things like:
"perl Imager resize", etc
zentara
>Jason
--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
Re: Using Imager module to resample images
am 29.01.2008 15:45:42 von Abigail
_
zentara (zentara@highstream.net) wrote on VCCLXIV September MCMXCIII in
:
@@ On Tue, 29 Jan 2008 03:08:11 -0800 (PST), Jason Carlton
@@ wrote:
@@
@@ >2. How do I recognize an animated GIF from a static GIF?
@@
@@ I believe it's safe to assume a static gif begins with GIF87, and
@@ an animated with GIF89. But I'm not sure it's an absolute.
While it is true that a GIF87a image is static, and that any animated
gif image is a GIF89a one, it does not mean that GIF89a forbids static
images. GIF89a is an enhancement of GIF87a. It allows animated images,
but it also allows interlaced images and other features. It doesn't
mandate one.
Abigail
--
perl -wle'print"Êõóô áîïôèåò Ðåòì Èáãëåò"^"\x80"x24'
Re: Using Imager module to resample images
am 29.01.2008 16:32:26 von 1usa
zentara wrote in
news:vv6up3521ho9pu3io1l4qvsk0nag72mgvg@4ax.com:
> On Tue, 29 Jan 2008 03:08:11 -0800 (PST), Jason Carlton
> wrote:
>
....
>>2. How do I recognize an animated GIF from a static GIF?
>
> I believe it's safe to assume a static gif begins with GIF87, and
> an animated with GIF89. But I'm not sure it's an absolute.
http://members.aol.com/royalef/gif89a.txt
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
clpmisc guidelines:
Re: Using Imager module to resample images
am 29.01.2008 22:32:59 von Martien Verbruggen
On Tue, 29 Jan 2008 03:08:11 -0800 (PST),
Jason Carlton wrote:
> I've been forcing my visitors to resample their images manually to
> keep them below 50kb, but this has only had so-so results. A lot of my
> visitors don't understand how to resample their image, so I'm getting
> high-resolution images that are something like 20px x 20px, but still
> take up 50kb!
There's always the possibility to advise people to use one of the many
online services that are available for image resizing. Typing 'image
resize' in a well known search engine gave me quite a few links.
> So, I've been thinking about resampling the images on the fly. The
> images would be restricted to JPG, JPEG, GIF, and PNG.
>
> I was planning to use a combination of Image::Resize and Imager, which
> I've never used before, but since the load on the server is ALWAYS
> high, I had a few questions first:
>
> 1. Will the use of Imager take up a lot of CPU resources?
Image manipulation always takes up a reasonable amount of CPU, as well
as memory. Whether it takes up "a lot" depends on both your expectations
and definition of a lot, and on the size of the images.
> 2. How do I recognize an animated GIF from a static GIF?
I don't know of a way with Imager, but there might be one. There's
informaton in the Imager::Files documentation about the tags Imager
will write, but I am not sure whether it will tell you that the input
image was an animated one. Imager combines animated GIFs into a single
RGB image.
You can do this with Image::Magick, by looking at the number of images
in the object, but you're not using that module, and it would be silly
to install it only for this.
Depending on what you want to do, there are other possible alternatives:
A quick search on CPAN showed up Image::DeAnim, which you could use
first to get rid of any animations, if you don't like the way Imager
does it, and if that's all you really want.
Alternatively, Image::ParseGIF should possibly be able to give you
the information directly, after which you can take whatever action you
would like to take. Note that if that action is to simply combine the
images, you might as well skip this step, and load it into Imager right
away.
Otherwise you can simply write your own miniparser with unpack to parse
the file yourself. The GIF file formats are described well all over the
Internet. www.wotsit.org is not a bad place to start, I generally find.
I doubt you would have to resort to this. I have forgotten whether this
information can be read from the image header, or whether you actually
have to parse the whole filei, or at least until the second frame.
> 3. Does Imager compress JPG images automatically? I didn't see an
> option to set a compression level in the docs, but it may be called
> something else that I'm not recognizing.
Check the Imager::Files documentation, which documents the file formats
Imager takes, and specific methods and options for each.
> 4. I assume that I'll need to use Image::Resize to get the current
> width and height, and if it exceeds the maximum that I input then I'll
> use Imager to resample it. Is there a smarter (read: simpler and/or
> less resource-heavy) option that this?
Why not simply use Imager for the whole lot? If you're trying to avoid
the overhead of that, Image::Size is probably the faster method. If you
don't want to use that module, it's not hard to write your own image
header parsing routines if you only support a small number of formats.
Regards,
Martien
--
|
Martien Verbruggen |
| "Mr Kaplan. Paging Mr Kaplan..."
|
Re: Using Imager module to resample images
am 30.01.2008 14:58:30 von zentara
On 29 Jan 2008 14:45:42 GMT, Abigail wrote:
> _
>zentara (zentara@highstream.net) wrote on VCCLXIV September MCMXCIII in
>:
>@@ On Tue, 29 Jan 2008 03:08:11 -0800 (PST), Jason Carlton
>@@ wrote:
>@@
>@@ >2. How do I recognize an animated GIF from a static GIF?
>@@
>@@ I believe it's safe to assume a static gif begins with GIF87, and
>@@ an animated with GIF89. But I'm not sure it's an absolute.
>
>While it is true that a GIF87a image is static, and that any animated
>gif image is a GIF89a one, it does not mean that GIF89a forbids static
>images. GIF89a is an enhancement of GIF87a. It allows animated images,
>but it also allows interlaced images and other features. It doesn't
>mandate one.
>
>Abigail
Yeah, it's not an absolute..... but the gif isn't 89a, then it won't
animate. So if it says 87a you can be sure it's a single image.
If it's 89a, then you test for multiple files
################################################
from perldoc Imager::Files:
read_multi
If you're reading from a format that supports multiple images per
file, use the "read_multi()" method:
my @imgs = Imager->read_multi(file=>$filename, type=>$type)
or die "Cannot read $filename: ", Imager->errstr;
As with the read() method, Imager will normally detect the "type"
automatically.
#################################################
So it seems like you can attempt to open the 89a with read_multi(),
and if you get an errstr, open it as a single file.
I havn't poured over every Imager perldoc, but there is a section on
GUESSSING TYPES in Imager::Files.
zentara
--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html