X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=perl%2FSesse%2Fpr0n%2FCommon.pm;h=f071afcf838bf8a4425a5b46632bb8c1ac00cc06;hb=c305a42e2491a0f998938e02f61c4456b8cb5d5d;hp=171f47d538ebead85ed6dc0e43f8b1e49f9a1fcc;hpb=5526f8d9bbc00db6accf155fd74550a61d4f2033;p=pr0n diff --git a/perl/Sesse/pr0n/Common.pm b/perl/Sesse/pr0n/Common.pm index 171f47d..f071afc 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.01"; + $VERSION = "v3.20"; @ISA = qw(Exporter); @EXPORT = qw(&error &dberror); %EXPORT_TAGS = qw(); @@ -207,22 +207,25 @@ sub get_dbh { sub get_disk_location { my ($r, $id) = @_; - my $dir = POSIX::floor($id / 256); + my $dir = POSIX::floor($id / 256); return $Sesse::pr0n::Config::image_base . "images/$dir/$id.jpg"; } sub get_cache_location { - my ($r, $id, $width, $height, $infobox, $dpr) = @_; - my $dir = POSIX::floor($id / 256); + 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"; } } @@ -245,7 +248,7 @@ sub ensure_disk_location_exists { sub get_mipmap_location { my ($r, $id, $width, $height) = @_; - my $dir = POSIX::floor($id / 256); + my $dir = POSIX::floor($id / 256); return $Sesse::pr0n::Config::image_base . "cache/$dir/$id-mipmap-$width-$height.jpg"; } @@ -569,19 +572,14 @@ sub read_original_image { } sub ensure_cached { - my ($r, $filename, $id, $dbwidth, $dbheight, $infobox, $dpr, $xres, $yres, @otherres) = @_; - - my ($new_dbwidth, $new_dbheight); + my ($r, $filename, $id, $dbwidth, $dbheight, $xres, $yres, @otherres) = @_; 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 $err; + my $cachename = get_cache_location($id, $xres, $yres); if (! -r $cachename or (-M $cachename > -M $fname)) { # If we are in overload mode (aka Slashdot mode), refuse to generate # new thumbnails. @@ -590,128 +588,146 @@ 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:'); + make_cache($r, $filename, $id, $dbwidth, $dbheight, $xres, $yres, @otherres); + } - $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'); - } + return ($cachename, 'image/jpeg'); +} - my $img; - ($img, $new_dbwidth, $new_dbheight) = make_mipmap($r, $filename, $id, $dbwidth, $dbheight, $xres, $yres, @otherres); +sub make_cache { + my ($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 $cimg; - if (defined($nxres) && defined($nyres)) { - # we have more resolutions to scale, so don't throw - # the image away - $cimg = $img->Clone(); - } else { - $cimg = $img; - } + my ($img, $new_dbwidth, $new_dbheight) = make_mipmap($r, $filename, $id, $dbwidth, $dbheight, $xres, $yres, @otherres); + + # Update the SQL database if it doesn't contain the required info + if (!defined($dbwidth) && defined($new_dbwidth)) { + log_info($r, "Updating width/height for $id: $new_dbwidth x $new_dbheight"); + update_image_info($r, $id, $new_dbwidth, $new_dbheight); + } + + my $err; + while (defined($xres) && defined($yres)) { + my ($nxres, $nyres) = (shift @otherres, shift @otherres); + my $cachename = get_cache_location($id, $xres, $yres); - my $width = $img->Get('columns'); - my $height = $img->Get('rows'); - my ($nwidth, $nheight) = scale_aspect($width, $height, $xres, $yres); + my $cimg; + if (defined($nxres) && defined($nyres)) { + # we have more resolutions to scale, so don't throw + # the image away + $cimg = $img->Clone(); + } else { + $cimg = $img; + } + + my $width = $img->Get('columns'); + my $height = $img->Get('rows'); + my ($nwidth, $nheight) = scale_aspect($width, $height, $xres, $yres); - my $filter = 'Lanczos'; - my $quality = 87; - my $sf = "1x1"; + my $filter = 'Lanczos'; + my $quality = 87; + my $sf = "1x1"; - if ($xres != -1) { - $cimg->Resize(width=>$nwidth, height=>$nheight, filter=>$filter, 'sampling-factor'=>$sf); - } + if ($xres != -1) { + $cimg->Resize(width=>$nwidth, height=>$nheight, filter=>$filter); + } - # Strip EXIF tags etc. - $cimg->Strip(); + # Strip EXIF tags etc. + $cimg->Strip(); - { - my %parms = ( - filename => $cachename, - quality => $quality - ); - if (($nwidth >= 640 && $nheight >= 480) || - ($nwidth >= 480 && $nheight >= 640)) { - $parms{'interlace'} = 'Plane'; - } - if (defined($sf)) { - $parms{'sampling-factor'} = $sf; - } - $err = $cimg->write(%parms); + { + my %parms = ( + filename => $cachename, + quality => $quality + ); + if (($nwidth >= 640 && $nheight >= 480) || + ($nwidth >= 480 && $nheight >= 640)) { + $parms{'interlace'} = 'Plane'; } + if (defined($sf)) { + $parms{'sampling-factor'} = $sf; + } + $err = $cimg->write(%parms); + } - undef $cimg; + undef $cimg; - ($xres, $yres) = ($nxres, $nyres); + ($xres, $yres) = ($nxres, $nyres); - log_info($r, "New cache: $nwidth x $nheight for $id.jpg"); - } - - undef $img; - if ($err) { - log_warn($r, "$fname: $err"); - $err =~ /(\d+)/; - if ($1 >= 400) { - #@$magick = (); - error($r, "$fname: $err"); - } - } + log_info($r, "New cache: $nwidth x $nheight for $id.jpg"); } - # Update the SQL database if it doesn't contain the required info - if (!defined($dbwidth) && defined($new_dbwidth)) { - log_info($r, "Updating width/height for $id: $new_dbwidth x $new_dbheight"); - update_image_info($r, $id, $new_dbwidth, $new_dbheight); + undef $img; + if ($err) { + log_warn($r, "$filename: $err"); + $err =~ /(\d+)/; + if ($1 >= 400) { + #@$magick = (); + error($r, "$filename: $err"); + } } +} - 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 { @@ -734,7 +750,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; } @@ -804,13 +820,13 @@ sub make_infobox { $model =~ s/^\s+//; $model =~ s/\s+$//; - push @parts, [ ' - ', 0 ] if (scalar @parts > 0); + push @parts, [ "\x{00A0}\x{2013}\x{00A0}", 0 ] if (scalar @parts > 0); push @parts, [ $model, 0 ]; } # classic fields if (scalar @classic_fields > 0) { - push @parts, [ ' - ', 0 ] if (scalar @parts > 0); + push @parts, [ "\x{00A0}\x{2013}\x{00A0}", 0 ] if (scalar @parts > 0); my $first_elem = 1; for my $field (@classic_fields) { @@ -825,14 +841,14 @@ sub make_infobox { $info->{'Flash'} =~ /no flash/i || $info->{'Flash'} =~ /not fired/i || $info->{'Flash'} =~ /Off/) { - push @parts, [ ' - ', 0 ] if (scalar @parts > 0); + push @parts, [ "\x{00A0}\x{2013}\x{00A0}", 0 ] if (scalar @parts > 0); push @parts, [ "No flash", 0 ]; } elsif ($info->{'Flash'} =~ /fired/i || $info->{'Flash'} =~ /On/) { - push @parts, [ ' - ', 0 ] if (scalar @parts > 0); + push @parts, [ "\x{00A0}\x{2013}\x{00A0}", 0 ] if (scalar @parts > 0); push @parts, [ "Flash", 0 ]; } else { - push @parts, [ ' - ', 0 ] if (scalar @parts > 0); + push @parts, [ "\x{00A0}\x{2013}\x{00A0}", 0 ] if (scalar @parts > 0); push @parts, [ $info->{'Flash'}, 0 ]; } }