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 {
27 croak "No picture set yet" if !$thumb->{content};
28 my $image = GD::Image->new($thumb->{content});
29 my ($width, $height) = $image->getBounds;
31 my $curr_max = max $width, $height, 1;
32 my $factor = $curr_max <= $max_dim ? 1 : $curr_max / $max_dim;
33 my $new_width = int($width / $factor + 0.5);
34 my $new_height = int($height / $factor + 0.5);
35 my $thumbnail = GD::Image->new($new_width, $new_height);
37 $thumbnail->copyResized($image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
39 $thumb->{thumbnail_img_content} = $thumbnail->png;
40 $thumb->{thumbnail_img_content_type} = "image/png";
41 $thumb->{thumbnail_img_width} = $new_width;
42 $thumb->{thumbnail_img_height} = $new_height;
47 sub file_update_thumbnail {
50 return 1 if !$self->file_content || !$self->file_content_type || !Rose::DB::Object::Util::get_column_value_modified($self, 'file_content');
51 $self->file_create_thumbnail;
55 sub file_probe_image_type {
56 my ($self, $mime_type, $basefile) = @_;
58 if ( !$supported_mime_types{ $mime_type } ) {
59 $self->js->flash('error',t8('file \'#1\' has unsupported image type \'#2\' (supported types: #3)',
60 $basefile, $mime_type, join(' ', sort keys %supported_mime_types)));
68 return (t8("No file uploaded yet")) if !$content;
69 my $info = Image::Info::image_info(\$content);
70 if (!$info || $info->{error} || !$info->{file_media_type} || !$supported_mime_types{ $info->{file_media_type} }) {
71 $::lxdebug->warn("Image::Info error: " . $info->{error}) if $info && $info->{error};
72 return (t8('Unsupported image type (supported types: #1)', join(' ', sort keys %supported_mime_types)));
76 $thumbnail->{file_content_type} = $info->{file_media_type};
77 $thumbnail->{file_image_width} = $info->{width};
78 $thumbnail->{file_image_height} = $info->{height};
79 $thumbnail->{content} = $content;
81 $thumbnail = &file_create_thumbnail($thumbnail);
86 sub file_update_type_and_dimensions {
89 return () if !$self->file_content;
90 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');
92 my @errors = $self->file_probe_type;
93 return @errors if @errors;
95 my $info = $supported_mime_types{ $self->file_content_type };
96 if ($info->{convert_to_png}) {
97 $self->file_content(GD::Image->new($self->file_content)->png);
98 $self->file_content_type('image/png');
99 $self->filename(apply { s/\.[^\.]+$//; $_ .= '.png'; } $self->filename);
113 SL::DB::Helper::ThumbnailCreator - DatabaseClass Helper for Fileuploads
117 use SL::DB::Helper::ThumbnailCreator;
123 # longer description..
127 Werner Hahn E<lt>wh@futureworldsearch.netE<gt>