Forum Moderators: coopster & phranque

Message Too Old, No Replies

Using Imager to resize and copy submitted photos

         

csdude55

12:10 am on Jun 13, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I've been using this for several years, but now I'm making some modifications to the script so I'm wondering two things:

1. Does this script seem relatively problem-free? It tests OK, but once I put it in production it will get hit thousands of times a day so I'm concerned about processing times.

2. Is there a better / faster-to-process option than using Imager to do this same thing?

use Imager;
use File::Copy;
...

# Copy and resample to /photos/ and /thumbs/
# Original location: $wwwpath/original/
if (-e "$wwwpath/original/$pic_name") {
copy("$wwwpath/original/$pic_name", "$wwwpath/photos/$pic_name")
or die "Photo failed: $!";
copy("$wwwpath/original/$pic_name", "$wwwpath/thumbs/$pic_name")
or die "Thumb failed: $!";

# Photos, resize to 275 x auto
my $img = Imager->new;
$img->read(file => "$wwwpath/photos/$pic_name");
$org_width = $img->getwidth();
$org_height = $img->getheight();

if ($org_width < $org_height) {
$resize_pic = $img->scale(xpixels=>275, qtype => 'mixing');
}
else {
$resize_pic = $img->scale(ypixels=>275, qtype => 'mixing');
}

$resize_pic->write(file => "$wwwpath/photos/$pic_name");

# Thumbs, resize to 75 x 75
my $img = Imager->new;
$img->read(file => "$wwwpath/thumbs/$pic_name");
$org_width = $img->getwidth();
$org_height = $img->getheight();

if ($org_width < $org_height) {
$resize_pic = $img->scale(xpixels=>75, qtype => 'mixing');
}
else {
$resize_pic = $img->scale(ypixels=>75, qtype => 'mixing');
}

$resize_pic = $resize_pic->crop(width=>75, height=>75);
$resize_pic->write(file => "$wwwpath/thumbs/$pic_name");
...
}

A modification I'm considering:

use Imager;
...

# Copy and resample to /photos/ and /thumbs/
# Original location: $wwwpath/original/
if (-e "$wwwpath/original/$pic_name") {
# Eliminate File::Copy and copy(), just read from /original/ and save to new path

# Only create $img once, not sure why I'm doing it twice
my $img = Imager->new;
$img->read(file => "$wwwpath/original/$pic_name");
$org_width = $img->getwidth();
$org_height = $img->getheight();

# Photos, resize to 275 x auto
if ($org_width < $org_height) {
$resize_photo = $img->scale(xpixels=>275, qtype => 'mixing');
}
else {
$resize_photo = $img->scale(ypixels=>275, qtype => 'mixing');
}

$resize_photo->write(file => "$wwwpath/photos/$pic_name");

# Thumbs, resize to 75 x 75
if ($org_width < $org_height) {
$resize_thumb = $img->scale(xpixels=>75, qtype => 'mixing');
}
else {
$resize_thumb = $img->scale(ypixels=>75, qtype => 'mixing');
}

$resize_thumb = $resize_thumb->crop(width=>75, height=>75);
$resize_thumb->write(file => "$wwwpath/thumbs/$pic_name");
...
}

Brett_Tabke

3:09 am on Jun 6, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Ever use Image Magick convert, montage, and identify instead? It's how I manipulate the featured images here from the facebook/twitter open graph stuff. I find it more robust and quicker than imager.