use strict; use warnings; use Imager; use MCE; processPics("./images"); sub processPics { my $dir = './images'; my $newDat = ''; # build list of jpg files to process opendir my $dh, $dir or die "Could not open '$dir' for reading '$!'\n"; my @pics = grep(/\.jpg$/, readdir $dh); closedir $dh; # check for subdirs and make if missing mkdir "${dir}/medium" unless (-d "${dir}/medium"); mkdir "${dir}/thumbs" unless (-d "${dir}/thumbs"); mkdir "${dir}/upright" unless (-d "${dir}/upright"); # construct MCE object my $mce = MCE->new( max_workers => MCE::Util::get_ncpu(), chunk_size => 1, gather => sub { $newDat .= $_[0]; }, # called inside parent user_func => sub { my $p = $_; my $img = Imager->new(file=>"${dir}/${p}", type=>'jpeg') or die Imager->errstr(); my $med = $img->copy(); my $thumb = $img->copy(); my $rot90; # sets width to 750px, write out image $med = $img->scale(xpixels=>750); $med->write(file=>"${dir}/medium/${p}") or die $med->errstr; # sets the height to 150px, write out image $thumb = $img->scale(ypixels=>150); $thumb->write(file=>"${dir}/thumbs/${p}") or die $thumb->errstr; # rotate image, write out image $rot90 = $img->rotate(degrees=>-90); $rot90->write(file=>"${dir}/upright/${p}") or die $rot90->errstr; # append filename without the extension MCE->gather( (split(/\./, $p))[0] . '|' ); }, ); # process image dir in parallel $mce->process(\@pics); $mce->shutdown(); # display $newDat line after removing trailing | chop $newDat if $newDat; print "completed: ${newDat}\n"; }