From b0660b08140ef8ea85838fd8e0df401cd9c9a44d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Lucas?= Date: Sat, 14 Dec 2013 21:49:55 +0100 Subject: [PATCH] Factorize the thumbnail generation and move it to the class Book. re #127 --- book.php | 36 +++++++++++++++++++ fetch.php | 104 ++++++++++++++++++++++++++++-------------------------- 2 files changed, 90 insertions(+), 50 deletions(-) diff --git a/book.php b/book.php index 2eb59dd..349fd05 100644 --- a/book.php +++ b/book.php @@ -439,6 +439,42 @@ class Book extends Base { echo "Exception : " . $e->getMessage(); } } + + public function getThumbnail ($width, $height) { + if (is_null ($width) && is_null ($height)) { + return false; + } + $file = $this->getFilePath ("jpg"); + // get image size + if ($size = GetImageSize($file)) { + $w = $size[0]; + $h = $size[1]; + //set new size + if (!is_null ($width)) { + $nw = $width; + if ($nw > $w) { return false; } + $nh = ($nw*$h)/$w; + } else { + $nh = $height; + if ($nh > $h) { return false; } + $nw = ($nh*$w)/$h; + } + } + else{ + //set new size + $nw = "160"; + $nh = "120"; + } + //draw the image + $src_img = imagecreatefromjpeg($file); + $dst_img = imagecreatetruecolor($nw,$nh); + imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $nw, $nh, $w, $h);//resizing the image + imagejpeg($dst_img,null,80); + imagedestroy($src_img); + imagedestroy($dst_img); + + return true; + } public function getLinkArray () { diff --git a/fetch.php b/fetch.php index a3b7b2a..afc509e 100644 --- a/fetch.php +++ b/fetch.php @@ -63,58 +63,62 @@ function notFound () { { case "jpg": header("Content-Type: image/jpeg"); - if (isset($_GET["width"])) - { - $file = $book->getFilePath ($type); - // get image size - if ($size = GetImageSize($file)) { - $w = $size[0]; - $h = $size[1]; - //set new size - $nw = $_GET["width"]; - if ($nw > $w) { break; } - $nh = ($nw*$h)/$w; - } - else{ - //set new size - $nw = "160"; - $nh = "120"; - } - //draw the image - $src_img = imagecreatefromjpeg($file); - $dst_img = imagecreatetruecolor($nw,$nh); - imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $nw, $nh, $w, $h);//resizing the image - imagejpeg($dst_img,null,80); - imagedestroy($src_img); - imagedestroy($dst_img); - return; - } - if (isset($_GET["height"])) - { - $file = $book->getFilePath ($type); - // get image size - if ($size = GetImageSize($file)) { - $w = $size[0]; - $h = $size[1]; - //set new size - $nh = $_GET["height"]; - if ($nh > $h) { break; } - $nw = ($nh*$w)/$h; - } - else{ - //set new size - $nw = "160"; - $nh = "120"; - } - //draw the image - $src_img = imagecreatefromjpeg($file); - $dst_img = imagecreatetruecolor($nw,$nh); - imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $nw, $nh, $w, $h);//resizing the image - imagejpeg($dst_img,null,80); - imagedestroy($src_img); - imagedestroy($dst_img); + if ($book->getThumbnail (getURLParam ("width"), getURLParam ("height"))) { + // The cover had to be resized return; } + // if (isset($_GET["width"])) + // { + // $file = $book->getFilePath ($type); + // // get image size + // if ($size = GetImageSize($file)) { + // $w = $size[0]; + // $h = $size[1]; + // //set new size + // $nw = $_GET["width"]; + // if ($nw > $w) { break; } + // $nh = ($nw*$h)/$w; + // } + // else{ + // //set new size + // $nw = "160"; + // $nh = "120"; + // } + // //draw the image + // $src_img = imagecreatefromjpeg($file); + // $dst_img = imagecreatetruecolor($nw,$nh); + // imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $nw, $nh, $w, $h);//resizing the image + // imagejpeg($dst_img,null,80); + // imagedestroy($src_img); + // imagedestroy($dst_img); + // return; + // } + // if (isset($_GET["height"])) + // { + // $file = $book->getFilePath ($type); + // // get image size + // if ($size = GetImageSize($file)) { + // $w = $size[0]; + // $h = $size[1]; + // //set new size + // $nh = $_GET["height"]; + // if ($nh > $h) { break; } + // $nw = ($nh*$w)/$h; + // } + // else{ + // //set new size + // $nw = "160"; + // $nh = "120"; + // } + // //draw the image + // $src_img = imagecreatefromjpeg($file); + // $dst_img = imagecreatetruecolor($nw,$nh); + // imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $nw, $nh, $w, $h);//resizing the image + // imagejpeg($dst_img,null,80); + // imagedestroy($src_img); + // imagedestroy($dst_img); + // return; + // } break; default: header("Content-Type: " . Data::$mimetypes[$type]);