X-Git-Url: https://git.sesse.net/?p=pr0n;a=blobdiff_plain;f=perl%2FSesse%2Fpr0n%2FCommon.pm;h=0a257a9855b8b4ae7b22d1331732aff1f594832b;hp=a6a8de4b52f82cd9db3138a8dc1fadea065d05e1;hb=85be4603493393c16a7ad028fbeaf98fa3878cd4;hpb=9b0484cc963ef4d7056d644b418ba03cce579bca diff --git a/perl/Sesse/pr0n/Common.pm b/perl/Sesse/pr0n/Common.pm index a6a8de4..0a257a9 100644 --- a/perl/Sesse/pr0n/Common.pm +++ b/perl/Sesse/pr0n/Common.pm @@ -32,7 +32,7 @@ BEGIN { require Sesse::pr0n::Config_local; }; - $VERSION = "v3.00"; + $VERSION = "v3.10"; @ISA = qw(Exporter); @EXPORT = qw(&error &dberror); %EXPORT_TAGS = qw(); @@ -128,7 +128,8 @@ sub get_query_string { my $first = 1; my $str = ""; - while (my ($key, $value) = each %$param) { + for my $key (sort keys %$param) { + my $value = $param->{$key}; next unless defined($value); next if (defined($defparam->{$key}) && $value == $defparam->{$key}); @@ -211,17 +212,20 @@ sub get_disk_location { } sub get_cache_location { - my ($r, $id, $width, $height, $infobox, $dpr) = @_; + my ($id, $width, $height) = @_; my $dir = POSIX::floor($id / 256); - if ($infobox) { - if ($dpr == 1) { - return $Sesse::pr0n::Config::image_base . "cache/$dir/$id-$width-$height-box.png"; - } else { - return $Sesse::pr0n::Config::image_base . "cache/$dir/$id-$width-$height-box\@$dpr.png"; - } + return $Sesse::pr0n::Config::image_base . "cache/$dir/$id-$width-$height-nobox.jpg"; +} + +sub get_infobox_cache_location { + my ($id, $width, $height, $dpr) = @_; + my $dir = POSIX::floor($id / 256); + + if ($dpr == 1) { + return $Sesse::pr0n::Config::image_base . "cache/$dir/$id-$width-$height-box.png"; } else { - return $Sesse::pr0n::Config::image_base . "cache/$dir/$id-$width-$height-nobox.jpg"; + return $Sesse::pr0n::Config::image_base . "cache/$dir/$id-$width-$height-box\@$dpr.png"; } } @@ -568,18 +572,16 @@ sub read_original_image { } sub ensure_cached { - my ($r, $filename, $id, $dbwidth, $dbheight, $infobox, $dpr, $xres, $yres, @otherres) = @_; + my ($r, $filename, $id, $dbwidth, $dbheight, $xres, $yres, @otherres) = @_; my ($new_dbwidth, $new_dbheight); my $fname = get_disk_location($r, $id); - if (!$infobox) { - unless (defined($xres) && (!defined($dbwidth) || !defined($dbheight) || $xres < $dbwidth || $yres < $dbheight || $xres == -1)) { - return ($fname, undef); - } + unless (defined($xres) && (!defined($dbwidth) || !defined($dbheight) || $xres < $dbwidth || $yres < $dbheight || $xres == -1)) { + return ($fname, undef); } - my $cachename = get_cache_location($r, $id, $xres, $yres, $infobox, $dpr); + my $cachename = get_cache_location($id, $xres, $yres); my $err; if (! -r $cachename or (-M $cachename > -M $fname)) { # If we are in overload mode (aka Slashdot mode), refuse to generate @@ -589,63 +591,12 @@ sub ensure_cached { error($r, 'System is in overload mode, not doing any scaling'); } - # If we're being asked for the box, make a new image with it. - # We don't care about @otherres since each of these images are - # already pretty cheap to generate, but we need the exact width so we can make - # one in the right size. - if ($infobox) { - my ($img, $width, $height); - - # This is slow, but should fortunately almost never happen, so don't bother - # special-casing it. - if (!defined($dbwidth) || !defined($dbheight)) { - $img = read_original_image($r, $filename, $id, $dbwidth, $dbheight, 0); - $new_dbwidth = $width = $img->Get('columns'); - $new_dbheight = $height = $img->Get('rows'); - @$img = (); - } else { - $img = Image::Magick->new; - $width = $dbwidth; - $height = $dbheight; - } - - if (defined($xres) && defined($yres)) { - ($width, $height) = scale_aspect($width, $height, $xres, $yres); - } - $height = 24 * $dpr; - $img->Set(size=>($width . "x" . $height)); - $img->Read('xc:white'); - - my $info = Image::ExifTool::ImageInfo($fname); - if (make_infobox($img, $info, $r, $dpr)) { - $img->Quantize(colors=>16, dither=>'False'); - - # Since the image is grayscale, ImageMagick overrides us and writes this - # as grayscale anyway, but at least we get rid of the alpha channel this - # way. - $img->Set(type=>'Palette'); - } else { - # Not enough room for the text, make a tiny dummy transparent infobox - @$img = (); - $img->Set(size=>"1x1"); - $img->Read('null:'); - - $width = 1; - $height = 1; - } - - $err = $img->write(filename => $cachename, quality => 90, depth => 8); - log_info($r, "New infobox cache: $width x $height for $id.jpg"); - - return ($cachename, 'image/png'); - } - my $img; ($img, $new_dbwidth, $new_dbheight) = make_mipmap($r, $filename, $id, $dbwidth, $dbheight, $xres, $yres, @otherres); while (defined($xres) && defined($yres)) { my ($nxres, $nyres) = (shift @otherres, shift @otherres); - my $cachename = get_cache_location($r, $id, $xres, $yres, $infobox, $dpr); + my $cachename = get_cache_location($id, $xres, $yres); my $cimg; if (defined($nxres) && defined($nyres)) { @@ -713,6 +664,69 @@ sub ensure_cached { return ($cachename, 'image/jpeg'); } +sub ensure_infobox_cached { + my ($r, $filename, $id, $dbwidth, $dbheight, $dpr, $xres, $yres) = @_; + + my ($new_dbwidth, $new_dbheight); + + my $fname = get_disk_location($r, $id); + my $cachename = get_infobox_cache_location($id, $xres, $yres, $dpr); + my $err; + if (! -r $cachename or (-M $cachename > -M $fname)) { + # If we are in overload mode (aka Slashdot mode), refuse to generate + # new thumbnails. + if (Sesse::pr0n::Overload::is_in_overload($r)) { + log_warn($r, "In overload mode, not scaling $id to $xres x $yres"); + error($r, 'System is in overload mode, not doing any scaling'); + } + + # We need the exact width so we can make one in the right size. + my ($width, $height); + + # This is slow, but should fortunately almost never happen, so don't bother + # special-casing it. + if (!defined($dbwidth) || !defined($dbheight)) { + my $img = read_original_image($r, $filename, $id, $dbwidth, $dbheight, 0); + $new_dbwidth = $width = $img->Get('columns'); + $new_dbheight = $height = $img->Get('rows'); + } else { + $width = $dbwidth; + $height = $dbheight; + } + my $img = Image::Magick->new; + + if (defined($xres) && defined($yres)) { + ($width, $height) = scale_aspect($width, $height, $xres, $yres); + } + $height = 24 * $dpr; + $img->Set(size=>($width . "x" . $height)); + $img->Read('xc:white'); + + my $info = Image::ExifTool::ImageInfo($fname); + if (make_infobox($img, $info, $r, $dpr)) { + $img->Quantize(colors=>16, dither=>'False'); + + # Since the image is grayscale, ImageMagick overrides us and writes this + # as grayscale anyway, but at least we get rid of the alpha channel this + # way. + $img->Set(type=>'Palette'); + } else { + # Not enough room for the text, make a tiny dummy transparent infobox + @$img = (); + $img->Set(size=>"1x1"); + $img->Read('null:'); + + $width = 1; + $height = 1; + } + + $err = $img->write(filename => $cachename, quality => 90, depth => 8); + log_info($r, "New infobox cache: $width x $height for $id.jpg"); + } + + return ($cachename, 'image/png'); +} + sub get_mimetype_from_filename { my $filename = shift; my MIME::Type $type = $mimetypes->mimeTypeOf($filename); @@ -733,7 +747,7 @@ sub make_infobox { $info->{'ExposureProgram'} =~ /shutter\b.*\bpriority/i); my $manual_aperture = (defined($info->{'ExposureProgram'}) && $info->{'ExposureProgram'} =~ /aperture\b.*\bpriority/i); - if ($info->{'ExposureProgram'} =~ /manual/i) { + if (defined($info->{'ExposureProgram'}) && $info->{'ExposureProgram'} =~ /manual/i) { $manual_shutter = 1; $manual_aperture = 1; } @@ -1006,7 +1020,7 @@ sub set_last_modified { my ($res, $mtime) = @_; my $str = POSIX::strftime("%a, %d %b %Y %H:%M:%S GMT", gmtime($mtime)); - $res->headers({ 'Last-Modified' => $str }); + $res->header('Last-Modified' => $str); } sub get_server_name {