From 60566f88588ad476cfe20195ef91b4e2ff9586b6 Mon Sep 17 00:00:00 2001 From: Werner Hahn Date: Fri, 22 Sep 2017 02:49:40 +0200 Subject: [PATCH] WebshopApi: ThumbnailCreator --- SL/Controller/Helper/ThumbnailCreator.pm | 40 ++++---- SL/DB/Helper/ThumbnailCreator.pm | 124 +++++++++++++++++++++++ 2 files changed, 142 insertions(+), 22 deletions(-) create mode 100644 SL/DB/Helper/ThumbnailCreator.pm diff --git a/SL/Controller/Helper/ThumbnailCreator.pm b/SL/Controller/Helper/ThumbnailCreator.pm index 072d89d76..6f3099871 100644 --- a/SL/Controller/Helper/ThumbnailCreator.pm +++ b/SL/Controller/Helper/ThumbnailCreator.pm @@ -15,7 +15,6 @@ require Exporter; our @ISA = qw(Exporter); our @EXPORT = qw(file_create_thumbnail file_update_thumbnail file_probe_type file_probe_image_type file_update_type_and_dimensions); -# TODO PDFs and others like odt,txt,... our %supported_mime_types = ( 'image/gif' => { extension => 'gif', convert_to_png => 1, }, 'image/png' => { extension => 'png' }, @@ -24,10 +23,9 @@ our %supported_mime_types = ( ); sub file_create_thumbnail { - my ($self) = @_; - croak "No picture set yet" if !$self->file_content; - - my $image = GD::Image->new($self->file_content); + my ($thumb) = @_; + croak "No picture set yet" if !$thumb->{content}; + my $image = GD::Image->new($thumb->{content}); my ($width, $height) = $image->getBounds; my $max_dim = 64; my $curr_max = max $width, $height, 1; @@ -38,11 +36,11 @@ sub file_create_thumbnail { $thumbnail->copyResized($image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); - $self->thumbnail_img_content($thumbnail->png); - $self->thumbnail_img_content_type('image/png'); - $self->thumbnail_img_width($new_width); - $self->thumbnail_img_height($new_height); - return 1; + $thumb->{thumbnail_img_content} = $thumbnail->png; + $thumb->{thumbnail_img_content_type} = "image/png"; + $thumb->{thumbnail_img_width} = $new_width; + $thumb->{thumbnail_img_height} = $new_height; + return $thumb; } @@ -66,25 +64,23 @@ sub file_probe_image_type { } sub file_probe_type { - my ($self) = @_; - - return (t8("No file uploaded yet")) if !$self->file_content; - my $mime_type = File::MimeInfo::Magic::magic($self->file_content); - - my $info = Image::Info::image_info(\$self->{file_content}); + my ($content) = @_; + return (t8("No file uploaded yet")) if !$content; + my $info = Image::Info::image_info(\$content); if (!$info || $info->{error} || !$info->{file_media_type} || !$supported_mime_types{ $info->{file_media_type} }) { $::lxdebug->warn("Image::Info error: " . $info->{error}) if $info && $info->{error}; return (t8('Unsupported image type (supported types: #1)', join(' ', sort keys %supported_mime_types))); } - $self->file_content_type($info->{file_media_type}); - $self->files_img_width($info->{width}); - $self->files_img_height($info->{height}); - $self->files_mtime(DateTime->now_local); + my $thumbnail; + $thumbnail->{file_content_type} = $info->{file_media_type}; + $thumbnail->{file_image_width} = $info->{width}; + $thumbnail->{file_image_height} = $info->{height}; + $thumbnail->{content} = $content; - $self->file_create_thumbnail; + $thumbnail = &file_create_thumbnail($thumbnail); - return (); + return $thumbnail; } sub file_update_type_and_dimensions { diff --git a/SL/DB/Helper/ThumbnailCreator.pm b/SL/DB/Helper/ThumbnailCreator.pm new file mode 100644 index 000000000..40d861aea --- /dev/null +++ b/SL/DB/Helper/ThumbnailCreator.pm @@ -0,0 +1,124 @@ +package SL::DB::Helper::ThumbnailCreator; + +use strict; + +use parent qw(SL::Controller::Base); + +use SL::Locale::String qw(t8); +use Carp; +use GD; +use Image::Info; +use File::MimeInfo::Magic; +use List::MoreUtils qw(apply); +use List::Util qw(max); +use Rose::DB::Object::Util; + +require Exporter; +our @ISA = qw(Exporter); +our @EXPORT = qw(file_create_thumbnail file_update_thumbnail file_probe_type file_update_type_and_dimensions); + +# TODO PDFs and others like odt,txt,... +our %supported_mime_types = ( + 'image/gif' => { extension => 'gif', convert_to_png => 1, }, + 'image/png' => { extension => 'png' }, + 'image/jpeg' => { extension => 'jpg' }, + 'image/tiff' => { extension => 'tif'}, +); + +sub file_create_thumbnail { + my ($self) = @_; + croak "No picture set yet" if !$self->file_content; + + my $image = GD::Image->new($self->file_content); + my ($width, $height) = $image->getBounds; + my $max_dim = 64; + my $curr_max = max $width, $height, 1; + my $factor = $curr_max <= $max_dim ? 1 : $curr_max / $max_dim; + my $new_width = int($width / $factor + 0.5); + my $new_height = int($height / $factor + 0.5); + my $thumbnail = GD::Image->new($new_width, $new_height); + + $thumbnail->copyResized($image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); + + $self->thumbnail_img_content($thumbnail->png); + $self->thumbnail_img_content_type('image/png'); + $self->thumbnail_img_width($new_width); + $self->thumbnail_img_height($new_height); + return 1; + +} + +sub file_update_thumbnail { + my ($self) = @_; + + return 1 if !$self->file_content || !$self->file_content_type || !Rose::DB::Object::Util::get_column_value_modified($self, 'file_content'); + $self->file_create_thumbnail; + return 1; +} + +sub file_probe_type { + my ($self) = @_; + + return (t8("No file uploaded yet")) if !$self->file_content; + my $mime_type = File::MimeInfo::Magic::magic($self->file_content); + + my $info = Image::Info::image_info(\$self->{file_content}); + if (!$info || $info->{error} || !$info->{file_media_type} || !$supported_mime_types{ $info->{file_media_type} }) { + $::lxdebug->warn("Image::Info error: " . $info->{error}) if $info && $info->{error}; + return (t8('Unsupported image type (supported types: #1)', join(' ', sort keys %supported_mime_types))); + } + + $self->file_content_type($info->{file_media_type}); + $self->files_img_width($info->{width}); + $self->files_img_height($info->{height}); + $self->files_mtime(DateTime->now_local); + + $self->file_create_thumbnail; + + return (); +} + +sub file_update_type_and_dimensions { + my ($self) = @_; + + return () if !$self->file_content; + 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'); + + my @errors = $self->file_probe_type; + return @errors if @errors; + + my $info = $supported_mime_types{ $self->file_content_type }; + if ($info->{convert_to_png}) { + $self->file_content(GD::Image->new($self->file_content)->png); + $self->file_content_type('image/png'); + $self->filename(apply { s/\.[^\.]+$//; $_ .= '.png'; } $self->filename); + } + return (); +} + +1; +__END__ + +=pod + +=encoding utf8 + +=head1 NAME + +SL::DB::Helper::ThumbnailCreator - DatabaseClass Helper for Fileuploads + +=head1 SYNOPSIS + +use SL::DB::Helper::ThumbnailCreator; + +# synopsis... + +=head1 DESCRIPTION + +# longer description.. + +=head1 AUTHOR + +Werner Hahn Ewh@futureworldsearch.netE + +=cut -- 2.20.1