From 27c7743995f4069d4c06a2f45d024227cb795187 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Lucas?= Date: Sun, 15 Dec 2013 14:23:45 +0100 Subject: [PATCH] Fix the thumbnail generation in case the same width is asked and a some unit test. re #127 --- book.php | 8 +++--- test/bookTest.php | 68 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 4 deletions(-) diff --git a/book.php b/book.php index 349fd05..1d96630 100644 --- a/book.php +++ b/book.php @@ -440,7 +440,7 @@ class Book extends Base { } } - public function getThumbnail ($width, $height) { + public function getThumbnail ($width, $height, $outputfile = NULL) { if (is_null ($width) && is_null ($height)) { return false; } @@ -452,11 +452,11 @@ class Book extends Base { //set new size if (!is_null ($width)) { $nw = $width; - if ($nw > $w) { return false; } + if ($nw >= $w) { return false; } $nh = ($nw*$h)/$w; } else { $nh = $height; - if ($nh > $h) { return false; } + if ($nh >= $h) { return false; } $nw = ($nh*$w)/$h; } } @@ -469,7 +469,7 @@ class Book extends Base { $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); + imagejpeg($dst_img,$outputfile,80); imagedestroy($src_img); imagedestroy($dst_img); diff --git a/test/bookTest.php b/test/bookTest.php index ddce41f..0e3c0ff 100644 --- a/test/bookTest.php +++ b/test/bookTest.php @@ -18,8 +18,35 @@ id:5 (1 book) Pierson's Magazine: H. G. Wells id:6 (8 books) Strand Magazine: Arthur Conan Doyle */ +define ("TEST_THUMBNAIL", dirname(__FILE__) . "/thumbnail.jpg"); +define ("COVER_WIDTH", 400); +define ("COVER_HEIGHT", 600); + class BookTest extends PHPUnit_Framework_TestCase { + public static function setUpBeforeClass() + { + $book = Book::getBookById(2); + if (!is_dir ($book->path)) { + mkdir ($book->path, 0777, true); + } + $im = imagecreatetruecolor(COVER_WIDTH, COVER_HEIGHT); + $text_color = imagecolorallocate($im, 255, 0, 0); + imagestring($im, 1, 5, 5, 'Book cover', $text_color); + imagejpeg ($im, $book->path . "/cover.jpg", 80); + } + + public static function tearDownAfterClass() + { + $book = Book::getBookById(2); + if (!file_exists ($book->path . "/cover.jpg")) { + return; + } + unlink ($book->path . "/cover.jpg"); + rmdir ($book->path); + rmdir (dirname ($book->path)); + } + public function testGetBookCount () { $this->assertEquals (14, Book::getBookCount ()); @@ -149,6 +176,10 @@ class BookTest extends PHPUnit_Framework_TestCase { // also check most of book's class methods $book = Book::getBookById(2); + + $linkArray = $book->getLinkArray (); + $this->assertCount (5, $linkArray); + $this->assertEquals ("The Return of Sherlock Holmes", $book->getTitle ()); $this->assertEquals ("urn:uuid:87ddbdeb-1e27-4d06-b79b-4b2a3bfc6a5f", $book->getEntryId ()); $this->assertEquals ("index.php?page=13&id=2", $book->getDetailUrl ()); @@ -163,6 +194,43 @@ class BookTest extends PHPUnit_Framework_TestCase $this->assertEquals ("Strand Magazine", $book->getPublisher()->name); } + public function testGetThumbnailNotNeeded () + { + $book = Book::getBookById(2); + + $this->assertFalse ($book->getThumbnail (NULL, NULL, NULL)); + + // Current cover is 400*600 + $this->assertFalse ($book->getThumbnail (COVER_WIDTH, NULL, NULL)); + $this->assertFalse ($book->getThumbnail (COVER_WIDTH + 1, NULL, NULL)); + $this->assertFalse ($book->getThumbnail (NULL, COVER_HEIGHT, NULL)); + $this->assertFalse ($book->getThumbnail (NULL, COVER_HEIGHT + 1, NULL)); + } + + /** + * @dataProvider providerThumbnail + */ + public function testGetThumbnailByWidth ($width, $height, $expectedWidth, $expectedHeight) + { + $book = Book::getBookById(2); + + $this->assertTrue ($book->getThumbnail ($width, $height, TEST_THUMBNAIL)); + + $size = GetImageSize(TEST_THUMBNAIL); + $this->assertEquals ($expectedWidth, $size [0]); + $this->assertEquals ($expectedHeight, $size [1]); + + unlink (TEST_THUMBNAIL); + } + + public function providerThumbnail () + { + return array ( + array (164, NULL, 164, 246), + array (NULL, 164, 109, 164) + ); + } + public function testGetMostInterestingDataToSendToKindle () { // Get Alice (available as MOBI, PDF, EPUB in that order)