mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-06-06 02:24:33 +08:00
Made display thumbnail generation use original data if smaller
Thumbnail generation would sometimes create a file larger than the original, if the original was already well optimized, therefore making the thumbnail counter-productive. This change compares the sizes of the original and the generated thumbnail, and uses the smaller of the two if the thumbnail does not change the aspect ratio of the image. Fixes #1751
This commit is contained in:
@ -36,6 +36,30 @@ class ImageTest extends TestCase
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_image_display_thumbnail_generation_does_not_increase_image_size()
|
||||
{
|
||||
$page = Page::first();
|
||||
$admin = $this->getAdmin();
|
||||
$this->actingAs($admin);
|
||||
|
||||
$originalFile = $this->getTestImageFilePath('compressed.png');
|
||||
$originalFileSize = filesize($originalFile);
|
||||
$imgDetails = $this->uploadGalleryImage($page, 'compressed.png');
|
||||
$relPath = $imgDetails['path'];
|
||||
|
||||
$this->assertTrue(file_exists(public_path($relPath)), 'Uploaded image found at path: '. public_path($relPath));
|
||||
$displayImage = $imgDetails['response']->thumbs->display;
|
||||
|
||||
$displayImageRelPath = implode('/', array_slice(explode('/', $displayImage), 3));
|
||||
$displayImagePath = public_path($displayImageRelPath);
|
||||
$displayFileSize = filesize($displayImagePath);
|
||||
|
||||
$this->deleteImage($relPath);
|
||||
$this->deleteImage($displayImageRelPath);
|
||||
|
||||
$this->assertEquals($originalFileSize, $displayFileSize, 'Display thumbnail generation should not increase image size');
|
||||
}
|
||||
|
||||
public function test_image_edit()
|
||||
{
|
||||
$editor = $this->getEditor();
|
||||
|
@ -10,9 +10,13 @@ trait UsesImages
|
||||
* Get the path to our basic test image.
|
||||
* @return string
|
||||
*/
|
||||
protected function getTestImageFilePath()
|
||||
protected function getTestImageFilePath(?string $fileName = null)
|
||||
{
|
||||
return base_path('tests/test-data/test-image.png');
|
||||
if (is_null($fileName)) {
|
||||
$fileName = 'test-image.png';
|
||||
}
|
||||
|
||||
return base_path('tests/test-data/' . $fileName);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -20,9 +24,9 @@ trait UsesImages
|
||||
* @param $fileName
|
||||
* @return UploadedFile
|
||||
*/
|
||||
protected function getTestImage($fileName)
|
||||
protected function getTestImage($fileName, ?string $testDataFileName = null)
|
||||
{
|
||||
return new UploadedFile($this->getTestImageFilePath(), $fileName, 'image/png', 5238, null, true);
|
||||
return new UploadedFile($this->getTestImageFilePath($testDataFileName), $fileName, 'image/png', 5238, null, true);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -52,9 +56,9 @@ trait UsesImages
|
||||
* @param string $contentType
|
||||
* @return \Illuminate\Foundation\Testing\TestResponse
|
||||
*/
|
||||
protected function uploadImage($name, $uploadedTo = 0, $contentType = 'image/png')
|
||||
protected function uploadImage($name, $uploadedTo = 0, $contentType = 'image/png', ?string $testDataFileName = null)
|
||||
{
|
||||
$file = $this->getTestImage($name);
|
||||
$file = $this->getTestImage($name, $testDataFileName);
|
||||
return $this->withHeader('Content-Type', $contentType)
|
||||
->call('POST', '/images/gallery', ['uploaded_to' => $uploadedTo], [], ['file' => $file], []);
|
||||
}
|
||||
@ -66,22 +70,23 @@ trait UsesImages
|
||||
* @param Page|null $page
|
||||
* @return array
|
||||
*/
|
||||
protected function uploadGalleryImage(Page $page = null)
|
||||
protected function uploadGalleryImage(Page $page = null, ?string $testDataFileName = null)
|
||||
{
|
||||
if ($page === null) {
|
||||
$page = Page::query()->first();
|
||||
}
|
||||
|
||||
$imageName = 'first-image.png';
|
||||
$imageName = $testDataFileName ?? 'first-image.png';
|
||||
$relPath = $this->getTestImagePath('gallery', $imageName);
|
||||
$this->deleteImage($relPath);
|
||||
|
||||
$upload = $this->uploadImage($imageName, $page->id);
|
||||
$upload = $this->uploadImage($imageName, $page->id, 'image/png', $testDataFileName);
|
||||
$upload->assertStatus(200);
|
||||
return [
|
||||
'name' => $imageName,
|
||||
'path' => $relPath,
|
||||
'page' => $page
|
||||
'page' => $page,
|
||||
'response' => json_decode($upload->getContent()),
|
||||
];
|
||||
}
|
||||
|
||||
|
BIN
tests/test-data/compressed.png
Normal file
BIN
tests/test-data/compressed.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 732 B |
Reference in New Issue
Block a user