+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');
+}
+