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 # TODO PDFs and others like odt,txt,...
 
  19 our %supported_mime_types = (
 
  20   'image/gif'  => { extension => 'gif', convert_to_png => 1, },
 
  21   'image/png'  => { extension => 'png' },
 
  22   'image/jpeg' => { extension => 'jpg' },
 
  23   'image/tiff' => { extension => 'tif'},
 
  26 sub file_create_thumbnail {
 
  28   croak "No picture set yet" if !$self->file_content;
 
  30   my $image            = GD::Image->new($self->file_content);
 
  31   my ($width, $height) = $image->getBounds;
 
  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   $self->thumbnail_img_content($thumbnail->png);
 
  42   $self->thumbnail_img_content_type('image/png');
 
  43   $self->thumbnail_img_width($new_width);
 
  44   $self->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)));
 
  71   return (t8("No file uploaded yet")) if !$self->file_content;
 
  72   my $mime_type = File::MimeInfo::Magic::magic($self->file_content);
 
  74   my $info = Image::Info::image_info(\$self->{file_content});
 
  75   if (!$info || $info->{error} || !$info->{file_media_type} || !$supported_mime_types{ $info->{file_media_type} }) {
 
  76     $::lxdebug->warn("Image::Info error: " . $info->{error}) if $info && $info->{error};
 
  77     return (t8('Unsupported image type (supported types: #1)', join(' ', sort keys %supported_mime_types)));
 
  80   $self->file_content_type($info->{file_media_type});
 
  81   $self->files_img_width($info->{width});
 
  82   $self->files_img_height($info->{height});
 
  83   $self->files_mtime(DateTime->now_local);
 
  85   $self->file_create_thumbnail;
 
  90 sub file_update_type_and_dimensions {
 
  93   return () if !$self->file_content;
 
  94   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');
 
  96   my @errors = $self->file_probe_type;
 
  97   return @errors if @errors;
 
  99   my $info = $supported_mime_types{ $self->file_content_type };
 
 100   if ($info->{convert_to_png}) {
 
 101     $self->file_content(GD::Image->new($self->file_content)->png);
 
 102     $self->file_content_type('image/png');
 
 103     $self->filename(apply { s/\.[^\.]+$//;  $_ .= '.png'; } $self->filename);
 
 117   SL::DB::Helper::ThumbnailCreator - DatabaseClass Helper for Fileuploads
 
 121   use SL::DB::Helper::ThumbnailCreator;
 
 127   # longer description..
 
 131   Werner Hahn E<lt>wh@futureworldsearch.netE<gt>