Fix the thumbnail generation in case the same width is asked and a some unit test. re #127

This commit is contained in:
Sébastien Lucas 2013-12-15 14:23:45 +01:00
parent b0660b0814
commit 27c7743995
2 changed files with 72 additions and 4 deletions

View file

@ -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)) { if (is_null ($width) && is_null ($height)) {
return false; return false;
} }
@ -452,11 +452,11 @@ class Book extends Base {
//set new size //set new size
if (!is_null ($width)) { if (!is_null ($width)) {
$nw = $width; $nw = $width;
if ($nw > $w) { return false; } if ($nw >= $w) { return false; }
$nh = ($nw*$h)/$w; $nh = ($nw*$h)/$w;
} else { } else {
$nh = $height; $nh = $height;
if ($nh > $h) { return false; } if ($nh >= $h) { return false; }
$nw = ($nh*$w)/$h; $nw = ($nh*$w)/$h;
} }
} }
@ -469,7 +469,7 @@ class Book extends Base {
$src_img = imagecreatefromjpeg($file); $src_img = imagecreatefromjpeg($file);
$dst_img = imagecreatetruecolor($nw,$nh); $dst_img = imagecreatetruecolor($nw,$nh);
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $nw, $nh, $w, $h);//resizing the image 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($src_img);
imagedestroy($dst_img); imagedestroy($dst_img);

View file

@ -18,8 +18,35 @@ id:5 (1 book) Pierson's Magazine: H. G. Wells
id:6 (8 books) Strand Magazine: Arthur Conan Doyle 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 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 () public function testGetBookCount ()
{ {
$this->assertEquals (14, Book::getBookCount ()); $this->assertEquals (14, Book::getBookCount ());
@ -149,6 +176,10 @@ class BookTest extends PHPUnit_Framework_TestCase
{ {
// also check most of book's class methods // also check most of book's class methods
$book = Book::getBookById(2); $book = Book::getBookById(2);
$linkArray = $book->getLinkArray ();
$this->assertCount (5, $linkArray);
$this->assertEquals ("The Return of Sherlock Holmes", $book->getTitle ()); $this->assertEquals ("The Return of Sherlock Holmes", $book->getTitle ());
$this->assertEquals ("urn:uuid:87ddbdeb-1e27-4d06-b79b-4b2a3bfc6a5f", $book->getEntryId ()); $this->assertEquals ("urn:uuid:87ddbdeb-1e27-4d06-b79b-4b2a3bfc6a5f", $book->getEntryId ());
$this->assertEquals ("index.php?page=13&id=2", $book->getDetailUrl ()); $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); $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 () public function testGetMostInterestingDataToSendToKindle ()
{ {
// Get Alice (available as MOBI, PDF, EPUB in that order) // Get Alice (available as MOBI, PDF, EPUB in that order)