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

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

Change how pLoader identifies images : now use md5 checksum - feature 961
Fix filename with accentuated characters causing processing failures - bug 952

  • Property svn:eol-style set to LF
File size: 28.1 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/;
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
92sub Init {
93    my ( $self ) = @_; 
94}
95
96
97
98# save exif preview image if available
99# otherwise create a preview image
100sub _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
119sub _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
137sub _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
185sub _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
222sub SetCurrentImage {
223    my ( $self, $indx ) = @_;   
224
225    $self->current_image(
226        $self->GetImage($indx)
227    );
228}
229
230
231sub 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
284sub _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
298sub _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
344sub _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
404sub _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
430sub 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 )
451sub 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    $status = $image->Set(background=>"white");
485    warn "$status" if $status ;
486
487    $status = $image->Set(Gravity=>"Center");
488    warn "$status" if $status ;
489
490    $image->Extent(
491        geometry=>sprintf("%sx%s", $size, $size),
492        gravity=>'center',
493    );
494
495    $image->Set(
496        quality=>$self->wx_quality
497    );
498
499    $status = $image->Strip();
500    warn "$status" if $status ;
501   
502
503    $image->Write(
504        sprintf(
505            "%s:%s",
506            $self->type,
507            encode('iso-8859-1', $self->current_image->wx_thumb_file),
508        )
509    );
510
511    undef $image;
512   
513    $rval = 1;
514   
515    return $rval;
516}
517
518
519sub CreateGUIPreview {
520    my ( $self ) = @_;
521    printf("CreateGUIPreview %s\n", $self->current_image->preview_file );
522    return 1 if( -e $self->current_image->preview_file );
523   
524    my $rval = 1;
525
526    my $image = Image::Magick->new();
527
528    my $ratio = $self->preview_ratio;
529
530
531    my $status = $image->Read(
532        sprintf(
533            "%s",
534            $self->current_image->file,
535        )
536    );
537    warn "$status ", $self->current_image->file, "\n" if $status ;
538    return 0 if $status;
539
540    $status = $image->Thumbnail(
541        geometry=>sprintf(
542                              "%s%%x%s%%>", 
543                              $ratio, 
544                              $ratio
545                         )
546    );
547    warn "$status" if $status ;
548    return 0 if $status;
549
550
551    $status = $image->Set(background=>"white");
552    warn "$status" if $status ;
553
554    $status = $image->Set(Gravity=>"Center");
555    warn "$status" if $status ;
556
557
558    $image->Set(quality=>$self->wx_quality);
559
560
561    $status = $image->Write(
562        sprintf(
563            "%s:%s",
564            $self->type,
565            encode('iso-8859-1', $self->current_image->preview_file),
566        )
567    );
568    warn "$status" if $status ;
569    return 0 if $status;
570   
571    undef $image;
572
573    return $rval;
574}
575
576
577sub CreateResized {
578    my ( $self ) = @_;
579   
580    my $rval = 1 ;
581    return $rval if( -e $self->current_image->site_resized_file );
582
583    printf(
584        "Create resized %s\n",
585        $self->current_image->file,
586    );     
587
588    my $image = new Image::Magick;
589
590    my $status = $image->ReadImage(
591        $self->current_image->file
592    );
593    warn "$status" if $status ;
594    return 0 if $status;
595
596    my $w = $image->Get('width');
597    my $h = $image->Get('height');
598       
599    # should calculate the aspect ratio
600    my $resize_w = $self->resize_w;
601    my $resize_h = $self->resize_h;
602       
603    if( $w < $h ){
604        my $resize_w_ = $resize_w;
605        $resize_w = $resize_h;
606        $resize_h = $resize_w_;
607    }
608   
609    $status = $image->Resize(
610        geometry => sprintf("%sx%s>", $resize_w, $resize_h), 
611        filter => sprintf("%s", $self->filter), 
612        blur => $self->blur
613    );
614    warn "$status" if $status ;
615    return 0 if $status;
616
617    $status = $image->Set(Gravity=>"Center");
618    warn "$status" if $status ;
619
620    # exif from original image
621    my $orientation = $self->current_image->exif_metadata->{Orientation};
622   
623    # Valid for Rotate 180, Rotate 90 CW, Rotate 270 CW
624    if( $orientation =~ m/Rotate (\d+)/ ){
625        printf(
626            "Rotate %s\n",
627            $1
628        );
629   
630        $image->Rotate( degrees=>$1 ); 
631    }
632
633    $status = $image->Set(quality=>$self->quality);
634    warn "$status" if $status ;
635
636    $status = $image->Set(interlace=>$self->interlace);
637    warn "$status" if $status ;
638
639    $image->Write(
640        sprintf(
641            "%s:%s",
642            $self->type,
643            encode('iso-8859-1', $self->current_image->site_resized_file),
644        )
645    );
646    warn "$status" if $status ;
647    return 0 if $status;
648   
649    undef $image;
650
651   
652    $rval = 0 if $status;
653
654    return $rval;
655}
656
657sub CreateThumbnail {
658    my ( $self ) = @_;
659   
660    return 1 if( -e $self->current_image->site_thumb_file );
661   
662    my $rval = 1;
663
664    my $image = new Image::Magick;
665
666    my $status = $image->ReadImage(
667        encode('iso-8859-1', $self->current_image->site_resized_file)
668    );
669    warn "$status" if $status ;
670
671   
672    $status = $image->Resize(
673        geometry => sprintf(
674                                "%sx%s>", 
675                                $self->thumb_size, 
676                                $self->thumb_size
677                           ),
678    );
679    warn "$status" if $status ;
680
681    $status = $image->Set(Gravity=>"Center");
682    warn "$status" if $status ;
683
684    $status = $image->Set(quality=>$self->th_quality);
685    warn "$status" if $status ;
686
687    $status = $image->Strip();
688    warn "$status" if $status ;
689
690
691    $image->Write(
692        sprintf(
693            "%s:%s",
694            $self->type,
695            encode('iso-8859-1', $self->current_image->site_thumb_file),
696        )
697    );
698   
699    undef $image;
700
701
702    $rval = 0 if $status;
703
704    return $rval;
705}
706
707
708
709sub _select_exif_data {
710    my ( $self, $exif ) = @_;
711
712    return {
713        map {
714            $_ => $exif->{$_},
715        }
716        qw/
717            CreateDate
718            ImageWidth
719            ImageHeight
720            Orientation
721            DateTimeOriginal
722            ISO
723            ExposureTime
724            ApertureValue
725            FocalLength
726            Lens
727            Exposure
728            Make
729            Model
730        /
731    };   
732}
733
734sub Store {
735    my ( $self ) = @_;
736   
737    my $data = $self->get_storable(
738        [ 
739            qw/
740                images
741                thumb_size
742                preview_ratio
743                type
744                filter
745                blur
746                quality
747                wx_quality
748                th_quality
749                prefix
750                author
751                count
752                resize_w
753                resize_h
754                new_files
755                storable_file
756                wx_thumb_size
757                current_image
758                exif_metadata
759                wx_thumb_dir
760                preview_dir
761                site_resized_dir
762                site_thumb_dir
763                userdata_dir
764                progress_msg
765                default_name_prefix
766                upload_high
767                auto_rotate
768                interlace
769                create_resized
770                use_exif_preview
771                image_sums
772                sums
773                version
774                imagelist_version
775            /
776        ] 
777   
778    );
779    eval {
780        store $data, $self->storable_file;
781    };
782    if($@){
783        print $@, "\n"; 
784    }
785}
786
787
788
789sub UploadSelection {
790    my ( $self ) = @_; 
791
792    my $viewer_callback = $self->UploadImagesViewerCallback ;
793
794
795    $self->upload_rejects(
796        []
797    );
798
799    my $count = 1;
800    my $msg;
801    $self->count(
802        $count
803    );
804    my $uploaded = 0;
805    my $rejected = 0;
806    my $time_begin = time;
807    my $last_error;
808    map {
809        # current image object         
810        $self->current_image(
811            $self->GetImage($_)
812        );
813
814        my ( $vol, $dir, $file ) = File::Spec->splitpath(
815            $self->current_image->file
816        );
817       
818        my $site_name = $self->current_image->site_name;
819   
820#        my ( $filename, $ext ) = split /\./, $file ;
821        my $filename = $self->current_image->file_sum ;
822
823        # lately defined to make sure we have the last global properties ( resize_w, resize_h )
824        $self->current_image->site_resized_file( 
825            File::Spec->catfile(
826                $self->site_resized_dir,
827                sprintf(
828                    "%s_%sx%s.%s",
829                    $filename,
830                    $self->resize_w,
831                    $self->resize_h,
832                    $self->type,
833                )
834            )
835        );
836       
837        $msg = sprintf(
838            "Preparing resized image for %s - %s",
839            $site_name,
840            $file,
841        ); 
842
843#       eval {
844            # set current image thumbnail
845            $self->progress_thumbnail_refresh->();
846
847            $self->progress_msg_refresh->($msg);
848   
849            # update upload progress dialog
850            $self->progressbar_refresh->(0.25);
851#        };
852        # user cancelled : dialog box is destroyed
853        croak "Upload cancelled. ", $@ if $@ ;
854print "1\n";
855        if( $self->create_resized ){
856#            eval {
857                if(!$self->CreateResized()){
858                    printf("CreateResized failed %s. Use ResizeCallback\n", $@);
859                    # use method provided by the caller
860                    # source, target, type, ratio, width, $height
861                    $self->ResizeCallback->(
862                        $self->current_image->file,
863                        $self->current_image->site_resized_file,
864                        $self->type,
865                        undef,
866                        $self->resize_w,
867                        $self->resize_h,
868                        $self->quality,
869                    );
870               
871                    $self->RotateImage(
872                        $self->current_image->site_resized_file,
873                    ) if $self->auto_rotate;
874                }
875#            };
876            $self->_set_exif_tag(
877                $self->current_image->site_resized_file,
878                'Orientation',
879                'Horizontal (normal)',
880            ) if $self->auto_rotate;
881        }
882        # the original is at the right size, no need to create a resize
883        else {
884            $self->current_image->site_resized_file(
885                $self->current_image->file,
886            );
887        }
888print "2\n";
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 $@ ;
907print "3\n";
908
909#       eval {
910            $self->CreateThumbnail();
911#        };
912
913        if($@){
914            $msg = sprintf(
915                "An error has occured %s - %s\n$@",
916                $site_name,
917                $file
918            );
919        }
920        else{
921            $msg = sprintf(
922                "Uploading %s - %s",
923                $site_name,
924                $file
925            );
926        }
927#       eval {
928            $self->progress_msg_refresh->($msg);
929            $self->progressbar_refresh->(0.50);
930#        };
931        croak "Upload cancelled. ", $@ if $@ ;
932print "4\n";
933
934        # photo metadata
935        $self->_prepare_upload_properties();           
936print "5\n";
937        my ( $status, $status_msg ) = $self->pwg->UploadImage();
938print "6\n";
939
940        if ( $status ){
941            $msg = sprintf(
942                "%s : %s upload succcessful.",
943                $site_name,
944                $file
945            );
946            $uploaded++;
947        } else {
948            $msg = sprintf(
949                "An error has occured.\n%s : %s upload is cancelled.\n$status_msg",
950                $site_name,
951                $file
952            );
953            $rejected++;
954            $last_error = $status_msg;
955        }       
956       
957        $count++;
958        $self->count(
959            $count
960        );
961        # update upload progress dialog
962#       eval {
963            $self->progress_msg_refresh->($msg);
964            $self->progressbar_refresh->(1);
965#        };
966        croak "Upload cancelled. ", $@ if $@ ;
967print "7\n";
968       
969    }
970    @{$self->image_selection} if defined 
971        $self->image_selection;
972
973    my $time_end = time;
974    my $duration = $time_end - $time_begin;
975    $msg = sprintf(
976        "%s images processed\n\n%s images uploaded\n\n%s images in errors and not uploaded - $last_error\n\nDuration : %s seconds",
977        $self->count - 1,
978        $uploaded,
979        $rejected,
980        $duration,
981    );
982    $self->progress_endinfo_refresh->($msg);
983}
984
985# if we need to rotate
986sub CreateHigh {
987    my ( $self ) = @_;
988
989    my $orientation = $self->current_image->exif_metadata->{Orientation};
990   
991    # Valid for Rotate 180, Rotate 90 CW, Rotate 270 CW
992    if( $self->auto_rotate and $orientation =~ m/Rotate (\d+)/ ){
993
994        my ( $vol, $dir, $file ) = File::Spec->splitpath(
995            $self->current_image->file
996        );
997   
998        my ( $filename, $ext ) = split /\./, $file ;
999   
1000        # high_file is a copy of original
1001        $self->current_image->site_high_file( 
1002            File::Spec->catfile(
1003                $self->site_resized_dir,
1004                sprintf(
1005                    "%s_high.%s",
1006                    $filename,
1007                    $self->type,
1008                )
1009            )
1010        );
1011
1012        my $image = Image::Magick->new();
1013        # we read original
1014        my $status = $image->Read(
1015            $self->current_image->file
1016        );
1017        warn "$status ", $self->current_image->file, "\n" if $status ;
1018        return 0 if $status;
1019
1020        $image->Rotate( degrees=>$1 ); 
1021       
1022        $image->Write(
1023            encode('iso-8859-1', filename=>$self->current_image->site_high_file)
1024        );
1025        warn "$status ", $self->current_image->site_high_file, "\n" if $status ;
1026        return 0 if $status;
1027       
1028        undef $image;
1029
1030        $self->_set_exif_tag(
1031            $self->current_image->site_high_file,
1032            'Orientation',
1033            'Horizontal (normal)',
1034        );
1035
1036        # Now all images that need to be rotated are done. Update exif
1037        $self->current_image->exif_metadata->{Orientation} = 'Horizontal (normal)';
1038    }
1039    else{
1040        # high file is the original file
1041        $self->current_image->site_high_file(
1042            $self->current_image->file
1043        );
1044    }
1045
1046    return 1;
1047}
1048
1049sub _prepare_upload_properties {
1050    my ( $self ) = @_;
1051   
1052    $self->pwg->upload_high(
1053        $self->upload_high
1054    );
1055
1056    $self->pwg->site_high_file(
1057        $self->current_image->site_high_file
1058    );
1059
1060    $self->pwg->site_resized_file(
1061        $self->current_image->site_resized_file
1062    );
1063
1064    $self->pwg->site_thumb_file(
1065        $self->current_image->site_thumb_file
1066    );
1067
1068    $self->pwg->site_author(
1069        $self->current_image->site_author
1070    );
1071
1072    $self->pwg->site_comment(
1073        $self->current_image->site_comment
1074    );
1075
1076    $self->pwg->site_image_name(
1077        $self->current_image->site_name
1078    );
1079
1080    $self->pwg->site_img_date_creation(
1081        $self->current_image->create_date
1082    );
1083
1084    $self->current_image->site_categories(
1085        $self->categories
1086    );
1087
1088    $self->pwg->categories(
1089        sprintf(
1090            "%s",
1091            join(';', @{$self->categories})
1092        )
1093    );
1094
1095    $self->pwg->tags(
1096        #join(',', @{$self->current_image->site_tags})
1097    );
1098       
1099}
1100
1101# read Orientation exif tag from original image
1102# apply rotation to image ( preview or resize )
1103sub RotateImage {
1104    my ( $self, $file ) = @_;
1105   
1106    # exif from original image
1107    my $orientation = $self->current_image->exif_metadata->{Orientation};
1108   
1109    # Valid for Rotate 180, Rotate 90 CW, Rotate 270 CW
1110    if( $orientation =~ m/Rotate (\d+)/ ){
1111        printf(
1112            "Rotate %s\n",
1113            $1
1114        );
1115
1116        my $image = Image::Magick->new();
1117       
1118        # read resized file
1119        my $status = $image->Read(
1120            $file
1121        );
1122        warn "$status ", $file, "\n" if $status ;
1123        return 0 if $status;
1124   
1125        $image->Rotate( degrees=>$1 ); 
1126       
1127        # write resizd file
1128        $image->Write(
1129            encode('iso-8859-1', filename=>$file)
1130        );
1131        warn "$status ", $file, "\n" if $status ;
1132        return 0 if $status;
1133       
1134        undef $image;
1135   
1136    }   
1137    return 1;
1138}
1139
1140sub GetImage {
1141    my ( $self, $indx ) = @_;
1142   
1143    my $sum = $self->sums->[$indx];
1144
1145    $self->image_sums->{$sum};
1146}
1147
11481;
Note: See TracBrowser for help on using the repository browser.