Dateimanagement: Controller zum Laden und Generierung der Dateien
[kivitendo-erp.git] / SL / Controller / Helper / ThumbnailCreator.pm
1 package SL::Controller::Helper::ThumbnailCreator;
2
3 use strict;
4
5 use SL::Locale::String qw(t8);
6 use Carp;
7 use GD;
8 use Image::Info;
9 use File::MimeInfo::Magic;
10 use List::MoreUtils qw(apply);
11 use List::Util qw(max);
12 use Rose::DB::Object::Util;
13
14 require Exporter;
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);
17
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'},
24 );
25
26 sub file_create_thumbnail {
27   my ($self) = @_;
28   croak "No picture set yet" if !$self->file_content;
29
30   my $image            = GD::Image->new($self->file_content);
31   my ($width, $height) = $image->getBounds;
32   my $max_dim          = 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);
38
39   $thumbnail->copyResized($image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
40
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);
45   return 1;
46
47 }
48
49 sub file_update_thumbnail {
50   my ($self) = @_;
51
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;
54   return 1;
55 }
56
57 sub file_probe_image_type {
58   my ($self, $mime_type, $basefile) = @_;
59
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)));
63     return 1;
64   }
65   return 0;
66 }
67
68 sub file_probe_type {
69   my ($self) = @_;
70
71   return (t8("No file uploaded yet")) if !$self->file_content;
72   my $mime_type = File::MimeInfo::Magic::magic($self->file_content);
73
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)));
78   }
79
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);
84
85   $self->file_create_thumbnail;
86
87   return ();
88 }
89
90 sub file_update_type_and_dimensions {
91   my ($self) = @_;
92
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');
95
96   my @errors = $self->file_probe_type;
97   return @errors if @errors;
98
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);
104   }
105   return ();
106 }
107
108 1;
109 __END__
110
111 =pod
112
113 =encoding utf8
114
115 =head1 NAME
116
117   SL::DB::Helper::ThumbnailCreator - DatabaseClass Helper for Fileuploads
118
119 =head1 SYNOPSIS
120
121   use SL::DB::Helper::ThumbnailCreator;
122
123   # synopsis...
124
125 =head1 DESCRIPTION
126
127   # longer description..
128
129 =head1 AUTHOR
130
131   Werner Hahn E<lt>wh@futureworldsearch.netE<gt>
132
133 =cut
134
135
136 =head1 INTERFACE
137
138
139 =head1 DEPENDENCIES
140
141
142 =head1 SEE ALSO
143
144