Skip to content

Commit

Permalink
fix: create previews from first page on multi-page documents
Browse files Browse the repository at this point in the history
  • Loading branch information
DeepDiver1975 committed Oct 12, 2023
1 parent b3902c9 commit 21f2d28
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 12 deletions.
6 changes: 6 additions & 0 deletions changelog/unreleased/41045
@@ -0,0 +1,6 @@
Bugfix: create previews from first page

On multi-page documents (doc, odt, pdf etc) the preview will now be generated
from the first page and no longer from the last page.

https://github.com/owncloud/core/pull/41045
3 changes: 2 additions & 1 deletion lib/private/Preview/Bitmap.php
Expand Up @@ -83,7 +83,8 @@ public function isAvailable(FileInfo $file) {
private function getResizedPreview($stream, $maxX, $maxY) {
$bp = new Imagick();

$bp->readImageFile($stream);
# using [0] as file name will tell Imagick to use the first page
$bp->readImageFile($stream, '[0]');

$bp = $this->resize($bp, $maxX, $maxY);

Expand Down
6 changes: 2 additions & 4 deletions lib/private/PreviewManager.php
Expand Up @@ -327,8 +327,6 @@ protected function registerCoreProviders() {
// This library adds imagick support for SVG, WMF, OpenEXR, DjVu and Graphviz:

if (\extension_loaded('imagick')) {
$checkImagick = new \Imagick();

$imagickProviders = [
'SVG' => ['mimetype' => '/image\/svg\+xml/', 'class' => '\OC\Preview\SVG'],
'TIFF' => ['mimetype' => '/image\/tiff/', 'class' => '\OC\Preview\TIFF'],
Expand All @@ -348,12 +346,12 @@ protected function registerCoreProviders() {
continue;
}

if (\count($checkImagick->queryFormats($queryFormat)) === 1) {
if (\count(\Imagick::queryFormats($queryFormat)) === 1) {
$this->registerCoreProvider($class, $provider['mimetype']);
}
}

if (\count($checkImagick->queryFormats('PDF')) === 1) {
if (\count(\Imagick::queryFormats('PDF')) === 1) {
// Office previews are only supported if either LibreOffice or OpenOffice is installed on the server
if (\OC_Helper::is_function_enabled('shell_exec')) {
$officeFound = \is_string($this->config->getSystemValue('preview_libreoffice_path', null));
Expand Down
Binary file modified tests/data/testimage.odt
Binary file not shown.
Binary file added tests/data/testimage.pdf
Binary file not shown.
52 changes: 52 additions & 0 deletions tests/lib/Preview/PDFTest.php
@@ -0,0 +1,52 @@
<?php
/**
* @author Olivier Paroz <owncloud@interfasys.ch>
*
* @copyright Copyright (c) 2018, ownCloud GmbH
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace lib\Preview;

use OC\Preview\PDF;
use OCP\Files\NotFoundException;
use Test\Preview\Provider;

/**
* Class SVGTest
*
* @group DB
*
* @package Test\Preview
*/
class PDFTest extends Provider {
/**
* @throws NotFoundException
*/
public function setUp(): void {
if (\count(\Imagick::queryFormats('SVG')) === 1) {
parent::setUp();

$fileName = 'testimage.pdf';
$this->imgPath = $this->prepareTestFile($fileName, \OC::$SERVERROOT . '/tests/data/' . $fileName);
$this->width = 595;
$this->height = 842;
$this->provider = new PDF();
} else {
$this->markTestSkipped('No PDF provider present');
}
}
}
11 changes: 4 additions & 7 deletions tests/lib/Preview/Provider.php
Expand Up @@ -21,12 +21,14 @@

namespace Test\Preview;

use OC\Preview\TXT;
use OCP\Files\File;
use OCP\Files\Node;
use OCP\Preview\IProvider2;
use Test\TestCase;
use Test\Traits\UserTrait;

abstract class Provider extends \Test\TestCase {
abstract class Provider extends TestCase {
use UserTrait;

/** @var File */
Expand Down Expand Up @@ -92,15 +94,10 @@ public function testGetThumbnail($widthAdjustment, $heightAdjustment) {
$this->maxWidth = $this->width - $widthAdjustment;
$this->maxHeight = $this->height - $heightAdjustment;

// Testing code
/*print_r("w $this->width ");
print_r("h $this->height ");
print_r("r $ratio ");*/

$preview = $this->getPreview($this->provider);
// The TXT provider uses the max dimensions to create its canvas,
// so the ratio will always be the one of the max dimension canvas
if (!$this->provider instanceof \OC\Preview\TXT) {
if (!$this->provider instanceof TXT) {
$this->doesRatioMatch($preview, $ratio);
}
$this->doesPreviewFit($preview);
Expand Down

0 comments on commit 21f2d28

Please sign in to comment.