| 1 | package Command::resize; | ||
| 2 | use Mojo::Base 'Mojolicious::Command'; | ||
| 3 | use Mojo::File 'path'; | ||
| 4 | use YAML::XS; | ||
| 5 | use DDP; | ||
| 6 | |||
| 7 | use File::Basename; | ||
| 8 | use GD; | ||
| 9 | |||
| 10 | use constant DEBUG => $ENV{PI_DEBUG} || 0; | ||
| 11 | |||
| 12 | has description => "Resize photos for gallery\n"; | ||
| 13 | has usage => "usage: $0\n"; | ||
| 14 | |||
| 15 | sub run { | ||
| 16 | my ($self) = @_; | ||
| 17 | |||
| 18 | my $config = $self->app->config->{gallery}; | ||
| 19 | my $main_path = $config->{main_path}; | ||
| 20 | |||
| 21 | my $path = path $main_path; | ||
| 22 | |||
| 23 | my $galleries = $path->list({dir => 1}); | ||
| 24 | for my $gallery ($galleries->each) { | ||
| 25 | my $meta_source = $gallery->list->first(qr/yml/)->slurp; | ||
| 26 | die 'meta is undef' unless $meta_source; | ||
| 27 | my $meta = Load $meta_source; | ||
| 28 | next if $meta->{skip}; | ||
| 29 | my $gallery_path = $gallery->to_string; | ||
| 30 | |||
| 31 | my $photos = $gallery->list->grep(qr/jpg|png|JPG|PNG/); | ||
| 32 | for my $photo ($photos->each) { | ||
| 33 | my $file_path = $photo->to_string; | ||
| 34 | |||
| 35 | _resize($file_path, $config->{sizes}, "$gallery_path"); | ||
| 36 | unlink $file_path; | ||
| 37 | say "Done $file_path"; | ||
| 38 | } | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 | sub _resize { | ||
| 43 | my ($file, $sizes, $imgDir) = @_; | ||
| 44 | # https://github.com/davepagurek/Image-Resizer | ||
| 45 | |||
| 46 | if (!$imgDir) { | ||
| 47 | my ($a, $b, $c) = fileparse($file); | ||
| 48 | $imgDir = $b; | ||
| 49 | } | ||
| 50 | |||
| 51 | if (!-d $imgDir) { | ||
| 52 | mkdir $imgDir or die "Unable to create $imgDir"; | ||
| 53 | } | ||
| 54 | |||
| 55 | if ($file && -e $file) { | ||
| 56 | my $name = "img"; | ||
| 57 | my $mime = "jpg"; | ||
| 58 | if ($file =~ /[\/\\]*([a-zA-Z0-9-_ ]*)\.([a-z]+)$/i) { | ||
| 59 | $name = $1; | ||
| 60 | $mime = $2; | ||
| 61 | } | ||
| 62 | |||
| 63 | my $img; | ||
| 64 | if (lc($mime) eq "jpg" || lc($mime) eq "jpeg") { | ||
| 65 | $img = GD::Image->newFromJpeg($file); | ||
| 66 | } elsif (lc($mime) eq "png") { | ||
| 67 | $img = GD::Image->newFromPng($file); | ||
| 68 | } else { | ||
| 69 | die "Unsupported file format: $mime"; | ||
| 70 | } | ||
| 71 | |||
| 72 | my ($w,$h) = $img->getBounds(); # find dimensions | ||
| 73 | |||
| 74 | for my $size (keys %$sizes) { | ||
| 75 | my $imgDirSize = "$imgDir/$size"; | ||
| 76 | if (!-d $imgDirSize) { | ||
| 77 | mkdir $imgDirSize or die "Unable to create $imgDirSize"; | ||
| 78 | } | ||
| 79 | my $newimg; | ||
| 80 | if ($sizes->{$size}->{crop}) { | ||
| 81 | my ($cut,$xcut,$ycut); | ||
| 82 | if ($w>$h){ | ||
| 83 | $cut=$h; | ||
| 84 | $xcut=(($w-$h)/2); | ||
| 85 | $ycut=0; | ||
| 86 | } | ||
| 87 | if ($w<$h){ | ||
| 88 | $cut=$w; | ||
| 89 | $xcut=0; | ||
| 90 | $ycut=(($h-$w)/2); | ||
| 91 | } | ||
| 92 | $newimg = new GD::Image($sizes->{$size}->{width}, $sizes->{$size}->{height}, 1); | ||
| 93 | $newimg->copyResampled($img,0,0,$xcut,$ycut,$sizes->{$size}->{width}, $sizes->{$size}->{height},$cut,$cut); | ||
| 94 | } else { | ||
| 95 | my $gd; | ||
| 96 | if ($w>$h) { | ||
| 97 | $newimg = new GD::Image($sizes->{$size}->{width}, (($h/$w)*$sizes->{$size}->{width}), 1); | ||
| 98 | $newimg->copyResampled($img,0,0,0,0,$sizes->{$size}->{width}, (($h/$w)*$sizes->{$size}->{width}),$w,$h); | ||
| 99 | } else { | ||
| 100 | $newimg = new GD::Image(($w/$h)*$sizes->{$size}->{height}, ($sizes->{$size}->{height}), 1); | ||
| 101 | $newimg->copyResampled($img,0,0,0,0,($w/$h)*$sizes->{$size}->{height}, ($sizes->{$size}->{height}),$w,$h); | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | open(my $thumbFile, ">", "$imgDirSize/$name.jpg") or die "Cannot open $imgDirSize/$name.jpg: $!"; | ||
| 106 | binmode $thumbFile; | ||
| 107 | print $thumbFile $newimg->jpeg($sizes->{$size}->{quality}); | ||
| 108 | close $thumbFile; | ||
| 109 | } | ||
| 110 | } else { | ||
| 111 | die "Can't find file $file\n"; | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | 1; |