Commit 9285ee23 authored by ale's avatar ale

use the image package to generate thumbnails instead of "convert"

parent 0910a960
......@@ -6,17 +6,22 @@ import (
"flag"
"fmt"
"html/template"
"image"
_ "image/gif"
"image/jpeg"
_ "image/png"
"io"
"log"
"net/http"
"net/url"
"os/exec"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/gorilla/mux"
"github.com/nfnt/resize"
)
var (
......@@ -261,7 +266,26 @@ func (s *uiServer) handleShowCover(book *Book, w http.ResponseWriter, req *http.
http.ServeContent(w, req, "", time.Time{}, f)
}
const thumbnailSize = "150x150"
var thumbnailSize uint = 150
func makeThumbnail(inputfile string) ([]byte, error) {
file, err := os.Open(inputfile)
if err != nil {
return nil, err
}
defer file.Close()
img, _, err := image.Decode(file)
if err != nil {
return nil, err
}
thumb := resize.Thumbnail(thumbnailSize, thumbnailSize, img, resize.Lanczos2)
var buf bytes.Buffer
if err := jpeg.Encode(&buf, thumb, &jpeg.Options{Quality: 65}); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (s *uiServer) handleShowThumbnail(book *Book, w http.ResponseWriter, req *http.Request) {
if book.CoverPath == "" {
......@@ -287,8 +311,7 @@ func (s *uiServer) handleShowThumbnail(book *Book, w http.ResponseWriter, req *h
}
defer cachedFile.Close()
data, err := exec.Command("convert", book.CoverPath, "-geometry", thumbnailSize,
"-quality", "60", "jpeg:-").Output()
data, err := makeThumbnail(s.storage.Abs(book.CoverPath))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment