OSDN Git Service

use jpegtran to reduce resized jpeg size.
authorgn64_jp <gn64@rec10.org>
Wed, 17 Apr 2013 02:07:34 +0000 (11:07 +0900)
committergn64_jp <gn64@rec10.org>
Wed, 17 Apr 2013 02:07:34 +0000 (11:07 +0900)
htmlserver.pl
jpegserver.pl
pdfsearch.pl

index 5621078..5179082 100755 (executable)
@@ -4,6 +4,7 @@ use Encode 'decode';
 use warnings;
 use CGI;
 use File::Basename;
+
 #
 #      Libre10
 #      Copyright 2013 yukikaze
@@ -19,10 +20,12 @@ $height = $cgi->param("height");
 $width  = $cgi->param("width");
 $bpage  = $pagenum - 1;
 $npage  = $pagenum + 1;
-$blink = "htmlserver.pl?pdfpath=$pdfpath&mode=$mode&width=$width&height=$height";
+$blink =
+  "htmlserver.pl?pdfpath=$pdfpath&mode=$mode&width=$width&height=$height";
 $nlink = $blink . "&pagenum=" . $npage;
 $blink = $blink . "&pagenum=" . $bpage;
-$jpeglink = "jpegserver.pl?pdfpath=\"$pdfpath\"&pagenum=$pagenum&mode=$mode&height=$height&width=$width";
+$jpeglink =
+"jpegserver.pl?pdfpath=\"$pdfpath\"&pagenum=$pagenum&mode=$mode&height=$height&width=$width";
 
 #print "Content-type:text/html\n\n";
 print $cgi->header( -charset => 'utf-8' );
@@ -32,16 +35,23 @@ print "<!DOCTYPE html>\n";
 print "<html lang='ja'>\n<head>\n  <meta charset='utf-8'>";
 print "<meta name='viewport' content='width=device-width, initial-scale=1'>\n";
 
-$linkfrm  = qq{<hr><div data-role="header" align='center'><a href='$blink' data-prefetch>前へ</a>};
-$linkfrm .= qq{page $pagenum     \n<a href='$nlink' data-prefetch>次へ</a></div><hr>\n};
+$linkfrm =
+qq{<hr><div data-role="header" align='center'><a href='$blink' data-prefetch>前へ</a>};
+$linkfrm .=
+qq{page $pagenum     \n<a href='$nlink' data-prefetch>次へ</a></div><hr>\n};
 
 if ( $mode eq "mobile" ) {
-       print "<link rel=\"stylesheet\" href=\"http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css\" />\n";
-       print "<script src=\"http://code.jquery.com/jquery-1.9.1.min.js\"></script>\n";
-       print "<script src=\"http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js\"></script>\n";
-       $linkfrm  = qq{<div data-role=\"header\" align='center'><a href='$blink'data-icon='arrow-l' data-prefetch>前へ</a>     };
+       print
+"<link rel=\"stylesheet\" href=\"http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css\" />\n";
+       print
+         "<script src=\"http://code.jquery.com/jquery-1.9.1.min.js\"></script>\n";
+       print
+"<script src=\"http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js\"></script>\n";
+       $linkfrm =
+qq{<div data-role=\"header\" align='center'><a href='$blink'data-icon='arrow-l' data-prefetch>前へ</a>     };
        $linkfrm .= qq{<h3>page $pagenum</h3>};
-       $linkfrm .= qq{     \n<a href='$nlink' data-icon='arrow-r' data-prefetch>次へ</a></div>\n};
+       $linkfrm .=
+qq{     \n<a href='$nlink' data-icon='arrow-r' data-prefetch>次へ</a></div>\n};
        $swipescript = "<script type='text/javascript'>
        \$('#page').live('pagecreate',function(){
        \$('#page').live('swipeleft',image_back);
@@ -59,10 +69,12 @@ print "<div id='page' data-role='page'>";
 #$linkfrm .="page " . $pagenum . "</h3>" ;
 #$linkfrm .="     \n<a href='" . $nlink . "' data-prefetch>次へ</a></div>\n";
 print $linkfrm;
-print "<div data-role=\"content\" id='image'><img src='$jpeglink' width='100%'/>";
+print
+  "<div data-role=\"content\" id='image'><img src='$jpeglink' width='100%'/>";
 print "</div>";
 print $linkfrm;
-print "<div align=center data-role='footer'>Libre10 git '13Q2<BR>Copyright yukikaze,long.inus</div>";
+print
+"<div align=center data-role='footer'>Libre10 git '13Q2<BR>Copyright yukikaze,long.inus</div>";
 print "</div>";
 print $cgi->end_html;
 
index b2730fe..f4e26cf 100755 (executable)
@@ -10,12 +10,22 @@ use DBI;
 use DBD::SQLite;
 use Data::Dumper;
 use Image::Magick;
+use YAML::XS;
+
 #use KCatch;
 #
 #      Libre10
 #      Copyright 2013 yukikaze,long.inus
 #
 $cgi = new CGI;
+my $confpath = "";
+if ( -e "libre10.conf" ) {
+       $confpath = "libre10.conf";
+}
+else {
+       $confpath = "/etc/libre10.conf";
+}
+$confdata = YAML::XS::LoadFile($confpath);
 $pdfpath = decode( 'utf-8', $cgi->param('pdfpath') );
 $pdfpath =~ s/\"//g;
 $pagenum    = int( $cgi->param('pagenum') );
@@ -24,21 +34,27 @@ $dpi        = int( $cgi->param('dpi') );
 $width      = int( $cgi->param('width') );
 $height     = int( $cgi->param('height') );
 $mode       = $cgi->param('mode');
-$outputpath = "/tmp/rec10pdf/tmp" . generate_unique_key();
-$dbpath = "/tmp/libre10.db";
-$dbi = DBI->connect("dbi:SQLite:dbname=$dbpath","","",{RaiseError=>1,AutoCommit => 1});
-$dbi_state=$dbi->prepare("SELECT path FROM pdffile WHERE id='$pdfpath'");
+$outputpath = "/tmp/libre10/tmp" . generate_unique_key();
+$dbpath     = $confdata->{dburl};
+$dbi =
+  DBI->connect( "dbi:SQLite:dbname=$dbpath", "", "",
+       { RaiseError => 1, AutoCommit => 1 } );
+$dbi_state = $dbi->prepare("SELECT path FROM pdffile WHERE id='$pdfpath'");
 $dbi_state->execute();
+
 #print $cgi->header;
 #print $cgi->start_html;
 #print Dumper $dbi;
-$pdfpath=$dbi_state->fetchrow_array;
+$pdfpath = $dbi_state->fetchrow_array;
 $dbi->disconnect;
+
 #print $pdfpath;
-if (-e "/tmp/rec10pdf"){
+if ( -e "/tmp/libre10" ) {
+
        #"";
-}else{
-       system "mkdir /tmp/rec10pdf";
+}
+else {
+       system "mkdir /tmp/libre10";
 }
 if ( ( -e $pdfpath ) && ( -r $pdfpath ) ) {
        $b = $pagenum - $pagediff;
@@ -47,21 +63,21 @@ if ( ( -e $pdfpath ) && ( -r $pdfpath ) ) {
                $b = 1;
        }
        system "pdfimages -f $pagenum -l $pagenum -j '$pdfpath' $outputpath";
-       #print "pdfimages -f $pagenum -l $pagenum -j '$pdfpath' $outputpath";
 }
 $outputpathjpg = "$outputpath-000.jpg";
 if ( $width > 0 && $height > 0 ) {
        $outputpath_rs_jpg = "$outputpath-000-$width-$height.jpg";
-       if ($mode eq 'im2'){
+       if ( $mode eq 'im2' ) {
                generate_resized_image( $outputpathjpg, $outputpath_rs_jpg, $width,
-                $height );
-       }else{
-               generate_resized_image_magick( $outputpathjpg, $outputpath_rs_jpg, $width,
-               $height );
+                       $height );
+       }
+       else {
+               generate_resized_image_magick( $outputpathjpg, $outputpath_rs_jpg,
+                       $width, $height );
        }
        $outputpathjpg = $outputpath_rs_jpg;
 }
-if (-e $outputpathjpg){
+if ( -e $outputpathjpg ) {
        $size = -s $outputpathjpg;
        print "Content-type: image/jpeg\n";
        print "Content-length:", $size, "\n";
@@ -69,6 +85,7 @@ if (-e $outputpathjpg){
        open( IN, $outputpathjpg );
        binmode(IN);
        binmode(STDOUT);
+
        #read(IN,$buf,$size);
        while ( read( IN, $buf, 1048576 ) ) {
                print $buf;
@@ -81,7 +98,8 @@ else {
        print $cgi->header( -type => "text/html", -charset => "utf-8" );
        print $cgi->start_html( -title => "rec10 pdf search", -lang => "ja-JP" );
        print "error<br>";
-       print "pdftk \"" . $pdfpath . "\" cat " . $b . "-" . $e . " output \"/tmp/rec10pdf/" . basename($pdfpath) . "\"";
+       print "pdftk \"$pdfpath\" cat $b - $e output \"/tmp/rec10pdf/ "
+         . basename($pdfpath) . "\"";
 }
 
 sub generate_unique_key {
@@ -103,29 +121,42 @@ sub generate_resized_image {
        }
        elsif ( $width / $height < $img->width / $img->height ) {
                $height = $width / $img->width * $img->height;
-       }else{
-               $width = $height / $img->height * $img ->width;
        }
-       $img = $img->create_scaled_image($width,$height);
+       else {
+               $width = $height / $img->height * $img->width;
+       }
+       $img = $img->create_scaled_image( $width, $height );
        $img->set_quality(40);
        $img->save($resized_img_path);
 }
+
 sub generate_resized_image_magick {
-        my ( $img_path, $resized_img_path, $width, $height ) = @_;
-        my $image = Image::Magick->new(size=>"$width".'x'."$height");
+       my ( $img_path, $resized_img_path, $width, $height ) = @_;
+       my $image = Image::Magick->new( size => "$width" . 'x' . "$height" );
        $image->Read($img_path);
-       my ($img_width,$img_height) = $image->Get('width','height');
-        if ( $img_width < $width && $img_height < $height ) {
-                $height = $img_height;
-                $width  = $img_width;
-        }
-        elsif ( $width / $height < $img_width / $img_height ) {
-                $height = $width / $img_width * $img_height;
-        }else{
-                $width = $height / $img_height * $img_width;
-        }
-       $image->Set(quality=>40);
-       #$image=$image->convert(preset => 'noalpha'); 
-        $image->Resize(width=>$width,height=>$height,blur=>0.8);
-        $image->Write($resized_img_path);
+       my ( $img_width, $img_height ) = $image->Get( 'width', 'height' );
+       if ( $img_width < $width && $img_height < $height ) {
+               $height = $img_height;
+               $width  = $img_width;
+       }
+       elsif ( $width / $height < $img_width / $img_height ) {
+               $height = $width / $img_width * $img_height;
+       }
+       else {
+               $width = $height / $img_height * $img_width;
+       }
+
+       #$image->Trim();
+       $image->Set( quality => 40 );
+
+       #$image->Level();
+       #$image=$image->convert(preset => 'noalpha');
+       $image->Resize( width => $width, height => $height, blur => 0.8 );
+       $image->Write($resized_img_path);
+       system
+"jpegtran -progressive -copy none -outfile $resized_img_path.out -optimize $resized_img_path";
+       if ( -e "$resized_img_path.out" ) {
+               system "rm $resized_img_path";
+               system "mv $resized_img_path.out $resized_img_path";
+       }
 }
index cf2df42..e9e0f9f 100755 (executable)
@@ -8,6 +8,7 @@ use WebService::Solr::Query;
 use warnings;
 use Data::Dumper;
 use YAML::XS;
+
 #use DBI;
 #
 #      Libre10
@@ -21,24 +22,28 @@ else {
        $confpath = "/etc/libre10.conf";
 }
 $confdata = YAML::XS::LoadFile($confpath);
+
 #my $sqlpath ="/tmp/libre10.db";
 #$sqldbi = DBI->connect("dbi:SQLite.dbname=" . $sqlpath);
-my $q = new CGI;
-my $mode = "";
-my $search_text=$q->param("search_text");
-$mode = $q->param("mode");
-$width = $q->param("width");
+my $q           = new CGI;
+my $mode        = "";
+my $search_text = $q->param("search_text");
+$mode   = $q->param("mode");
+$width  = $q->param("width");
 $height = $q->param("height");
 my $url  = $confdata->{solrurl};
 my $solr = WebService::Solr->new($url);
 print $q->header( -type => "text/html", -charset => "utf-8" );
-print $q->start_html( -title => "rec10 pdf search",
-                       -lang => "ja-JP",
-                       -script => {-type=>"text/javascript",-src=>"frame.js"},
-                       -style => {"src"=>"libre10.css"} );
+print $q->start_html(
+       -title  => "rec10 pdf search",
+       -lang   => "ja-JP",
+       -script => { -type => "text/javascript", -src => "frame.js" },
+       -style => { "src" => "libre10.css" }
+);
 print "<form action=\"pdfsearch.pl\" method=\"get\">";
 print "<div align=center><H1>Libre10 pdf search</H1></div>";
-print "<div align=center><p><input name='search_text' value='' style='width:200px'\/>";
+print
+"<div align=center><p><input name='search_text' value='' style='width:200px'\/>";
 print "<input type=\"submit\" value=\"search\"/></p></div><hr>";
 
 #print Dumper($confdata->{solrurl});
@@ -69,9 +74,9 @@ if ( length $search_text > 0 ) {
                }
        );
 
-       #my $response = $solr->search($query,{'rows' => '100','fl'=>'title,text,page,path'});
-       #my $response = $solr->search($query);
-       #print "texst";
+#my $response = $solr->search($query,{'rows' => '100','fl'=>'title,text,page,path'});
+#my $response = $solr->search($query);
+#print "texst";
        my $hits            = $response->content;
        my %hits            = %$hits;
        my $response_header = $hits{grouped}{title_group}{groups};
@@ -93,21 +98,27 @@ if ( length $search_text > 0 ) {
        }
 }
 else {
+
        #print "<a href=pdfsearch.pl?mode=$mode&width=1600>for Tablet</a><br>\n";
        print "<div align=center>";
-       print "<a href=pdfsearch.pl?mode=mobile&width=1024&height=1280>for narrow band</a><br>\n";
-       print "<a href=pdfsearch.pl?mode=mobile&width=1600&height=2560>for Tablet</a><br>\n";
+       print
+"<a href=pdfsearch.pl?mode=mobile&width=1024&height=1280>for narrow band</a><br>\n";
+       print
+"<a href=pdfsearch.pl?mode=mobile&width=1600&height=2560>for Tablet</a><br>\n";
 
-       #print "<a href=pdfsearch.pl?mode=$mode&width=540>SmartPhone(w540)</a><br>\n";
-       print "<a href=pdfsearch.pl?mode=mobile&width=1080&height=1920>for SmartPhone</a><br>\n";
-       print "<a href=pdfsearch.pl?mode=mobile&width=1600&height=2100>for Tablet retina</a><br>\n";
-       print "<a href=pdfsearch.pl?mode=mobile&width=1280&height=2400>for SmartPhone retina</a><br>\n";
+  #print "<a href=pdfsearch.pl?mode=$mode&width=540>SmartPhone(w540)</a><br>\n";
+       print
+"<a href=pdfsearch.pl?mode=mobile&width=1080&height=1920>for SmartPhone</a><br>\n";
+       print
+"<a href=pdfsearch.pl?mode=mobile&width=1600&height=2100>for Tablet retina</a><br>\n";
+       print
+"<a href=pdfsearch.pl?mode=mobile&width=1280&height=2400>for SmartPhone retina</a><br>\n";
        print "<a href=pdfsearch.pl?mode=''>for PC(default)</a><br>\n";
        print "</div>";
 }
-print "<input type='hidden' name='mode'   value=" . $mode   . " >\n";
+print "<input type='hidden' name='mode'   value=" . $mode . " >\n";
 print "<input type='hidden' name='height' value=" . $height . " >\n";
-print "<input type='hidden' name='width'  value=" . $width  . " >\n";
+print "<input type='hidden' name='width'  value=" . $width . " >\n";
 
 #print $url;
 #print Dumper($mode);
@@ -121,10 +132,12 @@ sub printGroupResults() {
        print "<p><h2>$title</h2>  -  $hitsnum 件の合致<br>";
        foreach $doc (@$hits) {
 
-               #print $doc->{text};
-               #$sqldbi->prepare("SELECT path FROM pdffile WHERE id=" . $doc->{$path_id});
-               ($pagenum,$path,$linkpath)=($doc->{page},$doc->{path_id},$doc->{path_id});
-               print "page " . $pagenum ."<br>";
+        #print $doc->{text};
+        #$sqldbi->prepare("SELECT path FROM pdffile WHERE id=" . $doc->{$path_id});
+               ( $pagenum, $path, $linkpath ) =
+                 ( $doc->{page}, $doc->{path_id}, $doc->{path_id} );
+               print "page " . $pagenum . "<br>";
+
                #print Dumper($hits_hl->{$doc->{id}}{text});
                print " " . $hits_hl->{ $doc->{id} }{text}[0] . "<br>";
                print "<a href=\"htmlserver.pl?pdfpath=$path";
@@ -134,4 +147,5 @@ sub printGroupResults() {
                print "</p>\n";
        }
 }
+
 #$sqldbi.disconnect;