Merge branch 'b-3.6.1' of ../kivitendo-erp_20220811
[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 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'},
23 );
24
25 sub file_create_thumbnail {
26   my ($thumb, %params) = @_;
27
28   croak "No picture set yet" if !$thumb->{content};
29
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);
38
39   $thumbnail->copyResized($image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
40
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;
45   return $thumb;
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 ($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)));
75   }
76
77   my $thumbnail;
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;
82
83   $thumbnail = &file_create_thumbnail($thumbnail, %params);
84
85   return $thumbnail;
86 }
87
88 sub file_update_type_and_dimensions {
89   my ($self) = @_;
90
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');
93
94   my @errors = $self->file_probe_type;
95   return @errors if @errors;
96
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);
102   }
103   return ();
104 }
105
106 1;
107
108 __END__
109
110 =pod
111
112 =encoding utf8
113
114 =head1 NAME
115
116 SL::Controller::Helper::ThumbnailCreator - Helper for Fileuploads
117
118 =head1 SYNOPSIS
119
120 use SL::Controller::Helper::ThumbnailCreator;
121
122 =head1 AUTHOR
123
124 Werner Hahn E<lt>wh@futureworldsearch.netE<gt>