Changeset 3226
- Timestamp:
- Mar 29, 2009, 12:24:08 PM (15 years ago)
- Location:
- extras/pLoader/trunk/src/Uploader
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
extras/pLoader/trunk/src/Uploader/Image.pm
r3177 r3226 39 39 preview_w 40 40 preview_h 41 file_sum 41 42 / 42 43 ); -
extras/pLoader/trunk/src/Uploader/ImageList.pm
r3177 r3226 28 28 use Data::Dumper; 29 29 use Storable; 30 use Digest::MD5::File qw/file_md5_hex md5_hex/; 31 use Encode qw/encode decode is_utf8/; 32 30 33 # this class implements a collection of image files with associated data 31 34 $|=1; … … 68 71 progressbar_refresh 69 72 progress_endinfo_refresh 73 RescaleCallback 70 74 ResizeCallback 71 75 upload_rejects … … 78 82 create_resized 79 83 use_exif_preview 84 image_sums 85 sums 86 version 87 imagelist_version 80 88 / 81 89 ); … … 96 104 # If PreviewImage is available, we use it 97 105 if(defined $imagedata ) { 106 print "_write_preview_image, use exif PreviewImage\n"; 98 107 eval { 99 108 open PREVIEW_FILE, ">", $self->current_image->preview_file ; … … 126 135 } 127 136 128 sub _set_current_image_filepaths {137 sub _set_current_image_filepaths__ { 129 138 my ( $self ) = @_; 130 139 … … 134 143 135 144 my ( $filename, $ext ) = split /\./, $file ; 145 136 146 137 147 $self->current_image->wx_thumb_file( … … 156 166 ) 157 167 ); 158 168 $self->current_image->preview_file( 169 encode('iso-8859-1', $self->current_image->preview_file) 170 ); 159 171 160 172 $self->current_image->site_thumb_file( … … 169 181 ); 170 182 171 172 } 183 } 184 185 sub _set_current_image_filepaths { 186 my ( $self ) = @_; 187 188 my $filename = sprintf( 189 "%s.%s", 190 $self->current_image->file_sum, 191 $self->type, 192 ); 193 194 195 $self->current_image->wx_thumb_file( 196 File::Spec->catfile( 197 $self->wx_thumb_dir, 198 $filename 199 ) 200 ); 201 202 $self->current_image->preview_file( 203 File::Spec->catfile( 204 $self->preview_dir, 205 $filename 206 ) 207 ); 208 $self->current_image->preview_file( 209 encode('iso-8859-1', $self->current_image->preview_file) 210 ); 211 212 $self->current_image->site_thumb_file( 213 File::Spec->catfile( 214 $self->site_thumb_dir, 215 $filename 216 ) 217 ); 218 219 } 220 173 221 174 222 sub SetCurrentImage { … … 176 224 177 225 $self->current_image( 178 $self-> images->[$indx]226 $self->GetImage($indx) 179 227 ); 180 228 } … … 187 235 188 236 # if some files have been previously selected 189 my $i = scalar @{$self-> images};237 my $i = scalar @{$self->sums}; 190 238 my $count = 0; 191 239 $self->count($count); 192 240 my $errors = 0; 241 193 242 map { 194 # read exif metadata 195 my $info; 196 197 198 eval { 199 $info = ImageInfo( $_ ); 200 }; 201 202 $info = {} if($@); 203 204 $self->_add_image($_, $info, $i); 243 my $info = $self->_read_exif_metatdata($_); 244 my $is_new_image = $self->_add_image($_, $info, $i); 205 245 $self->SetCurrentImage($i); 206 246 $self->_set_current_image_filepaths(); 207 247 208 my $create_gui_preview; 209 if($self->use_exif_preview){ 210 # an exif preview is available. we try to use it 211 if(defined $info->{PreviewImage} ){ 212 printf("use preview\n"); 213 $self->_write_preview_image( $info->{PreviewImage} ); 214 my $image = new Image::Magick; 215 eval { 216 $create_gui_preview = $image->ReadImage( 217 $self->current_image->preview_file 218 ); 219 }; 220 unlink $self->current_image->preview_file if $create_gui_preview; 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"); 221 255 } 222 256 else { 223 $create_gui_preview = 1; 257 $self->progress_msg("An error has occured when processing %s\n$@"); 258 # remove from list 259 splice @{$self->sums}, $i, 1; 260 $errors++; 224 261 } 262 263 $self->SetNewFilesProgressCallback->(); 225 264 } 226 else { 227 $create_gui_preview = 1; 228 } 229 print "create gui preview", $create_gui_preview, "\n"; 230 # have to create a preview file 231 if($create_gui_preview) { 232 eval { 233 if(!$self->CreateGUIPreview()){ 234 # use method provided by the caller 235 # source, target, type, ratio 236 $self->ResizeCallback->( 237 $self->current_image->file, 238 $self->current_image->preview_file, 239 $self->type, 240 $self->preview_ratio, 241 undef, 242 undef, 243 $self->quality, 244 ); 245 } 246 };# create a preview file 247 } 248 249 $self->RotateImage( 250 $self->current_image->preview_file, 251 ) if $self->auto_rotate; 252 253 $self->_set_exif_tag( 254 $self->current_image->preview_file, 255 'Orientation', 256 'Horizontal (normal)', 257 ) if $self->auto_rotate; 258 259 260 # Now, we should have a valid preview image. 261 # try to thumbnail it 262 eval { 263 printf("create gui thumbnail\n"); 264 # use the preview image to create a gui display thumbnail 265 $self->CreateGUIThumbnail(); 266 }; 267 # ok 268 if(!$@){ 269 $self->progress_msg("Thumbnail and preview created for %s"); 270 $i++; 271 $count++; 272 $self->count($count); 273 } 274 else { 275 $self->progress_msg("An error has occured when processing %s\n$@"); 276 # remove from list 277 splice @{$self->images}, $i, 1; 278 $errors++; 279 } 280 281 $self->SetNewFilesProgressCallback->(); 265 $i++; 266 $count++; 267 $self->count($count); 282 268 $self->SetNewFilesViewerRefreshCallback->(); 283 284 269 } 285 270 @{$files}; … … 297 282 } 298 283 284 sub _read_exif_metatdata { 285 my ( $self, $file ) = @_; 286 287 # read exif metadata 288 my $info; 289 eval { 290 $info = ImageInfo( $file ); 291 }; 292 $info = {} if($@); 293 294 $info; 295 } 296 299 297 # key is file path 300 298 sub _add_image { 301 299 my ( $self, $file, $info, $i ) = @_; 302 300 303 my $image = Uploader::Image->new( 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( 304 314 { 305 file => $_, 306 site_name => sprintf( 307 "%s %s", 308 $self->default_name_prefix, 309 $i, 310 ), 311 site_author => $self->author, 312 exif_metadata => $self->_select_exif_data($info), 313 add_rank => $i, 314 site_categories => [], 315 site_tags => [], 316 site_high_file => $_, 315 file => $_, 316 file_sum => $sum, 317 site_name => sprintf( 318 "%s %s", 319 $self->default_name_prefix, 320 $i, 321 ), 322 site_author => $self->author, 323 exif_metadata => $self->_select_exif_data($info), 324 add_rank => $i, 325 site_categories => [], 326 site_tags => [], 327 site_high_file => $_, 328 } 329 ); 330 331 $self->image_sums->{$sum} = $image ; 332 $is_new_image = 1; 333 } else { 334 $image = $self->image_sums->{$sum}; 335 } 336 337 338 #$self->images->[$i] = $image ; 339 $self->sums->[$i] = $sum ; 340 341 $is_new_image; 342 } 343 344 sub _create_gui_preview { 345 my ( $self, $info ) = @_; 346 347 my $create_gui_preview; 348 my $use_wx_resize; 349 if($self->use_exif_preview){ 350 # an exif preview is available. we try to use it 351 if(defined $info->{PreviewImage} ){ 352 printf("use preview\n"); 353 $self->_write_preview_image( $info->{PreviewImage} ); 354 my $image = new Image::Magick; 355 eval { 356 $create_gui_preview = $image->ReadImage( 357 $self->current_image->preview_file 358 ); 359 }; 360 unlink $self->current_image->preview_file if $create_gui_preview; 317 361 } 318 ); 319 320 # append to image list 321 $self->images->[$i] = $image ; 322 323 } 324 362 else { 363 $create_gui_preview = 1; 364 } 365 } 366 else { 367 $create_gui_preview = 1; 368 } 369 print "create gui preview", $create_gui_preview, "\n"; 370 # have to create a preview file 371 if($create_gui_preview) { 372 eval { 373 if(!$self->CreateGUIPreview()){ 374 # use method provided by the caller 375 # source, target, type, ratio 376 print "CreateGUIPreview failed, use callback\n"; 377 $self->RescaleCallback->( 378 $self->current_image->file, 379 $self->current_image->preview_file, 380 $self->type, 381 $self->preview_ratio, 382 undef, 383 undef, 384 $self->quality, 385 ); 386 $use_wx_resize = 1; 387 } 388 };# create a preview file 389 } 390 391 $self->RotateImage( 392 $self->current_image->preview_file, 393 ) if $self->auto_rotate; 394 395 $self->_set_exif_tag( 396 $self->current_image->preview_file, 397 'Orientation', 398 'Horizontal (normal)', 399 ) if $self->auto_rotate; 400 401 $use_wx_resize; 402 } 403 404 sub _create_gui_thumbnail { 405 my ( $self, $use_wx_resize ) = @_; 406 407 # Now, we should have a valid preview image. 408 # try to thumbnail it 409 eval { 410 printf("create gui thumbnail\n"); 411 # use the preview image to create a gui display thumbnail 412 if($use_wx_resize) 413 { 414 print "CreateGUIThumbnail failed, use callback\n"; 415 $self->ResizeCallback->( 416 $self->current_image->preview_file, 417 $self->current_image->wx_thumb_file, 418 $self->type, 419 $self->wx_thumb_size, 420 $self->wx_thumb_size, 421 $self->wx_quality, 422 ); 423 424 } else { 425 $self->CreateGUIThumbnail(); 426 } 427 }; 428 } 325 429 326 430 sub RemoveImageSelection { 327 431 my ( $self ) = @_; 328 432 329 return if (! scalar @{$self-> images} );433 return if (! scalar @{$self->sums} ); 330 434 return if (! defined $self->image_selection ); 331 435 … … 333 437 my @images = reverse @{$self->image_selection}; 334 438 map { 335 my $image = $self-> images->[$_]->file;336 splice @{$self-> images}, $_, 1 ;439 my $image = $self->GetImage($_)->file; 440 splice @{$self->sums}, $_, 1 ; 337 441 $self->wx_thumb_imglist->Remove($_); 338 442 shift @images; … … 350 454 return 1 if( -e $self->current_image->wx_thumb_file ); 351 455 my $rval = 0; 352 print "CreateGUIThumbnail ", $self->current_image->wx_thumb_file, "\n";456 print "CreateGUIThumbnail ", $self->current_image->wx_thumb_file, "\n"; 353 457 my $image = new Image::Magick; 354 458 … … 401 505 "%s:%s", 402 506 $self->type, 403 $self->current_image->wx_thumb_file,507 encode('iso-8859-1', $self->current_image->wx_thumb_file), 404 508 ) 405 509 ); … … 459 563 "%s:%s", 460 564 $self->type, 461 $self->current_image->preview_file,565 encode('iso-8859-1', $self->current_image->preview_file), 462 566 ) 463 567 ); … … 537 641 "%s:%s", 538 642 $self->type, 539 $self->current_image->site_resized_file,643 encode('iso-8859-1', $self->current_image->site_resized_file), 540 644 ) 541 645 ); … … 561 665 562 666 my $status = $image->ReadImage( 563 $self->current_image->site_resized_file667 encode('iso-8859-1', $self->current_image->site_resized_file) 564 668 ); 565 669 warn "$status" if $status ; … … 589 693 "%s:%s", 590 694 $self->type, 591 $self->current_image->site_thumb_file,695 encode('iso-8859-1', $self->current_image->site_thumb_file), 592 696 ) 593 697 ); … … 652 756 wx_thumb_size 653 757 current_image 654 images655 758 exif_metadata 656 759 wx_thumb_dir … … 666 769 create_resized 667 770 use_exif_preview 771 image_sums 772 sums 773 version 774 imagelist_version 668 775 / 669 776 ] … … 702 809 # current image object 703 810 $self->current_image( 704 $self-> images->[$_]811 $self->GetImage($_) 705 812 ); 706 813 … … 711 818 my $site_name = $self->current_image->site_name; 712 819 713 my ( $filename, $ext ) = split /\./, $file ; 820 # my ( $filename, $ext ) = split /\./, $file ; 821 my $filename = $self->current_image->file_sum ; 714 822 715 823 # lately defined to make sure we have the last global properties ( resize_w, resize_h ) … … 733 841 ); 734 842 735 eval {843 # eval { 736 844 # set current image thumbnail 737 845 $self->progress_thumbnail_refresh->(); … … 741 849 # update upload progress dialog 742 850 $self->progressbar_refresh->(0.25); 743 };851 # }; 744 852 # user cancelled : dialog box is destroyed 745 croak "Upload cancelled. "if $@ ;746 853 croak "Upload cancelled. ", $@ if $@ ; 854 print "1\n"; 747 855 if( $self->create_resized ){ 748 eval {856 # eval { 749 857 if(!$self->CreateResized()){ 750 858 printf("CreateResized failed %s. Use ResizeCallback\n", $@); … … 765 873 ) if $self->auto_rotate; 766 874 } 767 };875 # }; 768 876 $self->_set_exif_tag( 769 877 $self->current_image->site_resized_file, … … 778 886 ); 779 887 } 888 print "2\n"; 780 889 781 890 … … 792 901 ); 793 902 794 eval {903 # eval { 795 904 $self->progress_msg_refresh->($msg); 796 }; 797 croak "Upload cancelled." if $@ ; 798 799 eval { 905 # }; 906 croak "Upload cancelled. ", $@ if $@ ; 907 print "3\n"; 908 909 # eval { 800 910 $self->CreateThumbnail(); 801 };911 # }; 802 912 803 913 if($@){ … … 815 925 ); 816 926 } 817 eval {927 # eval { 818 928 $self->progress_msg_refresh->($msg); 819 929 $self->progressbar_refresh->(0.50); 820 }; 821 croak "Upload cancelled." if $@ ; 930 # }; 931 croak "Upload cancelled. ", $@ if $@ ; 932 print "4\n"; 822 933 823 934 # photo metadata 824 935 $self->_prepare_upload_properties(); 936 print "5\n"; 825 937 my ( $status, $status_msg ) = $self->pwg->UploadImage(); 938 print "6\n"; 826 939 827 940 if ( $status ){ … … 847 960 ); 848 961 # update upload progress dialog 849 eval {962 # eval { 850 963 $self->progress_msg_refresh->($msg); 851 964 $self->progressbar_refresh->(1); 852 }; 853 croak "Upload cancelled." if $@ ; 965 # }; 966 croak "Upload cancelled. ", $@ if $@ ; 967 print "7\n"; 854 968 855 969 } … … 907 1021 908 1022 $image->Write( 909 filename=>$self->current_image->site_high_file1023 encode('iso-8859-1', filename=>$self->current_image->site_high_file) 910 1024 ); 911 1025 warn "$status ", $self->current_image->site_high_file, "\n" if $status ; … … 1013 1127 # write resizd file 1014 1128 $image->Write( 1015 filename=>$file1129 encode('iso-8859-1', filename=>$file) 1016 1130 ); 1017 1131 warn "$status ", $file, "\n" if $status ; … … 1024 1138 } 1025 1139 1140 sub GetImage { 1141 my ( $self, $indx ) = @_; 1142 1143 my $sum = $self->sums->[$indx]; 1144 1145 $self->image_sums->{$sum}; 1146 } 1147 1026 1148 1;
Note: See TracChangeset
for help on using the changeset viewer.