1 | # +-----------------------------------------------------------------------+ |
---|
2 | # | pLoader - a Perl photo uploader for Piwigo | |
---|
3 | # +-----------------------------------------------------------------------+ |
---|
4 | # | Copyright(C) 2008 Piwigo Team http://piwigo.org | |
---|
5 | # +-----------------------------------------------------------------------+ |
---|
6 | # | This program is free software; you can redistribute it and/or modify | |
---|
7 | # | it under the terms of the GNU General Public License as published by | |
---|
8 | # | the Free Software Foundation | |
---|
9 | # | | |
---|
10 | # | This program is distributed in the hope that it will be useful, but | |
---|
11 | # | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
12 | # | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
13 | # | General Public License for more details. | |
---|
14 | # | | |
---|
15 | # | You should have received a copy of the GNU General Public License | |
---|
16 | # | along with this program; if not, write to the Free Software | |
---|
17 | # | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
18 | # | USA. | |
---|
19 | # +-----------------------------------------------------------------------+ |
---|
20 | package Uploader::ImageList; |
---|
21 | use strict; |
---|
22 | use Carp; |
---|
23 | use base qw/Uploader::Object/; |
---|
24 | use Image::ExifTool qw(:Public); |
---|
25 | use Image::Magick; |
---|
26 | use File::Spec; |
---|
27 | use Uploader::Image; |
---|
28 | use Data::Dumper; |
---|
29 | use Storable; |
---|
30 | use Digest::MD5::File qw/file_md5_hex md5_hex/; |
---|
31 | use Encode qw/encode decode is_utf8/; |
---|
32 | |
---|
33 | # this class implements a collection of image files with associated data |
---|
34 | $|=1; |
---|
35 | __PACKAGE__->mk_accessors( |
---|
36 | qw/ |
---|
37 | thumb_size |
---|
38 | preview_ratio |
---|
39 | categories |
---|
40 | type |
---|
41 | filter |
---|
42 | blur |
---|
43 | quality |
---|
44 | prefix |
---|
45 | author |
---|
46 | count |
---|
47 | resize_w |
---|
48 | resize_h |
---|
49 | new_files |
---|
50 | storable_file |
---|
51 | wx_thumb_size |
---|
52 | current_image |
---|
53 | images |
---|
54 | image_selection |
---|
55 | exif_metadata |
---|
56 | wx_thumb_imglist |
---|
57 | wx_thumb_dir |
---|
58 | preview_dir |
---|
59 | site_resized_dir |
---|
60 | site_thumb_dir |
---|
61 | userdata_dir |
---|
62 | progress_msg |
---|
63 | last_error_msg |
---|
64 | default_name_prefix |
---|
65 | SetNewFilesViewerRefreshCallback |
---|
66 | SetNewFilesProgressCallback |
---|
67 | SetNewFilesDisplayEndInfoCallback |
---|
68 | UploadImagesViewerCallback |
---|
69 | progress_thumbnail_refresh |
---|
70 | progress_msg_refresh |
---|
71 | progressbar_refresh |
---|
72 | progress_endinfo_refresh |
---|
73 | RescaleCallback |
---|
74 | ResizeCallback |
---|
75 | upload_rejects |
---|
76 | pwg |
---|
77 | upload_high |
---|
78 | wx_quality |
---|
79 | th_quality |
---|
80 | auto_rotate |
---|
81 | interlace |
---|
82 | create_resized |
---|
83 | use_exif_preview |
---|
84 | image_sums |
---|
85 | sums |
---|
86 | version |
---|
87 | imagelist_version |
---|
88 | / |
---|
89 | ); |
---|
90 | |
---|
91 | |
---|
92 | sub Init { |
---|
93 | my ( $self ) = @_; |
---|
94 | } |
---|
95 | |
---|
96 | |
---|
97 | |
---|
98 | # save exif preview image if available |
---|
99 | # otherwise create a preview image |
---|
100 | sub _write_preview_image { |
---|
101 | my ( $self, $imagedata ) = @_; |
---|
102 | |
---|
103 | |
---|
104 | # If PreviewImage is available, we use it |
---|
105 | if(defined $imagedata ) { |
---|
106 | print "_write_preview_image, use exif PreviewImage\n"; |
---|
107 | eval { |
---|
108 | open PREVIEW_FILE, ">", $self->current_image->preview_file ; |
---|
109 | binmode PREVIEW_FILE; |
---|
110 | print PREVIEW_FILE $$imagedata; |
---|
111 | close PREVIEW_FILE; |
---|
112 | }; |
---|
113 | $self->last_error_msg($@) if $@; |
---|
114 | } |
---|
115 | |
---|
116 | } |
---|
117 | |
---|
118 | |
---|
119 | sub _set_exif_tag { |
---|
120 | my ( $self, $file, $tag, $newValue ) = @_; |
---|
121 | |
---|
122 | my $options = {}; |
---|
123 | # Create a new Image::ExifTool object |
---|
124 | my $exifTool = new Image::ExifTool; |
---|
125 | |
---|
126 | # Extract meta information from an image |
---|
127 | $exifTool->ExtractInfo($file, $options); |
---|
128 | |
---|
129 | # Set a new value for a tag |
---|
130 | $exifTool->SetNewValue($tag, $newValue); |
---|
131 | |
---|
132 | # Write new meta information to a file |
---|
133 | $exifTool->WriteInfo($file); |
---|
134 | |
---|
135 | } |
---|
136 | |
---|
137 | sub _set_current_image_filepaths__ { |
---|
138 | my ( $self ) = @_; |
---|
139 | |
---|
140 | my ( $vol, $dir, $file ) = File::Spec->splitpath( |
---|
141 | $self->current_image->file |
---|
142 | ); |
---|
143 | |
---|
144 | my ( $filename, $ext ) = split /\./, $file ; |
---|
145 | |
---|
146 | |
---|
147 | $self->current_image->wx_thumb_file( |
---|
148 | File::Spec->catfile( |
---|
149 | $self->wx_thumb_dir, |
---|
150 | sprintf( |
---|
151 | "%s.%s", |
---|
152 | $filename, |
---|
153 | $self->type, |
---|
154 | ) |
---|
155 | ) |
---|
156 | ); |
---|
157 | |
---|
158 | $self->current_image->preview_file( |
---|
159 | File::Spec->catfile( |
---|
160 | $self->preview_dir, |
---|
161 | sprintf( |
---|
162 | "%s.%s", |
---|
163 | $filename, |
---|
164 | $self->type, |
---|
165 | ) |
---|
166 | ) |
---|
167 | ); |
---|
168 | $self->current_image->preview_file( |
---|
169 | encode('iso-8859-1', $self->current_image->preview_file) |
---|
170 | ); |
---|
171 | |
---|
172 | $self->current_image->site_thumb_file( |
---|
173 | File::Spec->catfile( |
---|
174 | $self->site_thumb_dir, |
---|
175 | sprintf( |
---|
176 | "%s.%s", |
---|
177 | $filename, |
---|
178 | $self->type, |
---|
179 | ) |
---|
180 | ) |
---|
181 | ); |
---|
182 | |
---|
183 | } |
---|
184 | |
---|
185 | sub _set_current_image_filepaths { |
---|
186 | my ( $self ) = @_; |
---|
187 | |
---|
188 | my $filename = sprintf( |
---|
189 | "%s.%s", |
---|
190 | $self->current_image->file_sum, |
---|
191 | $self->type, |
---|
192 | ); |
---|
193 | |
---|
194 | |
---|
195 | $self->current_image->wx_thumb_file( |
---|
196 | File::Spec->catfile( |
---|
197 | $self->wx_thumb_dir, |
---|
198 | $filename |
---|
199 | ) |
---|
200 | ); |
---|
201 | |
---|
202 | $self->current_image->preview_file( |
---|
203 | File::Spec->catfile( |
---|
204 | $self->preview_dir, |
---|
205 | $filename |
---|
206 | ) |
---|
207 | ); |
---|
208 | $self->current_image->preview_file( |
---|
209 | encode('iso-8859-1', $self->current_image->preview_file) |
---|
210 | ); |
---|
211 | |
---|
212 | $self->current_image->site_thumb_file( |
---|
213 | File::Spec->catfile( |
---|
214 | $self->site_thumb_dir, |
---|
215 | $filename |
---|
216 | ) |
---|
217 | ); |
---|
218 | |
---|
219 | } |
---|
220 | |
---|
221 | |
---|
222 | sub SetCurrentImage { |
---|
223 | my ( $self, $indx ) = @_; |
---|
224 | |
---|
225 | $self->current_image( |
---|
226 | $self->GetImage($indx) |
---|
227 | ); |
---|
228 | } |
---|
229 | |
---|
230 | |
---|
231 | sub SetNewFiles { |
---|
232 | my ( $self, $files ) = @_; |
---|
233 | |
---|
234 | $self->new_files( $files ); |
---|
235 | |
---|
236 | # if some files have been previously selected |
---|
237 | my $i = scalar @{$self->sums}; |
---|
238 | my $count = 0; |
---|
239 | $self->count($count); |
---|
240 | my $errors = 0; |
---|
241 | |
---|
242 | map { |
---|
243 | my $info = $self->_read_exif_metatdata($_); |
---|
244 | my $is_new_image = $self->_add_image($_, $info, $i); |
---|
245 | $self->SetCurrentImage($i); |
---|
246 | $self->_set_current_image_filepaths(); |
---|
247 | |
---|
248 | if($is_new_image){ |
---|
249 | my $use_wx_resize = $self->_create_gui_preview($info); |
---|
250 | $self->_create_gui_thumbnail($use_wx_resize); |
---|
251 | |
---|
252 | # ok |
---|
253 | if(!$@){ |
---|
254 | $self->progress_msg("Thumbnail and preview created for %s"); |
---|
255 | } |
---|
256 | else { |
---|
257 | $self->progress_msg("An error has occured when processing %s\n$@"); |
---|
258 | # remove from list |
---|
259 | splice @{$self->sums}, $i, 1; |
---|
260 | $errors++; |
---|
261 | } |
---|
262 | |
---|
263 | $self->SetNewFilesProgressCallback->(); |
---|
264 | } |
---|
265 | $i++; |
---|
266 | $count++; |
---|
267 | $self->count($count); |
---|
268 | $self->SetNewFilesViewerRefreshCallback->(); |
---|
269 | } |
---|
270 | @{$files}; |
---|
271 | $self->SetNewFilesDisplayEndInfoCallback->( |
---|
272 | sprintf( |
---|
273 | "%s images added to the selection\n\n%s errors", |
---|
274 | $self->count, |
---|
275 | $errors |
---|
276 | |
---|
277 | ) |
---|
278 | ); |
---|
279 | |
---|
280 | $self->Store; |
---|
281 | |
---|
282 | } |
---|
283 | |
---|
284 | sub _read_exif_metatdata { |
---|
285 | my ( $self, $file ) = @_; |
---|
286 | |
---|
287 | # read exif metadata |
---|
288 | my $info; |
---|
289 | eval { |
---|
290 | $info = ImageInfo( $file ); |
---|
291 | }; |
---|
292 | $info = {} if($@); |
---|
293 | |
---|
294 | $info; |
---|
295 | } |
---|
296 | |
---|
297 | # key is file path |
---|
298 | sub _add_image { |
---|
299 | my ( $self, $file, $info, $i ) = @_; |
---|
300 | |
---|
301 | my $is_new_image; |
---|
302 | |
---|
303 | # for legacy imagelist that do not have image_sums property |
---|
304 | $self->image_sums( |
---|
305 | {} |
---|
306 | ) if !$self->image_sums; |
---|
307 | |
---|
308 | my $sum = file_md5_hex($file); |
---|
309 | |
---|
310 | my $image; |
---|
311 | if ( !exists $self->image_sums->{$sum} ){ |
---|
312 | # append to image list |
---|
313 | $image = Uploader::Image->new( |
---|
314 | { |
---|
315 | file => $_, |
---|
316 | file_sum => $sum, |
---|
317 | site_name => sprintf( |
---|
318 | "%s %s", |
---|
319 | $self->default_name_prefix, |
---|
320 | $i, |
---|
321 | ), |
---|
322 | site_author => $self->author, |
---|
323 | exif_metadata => $self->_select_exif_data($info), |
---|
324 | add_rank => $i, |
---|
325 | site_categories => [], |
---|
326 | site_tags => [], |
---|
327 | site_high_file => $_, |
---|
328 | } |
---|
329 | ); |
---|
330 | |
---|
331 | $self->image_sums->{$sum} = $image ; |
---|
332 | $is_new_image = 1; |
---|
333 | } else { |
---|
334 | $image = $self->image_sums->{$sum}; |
---|
335 | } |
---|
336 | |
---|
337 | |
---|
338 | #$self->images->[$i] = $image ; |
---|
339 | $self->sums->[$i] = $sum ; |
---|
340 | |
---|
341 | $is_new_image; |
---|
342 | } |
---|
343 | |
---|
344 | sub _create_gui_preview { |
---|
345 | my ( $self, $info ) = @_; |
---|
346 | |
---|
347 | my $create_gui_preview; |
---|
348 | my $use_wx_resize; |
---|
349 | if($self->use_exif_preview){ |
---|
350 | # an exif preview is available. we try to use it |
---|
351 | if(defined $info->{PreviewImage} ){ |
---|
352 | printf("use preview\n"); |
---|
353 | $self->_write_preview_image( $info->{PreviewImage} ); |
---|
354 | my $image = new Image::Magick; |
---|
355 | eval { |
---|
356 | $create_gui_preview = $image->ReadImage( |
---|
357 | $self->current_image->preview_file |
---|
358 | ); |
---|
359 | }; |
---|
360 | unlink $self->current_image->preview_file if $create_gui_preview; |
---|
361 | } |
---|
362 | else { |
---|
363 | $create_gui_preview = 1; |
---|
364 | } |
---|
365 | } |
---|
366 | else { |
---|
367 | $create_gui_preview = 1; |
---|
368 | } |
---|
369 | print "create gui preview", $create_gui_preview, "\n"; |
---|
370 | # have to create a preview file |
---|
371 | if($create_gui_preview) { |
---|
372 | eval { |
---|
373 | if(!$self->CreateGUIPreview()){ |
---|
374 | # use method provided by the caller |
---|
375 | # source, target, type, ratio |
---|
376 | print "CreateGUIPreview failed, use callback\n"; |
---|
377 | $self->RescaleCallback->( |
---|
378 | $self->current_image->file, |
---|
379 | $self->current_image->preview_file, |
---|
380 | $self->type, |
---|
381 | $self->preview_ratio, |
---|
382 | undef, |
---|
383 | undef, |
---|
384 | $self->quality, |
---|
385 | ); |
---|
386 | $use_wx_resize = 1; |
---|
387 | } |
---|
388 | };# create a preview file |
---|
389 | } |
---|
390 | |
---|
391 | $self->RotateImage( |
---|
392 | $self->current_image->preview_file, |
---|
393 | ) if $self->auto_rotate; |
---|
394 | |
---|
395 | $self->_set_exif_tag( |
---|
396 | $self->current_image->preview_file, |
---|
397 | 'Orientation', |
---|
398 | 'Horizontal (normal)', |
---|
399 | ) if $self->auto_rotate; |
---|
400 | |
---|
401 | $use_wx_resize; |
---|
402 | } |
---|
403 | |
---|
404 | sub _create_gui_thumbnail { |
---|
405 | my ( $self, $use_wx_resize ) = @_; |
---|
406 | |
---|
407 | # Now, we should have a valid preview image. |
---|
408 | # try to thumbnail it |
---|
409 | eval { |
---|
410 | printf("create gui thumbnail\n"); |
---|
411 | # use the preview image to create a gui display thumbnail |
---|
412 | if($use_wx_resize) |
---|
413 | { |
---|
414 | print "CreateGUIThumbnail failed, use callback\n"; |
---|
415 | $self->ResizeCallback->( |
---|
416 | $self->current_image->preview_file, |
---|
417 | $self->current_image->wx_thumb_file, |
---|
418 | $self->type, |
---|
419 | $self->wx_thumb_size, |
---|
420 | $self->wx_thumb_size, |
---|
421 | $self->wx_quality, |
---|
422 | ); |
---|
423 | |
---|
424 | } else { |
---|
425 | $self->CreateGUIThumbnail(); |
---|
426 | } |
---|
427 | }; |
---|
428 | } |
---|
429 | |
---|
430 | sub RemoveImageSelection { |
---|
431 | my ( $self ) = @_; |
---|
432 | |
---|
433 | return if (! scalar @{$self->sums} ); |
---|
434 | return if (! defined $self->image_selection ); |
---|
435 | |
---|
436 | # higher first, to keep same indexes during remove |
---|
437 | my @images = reverse @{$self->image_selection}; |
---|
438 | map { |
---|
439 | my $image = $self->GetImage($_)->file; |
---|
440 | splice @{$self->sums}, $_, 1 ; |
---|
441 | $self->wx_thumb_imglist->Remove($_); |
---|
442 | shift @images; |
---|
443 | } |
---|
444 | @images; |
---|
445 | |
---|
446 | # clear image selection |
---|
447 | $self->image_selection([]); |
---|
448 | } |
---|
449 | |
---|
450 | # used for display in GUI. has to fit a square box ( wxImageList ) |
---|
451 | sub CreateGUIThumbnail { |
---|
452 | my ( $self ) = @_; |
---|
453 | |
---|
454 | return 1 if( -e $self->current_image->wx_thumb_file ); |
---|
455 | my $rval = 0; |
---|
456 | print "CreateGUIThumbnail ", $self->current_image->wx_thumb_file, "\n"; |
---|
457 | my $image = new Image::Magick; |
---|
458 | |
---|
459 | my $size = $self->wx_thumb_size; |
---|
460 | |
---|
461 | my $status = $image->Set(size=>sprintf("%sx%s", 3*$size, 3*$size)); |
---|
462 | warn "$status" if $status ; |
---|
463 | |
---|
464 | $status = $image->ReadImage( |
---|
465 | $self->current_image->preview_file |
---|
466 | ); |
---|
467 | warn "$status" if $status; |
---|
468 | return $rval if $status; |
---|
469 | |
---|
470 | $self->current_image->preview_w( |
---|
471 | $image->Get('width') |
---|
472 | ); |
---|
473 | $self->current_image->preview_h( |
---|
474 | $image->Get('height') |
---|
475 | ); |
---|
476 | |
---|
477 | |
---|
478 | $status = $image->Thumbnail( |
---|
479 | geometry=>sprintf("%s%s>", $size*$size, '@') |
---|
480 | ); |
---|
481 | warn "$status" if $status; |
---|
482 | return $rval if $status; |
---|
483 | |
---|
484 | # causes strange behaviour with i18n -> yellow borders when local is other than EN |
---|
485 | # $status = $image->Set(background=>"white"); |
---|
486 | # warn "$status" if $status ; |
---|
487 | |
---|
488 | $status = $image->Set(Gravity=>"Center"); |
---|
489 | warn "$status" if $status ; |
---|
490 | |
---|
491 | $image->Extent( |
---|
492 | geometry=>sprintf("%sx%s", $size, $size), |
---|
493 | gravity=>'center', |
---|
494 | ); |
---|
495 | |
---|
496 | $image->Set( |
---|
497 | quality=>$self->wx_quality |
---|
498 | ); |
---|
499 | |
---|
500 | $status = $image->Strip(); |
---|
501 | warn "$status" if $status ; |
---|
502 | |
---|
503 | |
---|
504 | $image->Write( |
---|
505 | sprintf( |
---|
506 | "%s:%s", |
---|
507 | $self->type, |
---|
508 | encode('iso-8859-1', $self->current_image->wx_thumb_file), |
---|
509 | ) |
---|
510 | ); |
---|
511 | |
---|
512 | undef $image; |
---|
513 | |
---|
514 | $rval = 1; |
---|
515 | |
---|
516 | return $rval; |
---|
517 | } |
---|
518 | |
---|
519 | |
---|
520 | sub CreateGUIPreview { |
---|
521 | my ( $self ) = @_; |
---|
522 | printf("CreateGUIPreview %s\n", $self->current_image->preview_file ); |
---|
523 | return 1 if( -e $self->current_image->preview_file ); |
---|
524 | |
---|
525 | my $rval = 1; |
---|
526 | |
---|
527 | my $image = Image::Magick->new(); |
---|
528 | |
---|
529 | my $ratio = $self->preview_ratio; |
---|
530 | |
---|
531 | |
---|
532 | my $status = $image->Read( |
---|
533 | sprintf( |
---|
534 | "%s", |
---|
535 | $self->current_image->file, |
---|
536 | ) |
---|
537 | ); |
---|
538 | warn "$status ", $self->current_image->file, "\n" if $status ; |
---|
539 | return 0 if $status; |
---|
540 | |
---|
541 | $status = $image->Thumbnail( |
---|
542 | geometry=>sprintf( |
---|
543 | "%s%%x%s%%>", |
---|
544 | $ratio, |
---|
545 | $ratio |
---|
546 | ) |
---|
547 | ); |
---|
548 | warn "$status" if $status ; |
---|
549 | return 0 if $status; |
---|
550 | |
---|
551 | |
---|
552 | $status = $image->Set(background=>"white"); |
---|
553 | warn "$status" if $status ; |
---|
554 | |
---|
555 | $status = $image->Set(Gravity=>"Center"); |
---|
556 | warn "$status" if $status ; |
---|
557 | |
---|
558 | |
---|
559 | $image->Set(quality=>$self->wx_quality); |
---|
560 | |
---|
561 | |
---|
562 | $status = $image->Write( |
---|
563 | sprintf( |
---|
564 | "%s:%s", |
---|
565 | $self->type, |
---|
566 | encode('iso-8859-1', $self->current_image->preview_file), |
---|
567 | ) |
---|
568 | ); |
---|
569 | warn "$status" if $status ; |
---|
570 | return 0 if $status; |
---|
571 | |
---|
572 | undef $image; |
---|
573 | |
---|
574 | return $rval; |
---|
575 | } |
---|
576 | |
---|
577 | |
---|
578 | sub CreateResized { |
---|
579 | my ( $self ) = @_; |
---|
580 | |
---|
581 | my $rval = 1 ; |
---|
582 | return $rval if( -e $self->current_image->site_resized_file ); |
---|
583 | |
---|
584 | printf( |
---|
585 | "Create resized %s\n", |
---|
586 | $self->current_image->file, |
---|
587 | ); |
---|
588 | |
---|
589 | my $image = new Image::Magick; |
---|
590 | |
---|
591 | my $status = $image->ReadImage( |
---|
592 | $self->current_image->file |
---|
593 | ); |
---|
594 | warn "$status" if $status ; |
---|
595 | return 0 if $status; |
---|
596 | |
---|
597 | my $w = $image->Get('width'); |
---|
598 | my $h = $image->Get('height'); |
---|
599 | |
---|
600 | # should calculate the aspect ratio |
---|
601 | my $resize_w = $self->resize_w; |
---|
602 | my $resize_h = $self->resize_h; |
---|
603 | |
---|
604 | if( $w < $h ){ |
---|
605 | my $resize_w_ = $resize_w; |
---|
606 | $resize_w = $resize_h; |
---|
607 | $resize_h = $resize_w_; |
---|
608 | } |
---|
609 | |
---|
610 | $status = $image->Resize( |
---|
611 | geometry => sprintf("%sx%s>", $resize_w, $resize_h), |
---|
612 | filter => sprintf("%s", $self->filter), |
---|
613 | blur => $self->blur |
---|
614 | ); |
---|
615 | warn "$status" if $status ; |
---|
616 | return 0 if $status; |
---|
617 | |
---|
618 | $status = $image->Set(Gravity=>"Center"); |
---|
619 | warn "$status" if $status ; |
---|
620 | |
---|
621 | # exif from original image |
---|
622 | my $orientation = $self->current_image->exif_metadata->{Orientation}; |
---|
623 | |
---|
624 | # Valid for Rotate 180, Rotate 90 CW, Rotate 270 CW |
---|
625 | if( $orientation =~ m/Rotate (\d+)/ ){ |
---|
626 | printf( |
---|
627 | "Rotate %s\n", |
---|
628 | $1 |
---|
629 | ); |
---|
630 | |
---|
631 | $image->Rotate( degrees=>$1 ); |
---|
632 | } |
---|
633 | |
---|
634 | $status = $image->Set(quality=>$self->quality); |
---|
635 | warn "$status" if $status ; |
---|
636 | |
---|
637 | $status = $image->Set(interlace=>$self->interlace); |
---|
638 | warn "$status" if $status ; |
---|
639 | |
---|
640 | $image->Write( |
---|
641 | sprintf( |
---|
642 | "%s:%s", |
---|
643 | $self->type, |
---|
644 | encode('iso-8859-1', $self->current_image->site_resized_file), |
---|
645 | ) |
---|
646 | ); |
---|
647 | warn "$status" if $status ; |
---|
648 | return 0 if $status; |
---|
649 | |
---|
650 | undef $image; |
---|
651 | |
---|
652 | |
---|
653 | $rval = 0 if $status; |
---|
654 | |
---|
655 | return $rval; |
---|
656 | } |
---|
657 | |
---|
658 | sub CreateThumbnail { |
---|
659 | my ( $self ) = @_; |
---|
660 | |
---|
661 | return 1 if( -e $self->current_image->site_thumb_file ); |
---|
662 | |
---|
663 | my $rval = 1; |
---|
664 | |
---|
665 | my $image = new Image::Magick; |
---|
666 | |
---|
667 | my $status = $image->ReadImage( |
---|
668 | encode('iso-8859-1', $self->current_image->site_resized_file) |
---|
669 | ); |
---|
670 | warn "$status" if $status ; |
---|
671 | |
---|
672 | |
---|
673 | $status = $image->Resize( |
---|
674 | geometry => sprintf( |
---|
675 | "%sx%s>", |
---|
676 | $self->thumb_size, |
---|
677 | $self->thumb_size |
---|
678 | ), |
---|
679 | ); |
---|
680 | warn "$status" if $status ; |
---|
681 | |
---|
682 | $status = $image->Set(Gravity=>"Center"); |
---|
683 | warn "$status" if $status ; |
---|
684 | |
---|
685 | $status = $image->Set(quality=>$self->th_quality); |
---|
686 | warn "$status" if $status ; |
---|
687 | |
---|
688 | $status = $image->Strip(); |
---|
689 | warn "$status" if $status ; |
---|
690 | |
---|
691 | |
---|
692 | $image->Write( |
---|
693 | sprintf( |
---|
694 | "%s:%s", |
---|
695 | $self->type, |
---|
696 | encode('iso-8859-1', $self->current_image->site_thumb_file), |
---|
697 | ) |
---|
698 | ); |
---|
699 | |
---|
700 | undef $image; |
---|
701 | |
---|
702 | |
---|
703 | $rval = 0 if $status; |
---|
704 | |
---|
705 | return $rval; |
---|
706 | } |
---|
707 | |
---|
708 | |
---|
709 | |
---|
710 | sub _select_exif_data { |
---|
711 | my ( $self, $exif ) = @_; |
---|
712 | |
---|
713 | return { |
---|
714 | map { |
---|
715 | $_ => $exif->{$_}, |
---|
716 | } |
---|
717 | qw/ |
---|
718 | CreateDate |
---|
719 | ImageWidth |
---|
720 | ImageHeight |
---|
721 | Orientation |
---|
722 | DateTimeOriginal |
---|
723 | ISO |
---|
724 | ExposureTime |
---|
725 | ApertureValue |
---|
726 | FocalLength |
---|
727 | Lens |
---|
728 | Exposure |
---|
729 | Make |
---|
730 | Model |
---|
731 | / |
---|
732 | }; |
---|
733 | } |
---|
734 | |
---|
735 | sub Store { |
---|
736 | my ( $self ) = @_; |
---|
737 | |
---|
738 | my $data = $self->get_storable( |
---|
739 | [ |
---|
740 | qw/ |
---|
741 | images |
---|
742 | thumb_size |
---|
743 | preview_ratio |
---|
744 | type |
---|
745 | filter |
---|
746 | blur |
---|
747 | quality |
---|
748 | wx_quality |
---|
749 | th_quality |
---|
750 | prefix |
---|
751 | author |
---|
752 | count |
---|
753 | resize_w |
---|
754 | resize_h |
---|
755 | new_files |
---|
756 | storable_file |
---|
757 | wx_thumb_size |
---|
758 | current_image |
---|
759 | exif_metadata |
---|
760 | wx_thumb_dir |
---|
761 | preview_dir |
---|
762 | site_resized_dir |
---|
763 | site_thumb_dir |
---|
764 | userdata_dir |
---|
765 | progress_msg |
---|
766 | default_name_prefix |
---|
767 | upload_high |
---|
768 | auto_rotate |
---|
769 | interlace |
---|
770 | create_resized |
---|
771 | use_exif_preview |
---|
772 | image_sums |
---|
773 | sums |
---|
774 | version |
---|
775 | imagelist_version |
---|
776 | / |
---|
777 | ] |
---|
778 | |
---|
779 | ); |
---|
780 | eval { |
---|
781 | store $data, $self->storable_file; |
---|
782 | }; |
---|
783 | if($@){ |
---|
784 | print $@, "\n"; |
---|
785 | } |
---|
786 | } |
---|
787 | |
---|
788 | |
---|
789 | |
---|
790 | sub UploadSelection { |
---|
791 | my ( $self ) = @_; |
---|
792 | |
---|
793 | my $viewer_callback = $self->UploadImagesViewerCallback ; |
---|
794 | |
---|
795 | |
---|
796 | $self->upload_rejects( |
---|
797 | [] |
---|
798 | ); |
---|
799 | |
---|
800 | my $count = 1; |
---|
801 | my $msg; |
---|
802 | $self->count( |
---|
803 | $count |
---|
804 | ); |
---|
805 | my $uploaded = 0; |
---|
806 | my $rejected = 0; |
---|
807 | my $time_begin = time; |
---|
808 | my $last_error; |
---|
809 | map { |
---|
810 | # current image object |
---|
811 | $self->current_image( |
---|
812 | $self->GetImage($_) |
---|
813 | ); |
---|
814 | |
---|
815 | my ( $vol, $dir, $file ) = File::Spec->splitpath( |
---|
816 | $self->current_image->file |
---|
817 | ); |
---|
818 | |
---|
819 | my $site_name = $self->current_image->site_name; |
---|
820 | |
---|
821 | # my ( $filename, $ext ) = split /\./, $file ; |
---|
822 | my $filename = $self->current_image->file_sum ; |
---|
823 | |
---|
824 | # lately defined to make sure we have the last global properties ( resize_w, resize_h ) |
---|
825 | $self->current_image->site_resized_file( |
---|
826 | File::Spec->catfile( |
---|
827 | $self->site_resized_dir, |
---|
828 | sprintf( |
---|
829 | "%s_%sx%s.%s", |
---|
830 | $filename, |
---|
831 | $self->resize_w, |
---|
832 | $self->resize_h, |
---|
833 | $self->type, |
---|
834 | ) |
---|
835 | ) |
---|
836 | ); |
---|
837 | |
---|
838 | $msg = sprintf( |
---|
839 | "Preparing resized image for %s - %s", |
---|
840 | $site_name, |
---|
841 | $file, |
---|
842 | ); |
---|
843 | |
---|
844 | eval { |
---|
845 | # set current image thumbnail |
---|
846 | $self->progress_thumbnail_refresh->(); |
---|
847 | |
---|
848 | $self->progress_msg_refresh->($msg); |
---|
849 | |
---|
850 | # update upload progress dialog |
---|
851 | $self->progressbar_refresh->(0.25); |
---|
852 | }; |
---|
853 | # user cancelled : dialog box is destroyed |
---|
854 | croak "Upload cancelled. ", $@ if $@ ; |
---|
855 | |
---|
856 | if( $self->create_resized ){ |
---|
857 | eval { |
---|
858 | if(!$self->CreateResized()){ |
---|
859 | printf("CreateResized failed %s. Use ResizeCallback\n", $@); |
---|
860 | # use method provided by the caller |
---|
861 | # source, target, type, ratio, width, $height |
---|
862 | $self->ResizeCallback->( |
---|
863 | $self->current_image->file, |
---|
864 | $self->current_image->site_resized_file, |
---|
865 | $self->type, |
---|
866 | undef, |
---|
867 | $self->resize_w, |
---|
868 | $self->resize_h, |
---|
869 | $self->quality, |
---|
870 | ); |
---|
871 | |
---|
872 | $self->RotateImage( |
---|
873 | $self->current_image->site_resized_file, |
---|
874 | ) if $self->auto_rotate; |
---|
875 | } |
---|
876 | }; |
---|
877 | $self->_set_exif_tag( |
---|
878 | $self->current_image->site_resized_file, |
---|
879 | 'Orientation', |
---|
880 | 'Horizontal (normal)', |
---|
881 | ) if $self->auto_rotate; |
---|
882 | } |
---|
883 | # the original is at the right size, no need to create a resize |
---|
884 | else { |
---|
885 | $self->current_image->site_resized_file( |
---|
886 | $self->current_image->file, |
---|
887 | ); |
---|
888 | } |
---|
889 | |
---|
890 | |
---|
891 | |
---|
892 | # if upload high, rotate a copy of original file |
---|
893 | if($self->upload_high){ |
---|
894 | $self->CreateHigh(); |
---|
895 | } |
---|
896 | |
---|
897 | $msg = sprintf( |
---|
898 | "Preparing thumbnail for %s - %s", |
---|
899 | $site_name, |
---|
900 | $file, |
---|
901 | ); |
---|
902 | |
---|
903 | eval { |
---|
904 | $self->progress_msg_refresh->($msg); |
---|
905 | }; |
---|
906 | croak "Upload cancelled. ", $@ if $@ ; |
---|
907 | |
---|
908 | eval { |
---|
909 | $self->CreateThumbnail(); |
---|
910 | }; |
---|
911 | |
---|
912 | if($@){ |
---|
913 | $msg = sprintf( |
---|
914 | "An error has occured %s - %s\n$@", |
---|
915 | $site_name, |
---|
916 | $file |
---|
917 | ); |
---|
918 | } |
---|
919 | else{ |
---|
920 | $msg = sprintf( |
---|
921 | "Uploading %s - %s", |
---|
922 | $site_name, |
---|
923 | $file |
---|
924 | ); |
---|
925 | } |
---|
926 | eval { |
---|
927 | $self->progress_msg_refresh->($msg); |
---|
928 | $self->progressbar_refresh->(0.50); |
---|
929 | }; |
---|
930 | croak "Upload cancelled. ", $@ if $@ ; |
---|
931 | |
---|
932 | # photo metadata |
---|
933 | $self->_prepare_upload_properties(); |
---|
934 | my ( $status, $status_msg ) = $self->pwg->UploadImage(); |
---|
935 | |
---|
936 | if ( $status ){ |
---|
937 | $msg = sprintf( |
---|
938 | "%s : %s upload succcessful.", |
---|
939 | $site_name, |
---|
940 | $file |
---|
941 | ); |
---|
942 | $uploaded++; |
---|
943 | } else { |
---|
944 | $msg = sprintf( |
---|
945 | "An error has occured.\n%s : %s upload is cancelled.\n$status_msg", |
---|
946 | $site_name, |
---|
947 | $file |
---|
948 | ); |
---|
949 | $rejected++; |
---|
950 | $last_error = $status_msg; |
---|
951 | } |
---|
952 | |
---|
953 | $count++; |
---|
954 | $self->count( |
---|
955 | $count |
---|
956 | ); |
---|
957 | # update upload progress dialog |
---|
958 | eval { |
---|
959 | $self->progress_msg_refresh->($msg); |
---|
960 | $self->progressbar_refresh->(1); |
---|
961 | }; |
---|
962 | croak "Upload cancelled. ", $@ if $@ ; |
---|
963 | |
---|
964 | } |
---|
965 | @{$self->image_selection} if defined |
---|
966 | $self->image_selection; |
---|
967 | |
---|
968 | my $time_end = time; |
---|
969 | my $duration = $time_end - $time_begin; |
---|
970 | $msg = sprintf( |
---|
971 | "%s images processed\n\n%s images uploaded\n\n%s images in errors and not uploaded - $last_error\n\nDuration : %s seconds", |
---|
972 | $self->count - 1, |
---|
973 | $uploaded, |
---|
974 | $rejected, |
---|
975 | $duration, |
---|
976 | ); |
---|
977 | $self->progress_endinfo_refresh->($msg); |
---|
978 | } |
---|
979 | |
---|
980 | # if we need to rotate |
---|
981 | sub CreateHigh { |
---|
982 | my ( $self ) = @_; |
---|
983 | |
---|
984 | my $orientation = $self->current_image->exif_metadata->{Orientation}; |
---|
985 | |
---|
986 | # Valid for Rotate 180, Rotate 90 CW, Rotate 270 CW |
---|
987 | if( $self->auto_rotate and $orientation =~ m/Rotate (\d+)/ ){ |
---|
988 | |
---|
989 | my ( $vol, $dir, $file ) = File::Spec->splitpath( |
---|
990 | $self->current_image->file |
---|
991 | ); |
---|
992 | |
---|
993 | my ( $filename, $ext ) = split /\./, $file ; |
---|
994 | |
---|
995 | # high_file is a copy of original |
---|
996 | $self->current_image->site_high_file( |
---|
997 | File::Spec->catfile( |
---|
998 | $self->site_resized_dir, |
---|
999 | sprintf( |
---|
1000 | "%s_high.%s", |
---|
1001 | $filename, |
---|
1002 | $self->type, |
---|
1003 | ) |
---|
1004 | ) |
---|
1005 | ); |
---|
1006 | |
---|
1007 | my $image = Image::Magick->new(); |
---|
1008 | # we read original |
---|
1009 | my $status = $image->Read( |
---|
1010 | $self->current_image->file |
---|
1011 | ); |
---|
1012 | warn "$status ", $self->current_image->file, "\n" if $status ; |
---|
1013 | return 0 if $status; |
---|
1014 | |
---|
1015 | $image->Rotate( degrees=>$1 ); |
---|
1016 | |
---|
1017 | $image->Write( |
---|
1018 | filename=>encode('iso-8859-1', $self->current_image->site_high_file) |
---|
1019 | ); |
---|
1020 | warn "$status ", $self->current_image->site_high_file, "\n" if $status ; |
---|
1021 | return 0 if $status; |
---|
1022 | |
---|
1023 | undef $image; |
---|
1024 | |
---|
1025 | $self->_set_exif_tag( |
---|
1026 | $self->current_image->site_high_file, |
---|
1027 | 'Orientation', |
---|
1028 | 'Horizontal (normal)', |
---|
1029 | ); |
---|
1030 | |
---|
1031 | # Now all images that need to be rotated are done. Update exif |
---|
1032 | $self->current_image->exif_metadata->{Orientation} = 'Horizontal (normal)'; |
---|
1033 | } |
---|
1034 | else{ |
---|
1035 | # high file is the original file |
---|
1036 | $self->current_image->site_high_file( |
---|
1037 | $self->current_image->file |
---|
1038 | ); |
---|
1039 | } |
---|
1040 | |
---|
1041 | return 1; |
---|
1042 | } |
---|
1043 | |
---|
1044 | sub _prepare_upload_properties { |
---|
1045 | my ( $self ) = @_; |
---|
1046 | |
---|
1047 | $self->pwg->upload_high( |
---|
1048 | $self->upload_high |
---|
1049 | ); |
---|
1050 | |
---|
1051 | $self->pwg->site_high_file( |
---|
1052 | $self->current_image->site_high_file |
---|
1053 | ); |
---|
1054 | |
---|
1055 | $self->pwg->site_resized_file( |
---|
1056 | $self->current_image->site_resized_file |
---|
1057 | ); |
---|
1058 | |
---|
1059 | $self->pwg->site_thumb_file( |
---|
1060 | $self->current_image->site_thumb_file |
---|
1061 | ); |
---|
1062 | |
---|
1063 | $self->pwg->site_author( |
---|
1064 | $self->current_image->site_author |
---|
1065 | ); |
---|
1066 | |
---|
1067 | $self->pwg->site_comment( |
---|
1068 | $self->current_image->site_comment |
---|
1069 | ); |
---|
1070 | |
---|
1071 | $self->pwg->site_image_name( |
---|
1072 | $self->current_image->site_name |
---|
1073 | ); |
---|
1074 | |
---|
1075 | $self->pwg->site_img_date_creation( |
---|
1076 | $self->current_image->create_date |
---|
1077 | ); |
---|
1078 | |
---|
1079 | $self->current_image->site_categories( |
---|
1080 | $self->categories |
---|
1081 | ); |
---|
1082 | |
---|
1083 | $self->pwg->categories( |
---|
1084 | sprintf( |
---|
1085 | "%s", |
---|
1086 | join(';', @{$self->categories}) |
---|
1087 | ) |
---|
1088 | ); |
---|
1089 | |
---|
1090 | $self->pwg->tags( |
---|
1091 | #join(',', @{$self->current_image->site_tags}) |
---|
1092 | ); |
---|
1093 | |
---|
1094 | } |
---|
1095 | |
---|
1096 | # read Orientation exif tag from original image |
---|
1097 | # apply rotation to image ( preview or resize ) |
---|
1098 | sub RotateImage { |
---|
1099 | my ( $self, $file ) = @_; |
---|
1100 | |
---|
1101 | # exif from original image |
---|
1102 | my $orientation = $self->current_image->exif_metadata->{Orientation}; |
---|
1103 | |
---|
1104 | # Valid for Rotate 180, Rotate 90 CW, Rotate 270 CW |
---|
1105 | if( $orientation =~ m/Rotate (\d+)/ ){ |
---|
1106 | printf( |
---|
1107 | "Rotate %s\n", |
---|
1108 | $1 |
---|
1109 | ); |
---|
1110 | |
---|
1111 | my $image = Image::Magick->new(); |
---|
1112 | |
---|
1113 | # read resized file |
---|
1114 | my $status = $image->Read( |
---|
1115 | $file |
---|
1116 | ); |
---|
1117 | warn "$status ", $file, "\n" if $status ; |
---|
1118 | return 0 if $status; |
---|
1119 | |
---|
1120 | $image->Rotate( degrees=>$1 ); |
---|
1121 | |
---|
1122 | # write resizd file |
---|
1123 | $image->Write( |
---|
1124 | filename=>encode('iso-8859-1', $file) |
---|
1125 | ); |
---|
1126 | warn "$status ", $file, "\n" if $status ; |
---|
1127 | return 0 if $status; |
---|
1128 | |
---|
1129 | undef $image; |
---|
1130 | |
---|
1131 | } |
---|
1132 | return 1; |
---|
1133 | } |
---|
1134 | |
---|
1135 | sub GetImage { |
---|
1136 | my ( $self, $indx ) = @_; |
---|
1137 | |
---|
1138 | my $sum = $self->sums->[$indx]; |
---|
1139 | |
---|
1140 | $self->image_sums->{$sum}; |
---|
1141 | } |
---|
1142 | |
---|
1143 | 1; |
---|