http://www.perlmonks.org?node_id=985250

palkia has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone.
I'm trying to find a way to predict which image (Imager)
will take the smaller size once saved to a file (before saving).
Since I'm planning this process to run many times,
I don't want to slow down my program by actually saving the images to files and than comparing sizes, and removing the bigger one.

Your thoughts ?
Thx
  • Comment on How can I predict which image will big smaller ?

Replies are listed 'Best First'.
Re: How can I predict which image will big smaller ?
by BrowserUk (Patriarch) on Aug 03, 2012 at 14:55 UTC

    Why not have it write all the filetypes to a ramfile first:

    my %sizes; for my $type ( Imager->write_types ) { open my $ramFH, '>:raw', \$buffer; $im->write( fh => $ramFH, type => $type ); $sizes{ length $buffer } = $type; } ## Pick type of smallest my $filetype = $sizes{ (sort{ $a <=> $b } keys %sizes)[0] }; ## write to real file ...

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

Re: How can I predict which image will big smaller ?
by ww (Archbishop) on Aug 03, 2012 at 15:06 UTC
    Did you read the documentation?
    ... especially
    # try to save in one of these formats
    SAVE:
    "
    (assumption: you mean smaller file size).

    Or, "# Create smaller version
    # documented in Imager::Transformations
    "
    if "smaller size" refers to dimensions in pixels.