source: extensions/pLoader/trunk/src/Uploader/ImageList.pm @ 3472

Last change on this file since 3472 was 3472, checked in by ronosman, 15 years ago

Feature 993 added : set Piwigo tags. Only existing tags are supported.

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