From: Steinar H. Gunderson Date: Mon, 26 May 2008 18:59:39 +0000 (+0200) Subject: Support a new form of image: box/, which is exactly the opposite of nobox/ X-Git-Url: https://git.sesse.net/?p=pr0n;a=commitdiff_plain;h=f7c70497096c80290e4a8d5d69fe8417d1aa931f;hp=ca8871b0a277f2175611515719251caaa39d64b5 Support a new form of image: box/, which is exactly the opposite of nobox/ (ie. the infobox only, no image), as a PNG file. Will be used by the fullscreen part soonish. --- diff --git a/perl/Sesse/pr0n/Common.pm b/perl/Sesse/pr0n/Common.pm index eb111ce..e348d32 100644 --- a/perl/Sesse/pr0n/Common.pm +++ b/perl/Sesse/pr0n/Common.pm @@ -218,10 +218,12 @@ sub get_cache_location { my ($r, $id, $width, $height, $infobox) = @_; my $dir = POSIX::floor($id / 256); - if ($infobox) { + if ($infobox eq 'both') { return get_base($r) . "cache/$dir/$id-$width-$height.jpg"; - } else { + } elsif ($infobox eq 'nobox') { return get_base($r) . "cache/$dir/$id-$width-$height-nobox.jpg"; + } else { + return get_base($r) . "cache/$dir/$id-$width-$height-box.png"; } } @@ -528,8 +530,10 @@ sub ensure_cached { my ($r, $filename, $id, $dbwidth, $dbheight, $infobox, $xres, $yres, @otherres) = @_; my $fname = get_disk_location($r, $id); - unless (defined($xres) && (!defined($dbwidth) || !defined($dbheight) || $xres < $dbheight || $yres < $dbwidth || $xres == -1)) { - return ($fname, 0); + if ($infobox ne 'box') { + unless (defined($xres) && (!defined($dbwidth) || !defined($dbheight) || $xres < $dbheight || $yres < $dbwidth || $xres == -1)) { + return ($fname, undef); + } } my $cachename = get_cache_location($r, $id, $xres, $yres, $infobox); @@ -541,6 +545,42 @@ sub ensure_cached { $r->log->warn("In overload mode, not scaling $id to $xres x $yres"); error($r, 'System is in overload mode, not doing any scaling'); } + + # If we're being asked for just the box, make a new image with just the box. + # 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. + $r->log->warn("BOX: $infobox"); + if ($infobox eq 'box') { + 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, $id, $dbwidth, $dbheight); + $width = $img->Get('columns'); + $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); + } + $img->Set(size=>($width . "x24")); + $img->Read('xc:white'); + + my $info = Image::ExifTool::ImageInfo($fname); + make_infobox($img, $info, $r); + + $err = $img->write(filename => $cachename); + $r->log->info("New infobox cache: $width x 24 for $id.jpg"); + + return ($cachename, 'image/png'); + } my $img = make_mipmap($r, $fname, $id, $dbwidth, $dbheight, $xres, $yres, @otherres); @@ -576,7 +616,7 @@ sub ensure_cached { $cimg->Resize(width=>$nwidth, height=>$nheight, filter=>$filter); } - if (($nwidth >= 800 || $nheight >= 600 || $xres == -1) && $infobox == 1) { + if (($nwidth >= 800 || $nheight >= 600 || $xres == -1) && $infobox ne 'nobox') { my $info = Image::ExifTool::ImageInfo($fname); make_infobox($cimg, $info, $r); } @@ -616,7 +656,7 @@ sub ensure_cached { } } } - return ($cachename, 1); + return ($cachename, 'image/jpeg'); } sub get_mimetype_from_filename { diff --git a/perl/Sesse/pr0n/Image.pm b/perl/Sesse/pr0n/Image.pm index e298dc3..da45cfb 100644 --- a/perl/Sesse/pr0n/Image.pm +++ b/perl/Sesse/pr0n/Image.pm @@ -15,22 +15,26 @@ sub handler { # Find the event and file name my ($event,$filename,$xres,$yres); - my $infobox = 1; - if ($r->uri =~ m#^/([a-zA-Z0-9-]+)/original/(nobox/)?([a-zA-Z0-9._()-]+)$#) { + my $infobox = 'both'; + if ($r->uri =~ m#^/([a-zA-Z0-9-]+)/original/((?:no)?box/)?([a-zA-Z0-9._()-]+)$#) { $event = $1; $filename = $3; - } elsif ($r->uri =~ m#^/([a-zA-Z0-9-]+)/(\d+)x(\d+)/(nobox/)?([a-zA-Z0-9._()-]+)$#) { + $infobox = 'nobox' if (defined($2) && $2 eq 'nobox/'); + $infobox = 'box' if (defined($2) && $2 eq 'box/'); + } elsif ($r->uri =~ m#^/([a-zA-Z0-9-]+)/(\d+)x(\d+)/((?:no)?box/)?([a-zA-Z0-9._()-]+)$#) { $event = $1; $filename = $5; $xres = $2; $yres = $3; - $infobox = 0 if (defined($4)); - } elsif ($r->uri =~ m#^/([a-zA-Z0-9-]+)/(nobox/)?([a-zA-Z0-9._()-]+)$#) { + $infobox = 'nobox' if (defined($4) && $4 eq 'nobox/'); + $infobox = 'box' if (defined($4) && $4 eq 'box/'); + } elsif ($r->uri =~ m#^/([a-zA-Z0-9-]+)/((?:no)?box/)?([a-zA-Z0-9._()-]+)$#) { $event = $1; $filename = $3; $xres = -1; $yres = -1; - $infobox = 0 if (defined($2)); + $infobox = 'nobox' if (defined($2) && $2 eq 'nobox/'); + $infobox = 'box' if (defined($2) && $2 eq 'box/'); } my ($id, $dbwidth, $dbheight); @@ -48,13 +52,10 @@ sub handler { $dbheight = $ref->{'height'}; # Scale if we need to do so - my ($fname,$thumbnail) = Sesse::pr0n::Common::ensure_cached($r, $filename, $id, $dbwidth, $dbheight, $infobox, $xres, $yres); + my ($fname, $mime_type) = Sesse::pr0n::Common::ensure_cached($r, $filename, $id, $dbwidth, $dbheight, $infobox, $xres, $yres); # Output the image to the user - my $mime_type; - if ($thumbnail) { - $mime_type = "image/jpeg"; - } else { + if (!defined($mime_type)) { $mime_type = Sesse::pr0n::Common::get_mimetype_from_filename($filename); } $r->content_type($mime_type); diff --git a/perl/Sesse/pr0n/pr0n.pm b/perl/Sesse/pr0n/pr0n.pm index 16b6964..eeb93b0 100644 --- a/perl/Sesse/pr0n/pr0n.pm +++ b/perl/Sesse/pr0n/pr0n.pm @@ -72,7 +72,7 @@ sub handler { $uri =~ /^\/\+all\/?$/ || $uri =~ /^\/\+tags\/[a-zA-Z0-9-]+\/?$/) { return Sesse::pr0n::Index::handler($r); - } elsif ($uri =~ m#^/[a-zA-Z0-9-]+/(\d+x\d+/|original/)?(nobox/)?[a-zA-Z0-9._()-]+$#) { + } elsif ($uri =~ m#^/[a-zA-Z0-9-]+/(\d+x\d+/|original/)?((?:no)?box/)?[a-zA-Z0-9._()-]+$#) { return Sesse::pr0n::Image::handler($r); }