source: extras/pLoader/trunk/src/Uploader/ImageList.pm @ 3267

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

Feature 987 added: use filename or prefix to create a default photo name.

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