From b0669c1a367aca4dbe895f552c3cffdba6864e7d Mon Sep 17 00:00:00 2001 From: Doug Bell Date: Thu, 31 Jul 2008 20:31:08 +0000 Subject: [PATCH] fixed Gallery Disk Space problem by adding a way to change image density --- docs/changelog/7.x.x.txt | 1 + docs/upgrades/upgrade_7.5.18-7.5.19.pl | 14 +++++++ lib/WebGUI/Asset/File/GalleryFile/Photo.pm | 17 +++++--- lib/WebGUI/Asset/Wobject/Gallery.pm | 13 +++++++ lib/WebGUI/Storage/Image.pm | 45 ++++++++++++++++------ lib/WebGUI/i18n/English/Asset_Gallery.pm | 28 +++++++++++++- 6 files changed, 100 insertions(+), 18 deletions(-) diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index 474bddca3..ee6b0f28a 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -12,6 +12,7 @@ - fixed: per item shipping - fixed: product title problem - fixed: adding payment brings me to / + - fixed: Gallery uses too much disk space. Added a way to change an images pixel density. 7.5.18 - fixed: Collateral Image Manager broken in Firefox 3 diff --git a/docs/upgrades/upgrade_7.5.18-7.5.19.pl b/docs/upgrades/upgrade_7.5.18-7.5.19.pl index d335605e9..417f638ee 100644 --- a/docs/upgrades/upgrade_7.5.18-7.5.19.pl +++ b/docs/upgrades/upgrade_7.5.18-7.5.19.pl @@ -32,6 +32,7 @@ my $session = start(); # this line required addNewInboxIndexes( $session ); updateAddressTable( $session ); addProductShipping( $session ); +addGalleryImageDensity( $session ); correctPayDriverReceiptTemplate( $session ); finish($session); # this line required @@ -45,6 +46,19 @@ finish($session); # this line required # print "DONE!\n" unless $quiet; #} +#---------------------------------------------------------------------------- +# Add the image density property to the Gallery +sub addGalleryImageDensity { + my $session = shift; + print "\tAdding Image Density to Gallery... " unless $quiet; + + $session->db->write( + "ALTER TABLE Gallery ADD COLUMN imageDensity INT" + ); + + print "DONE!\n" unless $quiet; +} + #---------------------------------------------------------------------------- # Corrects the asset id of the default receipt email template for the PayDriver sub correctPayDriverReceiptTemplate{ diff --git a/lib/WebGUI/Asset/File/GalleryFile/Photo.pm b/lib/WebGUI/Asset/File/GalleryFile/Photo.pm index 5b34c7b4d..bec196849 100644 --- a/lib/WebGUI/Asset/File/GalleryFile/Photo.pm +++ b/lib/WebGUI/Asset/File/GalleryFile/Photo.pm @@ -109,21 +109,28 @@ C is a hash reference of options and is currently not used. sub applyConstraints { my $self = shift; + my $options = shift; my $gallery = $self->getGallery; # Update the asset's size and make a thumbnail - my $maxImageSize = $self->getGallery->get("imageViewSize") + my $maxImageSize = $gallery->get("imageViewSize") || $self->session->setting->get("maxImageSize"); - my $thumbnailSize = $self->getGallery->get("imageThumbnailSize") - || $self->session->setting->get("thumbnailSize"); my $parameters = $self->get("parameters"); my $storage = $self->getStorageLocation; my $file = $self->get("filename"); + + # Make resolutions before fixing image, so that we can get higher quality + # resolutions + $self->makeResolutions; + + # adjust density before size, so that the dimensions won't change + $storage->resize( $file, undef, undef, $gallery->get( 'imageDensity' ) ); $storage->adjustMaxImageSize($file, $maxImageSize); + $self->generateThumbnail; $self->setSize; - $self->makeResolutions; $self->updateExifDataFromFile; + $self->SUPER::applyConstraints( $options ); } #------------------------------------------------------------------- @@ -361,7 +368,7 @@ sub makeResolutions { } my $newFilename = $res . ".jpg"; $storage->copyFile( $self->get("filename"), $newFilename ); - $storage->resize( $newFilename, $res ); + $storage->resize( $newFilename, $res, undef, $self->getGallery->get( 'imageDensity' ) ); } } diff --git a/lib/WebGUI/Asset/Wobject/Gallery.pm b/lib/WebGUI/Asset/Wobject/Gallery.pm index c7430536d..271599c0c 100644 --- a/lib/WebGUI/Asset/Wobject/Gallery.pm +++ b/lib/WebGUI/Asset/Wobject/Gallery.pm @@ -65,6 +65,11 @@ sub definition { DESC => $i18n->get("viewListOrderDirection option desc"), ); + tie my %imageDensityOptions, 'Tie::IxHash', ( + 72 => $i18n->get( "imageDensity option web" ), + 300 => $i18n->get( "imageDensity option print" ), + ); + tie my %properties, 'Tie::IxHash', ( groupIdAddComment => { tab => "security", @@ -102,6 +107,14 @@ sub definition { label => $i18n->get("imageThumbnailSize label"), hoverHelp => $i18n->get("imageThumbnailSize description"), }, + imageDensity => { + tab => "properties", + fieldType => "selectBox", + options => \%imageDensityOptions, + defaultValue => 72, + label => $i18n->get( "imageDensity label" ), + hoverHelp => $i18n->get( "imageDensity description" ), + }, maxSpacePerUser => { tab => "properties", fieldType => "integer", diff --git a/lib/WebGUI/Storage/Image.pm b/lib/WebGUI/Storage/Image.pm index 1e0fbf314..da357d183 100644 --- a/lib/WebGUI/Storage/Image.pm +++ b/lib/WebGUI/Storage/Image.pm @@ -118,6 +118,8 @@ sub addFileFromCaptcha { return ($filename, $challenge); } +#------------------------------------------------------------------- + =head2 adjustMaxImageSize ( $file ) Adjust the size of an image according to the C setting in the Admin @@ -356,13 +358,20 @@ The new width of the image in pixels. The new height of the image in pixels. +=head3 density + +The new image density in pixels per inch. + =cut +# TODO: Make this take a hash reference with width, height, and density keys. + sub resize { - my $self = shift; - my $filename = shift; - my $width = shift; - my $height = shift; + my $self = shift; + my $filename = shift; + my $width = shift; + my $height = shift; + my $density = shift; unless (defined $filename) { $self->session->errorHandler->error("Can't resize when you haven't specified a file."); return 0; @@ -371,7 +380,7 @@ sub resize { $self->session->errorHandler->error("Can't resize something that's not an image."); return 0; } - unless ($width || $height) { + unless ($width || $height || $density) { $self->session->errorHandler->error("Can't resize with no resizing parameters."); return 0; } @@ -381,19 +390,33 @@ sub resize { $self->session->errorHandler->error("Couldn't read image for resizing: ".$error); return 0; } - my ($x, $y) = $image->Get('width','height'); - if (!$height) { # proportional scale by width - $height = $width / $x * $y; + + # First, change image density + if ( $density ) { + $self->session->errorHandler->info( "Setting $filename to $density" ); + $image->Set( density => "${density}x${density}" ); } - elsif (!$width) { # proportional scale by height - $width = $height * $x / $y; + + # Next, resize dimensions + if ( $width || $height ) { + $self->session->errorHandler->info( "Resizing $filename to w:$width h:$height" ); + my ($x, $y) = $image->Get('width','height'); + if (!$height) { # proportional scale by width + $height = $width / $x * $y; + } + elsif (!$width) { # proportional scale by height + $width = $height * $x / $y; + } + $image->Resize( height => $height, width => $width ); } - $image->Resize( height => $height, width => $width, filter => "lanczos" ); + + # Write our changes to disk $error = $image->Write($self->getPath($filename)); if ($error) { $self->session->errorHandler->error("Couldn't resize image: ".$error); return 0; } + return 1; } diff --git a/lib/WebGUI/i18n/English/Asset_Gallery.pm b/lib/WebGUI/i18n/English/Asset_Gallery.pm index 7c86a0385..f90d59844 100644 --- a/lib/WebGUI/i18n/English/Asset_Gallery.pm +++ b/lib/WebGUI/i18n/English/Asset_Gallery.pm @@ -41,7 +41,7 @@ our $I18N = { context => '', }, "imageResolutions description" => { - message => "The sizes of images (in pixels) available for download. Images will be altered to 72 DPI before proportionally constrained to the specified number of pixels. In other words, this number is the maximum height or width that the image will have.", + message => "The sizes of images (in pixels) available for download. This number is the maximum height or width that the image will have.", lastUpdated => 0, context => '', }, @@ -73,7 +73,7 @@ our $I18N = { context => '', }, "maxSpacePerUser description" => { - message => "The maximum amount of disk space (in megabytes) a user is allowed to use in this Gallery.", + message => "The maximum amount of disk space (in megabytes) a user is allowed to use in this Gallery. Each image will take up to 6 megabytes depending on what resolutions and density you choose.", lastUpdated => 0, context => '', }, @@ -711,6 +711,30 @@ our $I18N = { lastUpdated => 0, context => q{Description of asset property}, }, + + 'imageDensity label' => { + message => q{Image Density}, + lastUpdated => 0, + context => q{Label for asset property}, + }, + + 'imageDensity description' => { + message => q{The density of the image. Print-quality images are more than three times the size of web-quality images.}, + lastUpdated => 0, + context => q{Description of asset property}, + }, + + 'imageDensity option web' => { + message => q{Web quality (72 pixels per inch)}, + lastUpdated => 0, + context => q{Option label for 72 pixels-per-inch images, the highest that monitors can display}, + }, + + 'imageDensity option print' => { + message => q{Print quality (300 pixels per inch)}, + lastUpdated => 0, + context => q{Option label for 300 pixels-per-inch images, good for printing images}, + }, }; 1;