1 package SL::Controller::Helper::ThumbnailCreator;
 
   5 use SL::Locale::String qw(t8);
 
   9 use File::MimeInfo::Magic;
 
  10 use List::MoreUtils qw(apply);
 
  11 use List::Util qw(max);
 
  12 use Rose::DB::Object::Util;
 
  15 our @ISA      = qw(Exporter);
 
  16 our @EXPORT   = qw(file_create_thumbnail file_update_thumbnail file_probe_type file_probe_image_type file_update_type_and_dimensions);
 
  18 our %supported_mime_types = (
 
  19   'image/gif'  => { extension => 'gif', convert_to_png => 1, },
 
  20   'image/png'  => { extension => 'png' },
 
  21   'image/jpeg' => { extension => 'jpg' },
 
  22   'image/tiff' => { extension => 'tif'},
 
  25 sub file_create_thumbnail {
 
  26   my ($thumb, %params) = @_;
 
  28   croak "No picture set yet" if !$thumb->{content};
 
  30   my $image            = GD::Image->new($thumb->{content});
 
  31   my ($width, $height) = $image->getBounds;
 
  32   my $max_dim          = $params{size} // 64;
 
  33   my $curr_max         = max $width, $height, 1;
 
  34   my $factor           = $curr_max <= $max_dim ? 1 : $curr_max / $max_dim;
 
  35   my $new_width        = int($width  / $factor + 0.5);
 
  36   my $new_height       = int($height / $factor + 0.5);
 
  37   my $thumbnail        = GD::Image->new($new_width, $new_height);
 
  39   $thumbnail->copyResized($image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
 
  41   $thumb->{thumbnail_img_content} = $thumbnail->png;
 
  42   $thumb->{thumbnail_img_content_type} = "image/png";
 
  43   $thumb->{thumbnail_img_width} = $new_width;
 
  44   $thumb->{thumbnail_img_height} = $new_height;
 
  49 sub file_update_thumbnail {
 
  52   return 1 if !$self->file_content || !$self->file_content_type || !Rose::DB::Object::Util::get_column_value_modified($self, 'file_content');
 
  53   $self->file_create_thumbnail;
 
  57 sub file_probe_image_type {
 
  58   my ($self, $mime_type, $basefile) = @_;
 
  60   if ( !$supported_mime_types{ $mime_type } ) {
 
  61     $self->js->flash('error',t8('file \'#1\' has unsupported image type \'#2\' (supported types: #3)',
 
  62                                 $basefile, $mime_type, join(' ', sort keys %supported_mime_types)));
 
  69   my ($content, %params) = @_;
 
  70   return (t8("No file uploaded yet")) if !$content;
 
  71   my $info = Image::Info::image_info(\$content);
 
  72   if (!$info || $info->{error} || !$info->{file_media_type} || !$supported_mime_types{ $info->{file_media_type} }) {
 
  73     $::lxdebug->warn("Image::Info error: " . $info->{error}) if $info && $info->{error};
 
  74     return (t8('Unsupported image type (supported types: #1)', join(' ', sort keys %supported_mime_types)));
 
  78   $thumbnail->{file_content_type} = $info->{file_media_type};
 
  79   $thumbnail->{file_image_width} = $info->{width};
 
  80   $thumbnail->{file_image_height} = $info->{height};
 
  81   $thumbnail->{content} = $content;
 
  83   $thumbnail = &file_create_thumbnail($thumbnail, %params);
 
  88 sub file_update_type_and_dimensions {
 
  91   return () if !$self->file_content;
 
  92   return () if $self->file_content_type && $self->files_img_width && $self->files_img_height && !Rose::DB::Object::Util::get_column_value_modified($self, 'file_content');
 
  94   my @errors = $self->file_probe_type;
 
  95   return @errors if @errors;
 
  97   my $info = $supported_mime_types{ $self->file_content_type };
 
  98   if ($info->{convert_to_png}) {
 
  99     $self->file_content(GD::Image->new($self->file_content)->png);
 
 100     $self->file_content_type('image/png');
 
 101     $self->filename(apply { s/\.[^\.]+$//;  $_ .= '.png'; } $self->filename);
 
 116 SL::Controller::Helper::ThumbnailCreator - Helper for Fileuploads
 
 120 use SL::Controller::Helper::ThumbnailCreator;
 
 124 Werner Hahn E<lt>wh@futureworldsearch.netE<gt>