Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions lib/private/Preview/Imaginary.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

namespace OC\Preview;

use GuzzleHttp\Psr7\AppendStream;
use GuzzleHttp\Psr7\Utils;
use OC\StreamImage;
use OCP\Files\File;
use OCP\Http\Client\IClientService;
Expand Down Expand Up @@ -108,15 +106,9 @@ public function getCroppedThumbnail(File $file, int $maxX, int $maxY, bool $crop
default:
}

$body = $stream;
if ($autorotate && $file->getMimeType() === 'image/jpeg') {
// Each pipeline operation re-encodes the full image, only rotate when needed
$head = fread($stream, self::EXIF_HEAD_SIZE);
if ($head === false) {
return null;
}
$autorotate = $this->needsRotation($head);
$body = new AppendStream([Utils::streamFor($head), Utils::streamFor($stream)]);
$autorotate = $this->needsRotation($file);
}

$operations = [];
Expand Down Expand Up @@ -166,7 +158,7 @@ public function getCroppedThumbnail(File $file, int $maxX, int $maxY, bool $crop
'headers' => [
'Content-Type' => $file->getMimeType(),
],
'body' => $body,
'body' => $stream,
'nextcloud' => ['allow_local_address' => true],
'timeout' => 120,
'connect_timeout' => 3,
Expand Down Expand Up @@ -201,10 +193,20 @@ public function getCroppedThumbnail(File $file, int $maxX, int $maxY, bool $crop
return $image->valid() ? $image : null;
}

private function needsRotation(string $head): bool {
private function needsRotation(File $file): bool {
if (!is_callable('exif_read_data')) {
return true;
}
// Read separately, the stream sent to Imaginary has to start at the beginning of the file
$stream = $file->fopen('r');
if (!is_resource($stream)) {
return true;
}
$head = fread($stream, self::EXIF_HEAD_SIZE);
fclose($stream);
if ($head === false) {
return true;
}
$exif = @exif_read_data('data://image/jpeg;base64,' . base64_encode($head), 'IFD0');
return $exif !== false && isset($exif['Orientation']) && (int)$exif['Orientation'] !== 1;
}
Expand Down
54 changes: 33 additions & 21 deletions tests/lib/Preview/ImaginaryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,34 +38,46 @@ protected function tearDown(): void {
parent::tearDown();
}

private function getJpeg(?int $orientation): string {
$image = imagecreatetruecolor(40, 30);
private function getJpeg(?int $orientation, bool $large = false): string {
$image = imagecreatetruecolor($large ? 600 : 40, $large ? 600 : 30);
if ($large) {
// Noise does not compress, the image data gets larger than the part read for the orientation
for ($x = 0; $x < 600; $x++) {
for ($y = 0; $y < 600; $y++) {
imagesetpixel($image, $x, $y, mt_rand(0, 0xffffff));
}
}
}
ob_start();
imagejpeg($image);
imagejpeg($image, null, 100);
$jpeg = ob_get_clean();
if ($orientation === null) {
return $jpeg;
}

// EXIF segment with only the orientation
$tiff = "II*\0" . pack('V', 8) . pack('v', 1) . pack('vvVvv', 0x0112, 3, 1, $orientation, 0) . pack('V', 0);
$app1 = "\xFF\xE1" . pack('n', 2 + 6 + strlen($tiff)) . "Exif\0\0" . $tiff;
return substr($jpeg, 0, 2) . $app1 . substr($jpeg, 2);
$segments = '';
if ($orientation !== null) {
// EXIF segment with only the orientation
$tiff = "II*\0" . pack('V', 8) . pack('v', 1) . pack('vvVvv', 0x0112, 3, 1, $orientation, 0) . pack('V', 0);
$segments .= "\xFF\xE1" . pack('n', 2 + 6 + strlen($tiff)) . "Exif\0\0" . $tiff;
}
return substr($jpeg, 0, 2) . $segments . substr($jpeg, 2);
}

#[TestWith([null, false])]
#[TestWith([1, false])]
#[TestWith([6, true])]
public function testAutorotateOnlyWhenNeeded(?int $orientation, bool $expectAutorotate): void {
$jpeg = $this->getJpeg($orientation);
$stream = fopen('php://memory', 'r+');
fwrite($stream, $jpeg);
rewind($stream);
#[TestWith([null, false, false])]
#[TestWith([1, false, false])]
#[TestWith([6, true, false])]
#[TestWith([null, false, true])]
#[TestWith([6, true, true])]
public function testAutorotateOnlyWhenNeeded(?int $orientation, bool $expectAutorotate, bool $large): void {
$jpeg = $this->getJpeg($orientation, $large);

$file = $this->createMock(File::class);
$file->method('getSize')->willReturn(strlen($jpeg));
$file->method('getMimeType')->willReturn('image/jpeg');
$file->method('fopen')->willReturn($stream);
$file->method('fopen')->willReturnCallback(function () use ($jpeg) {
$stream = fopen('php://memory', 'r+');
fwrite($stream, $jpeg);
rewind($stream);
return $stream;
});

$response = $this->createMock(IResponse::class);
$response->method('getStatusCode')->willReturn(200);
Expand All @@ -81,9 +93,9 @@ public function testAutorotateOnlyWhenNeeded(?int $orientation, bool $expectAuto
$operations = array_column(json_decode($options['query']['operations'], true), 'operation');
$this->assertSame($expectAutorotate ? ['autorotate', 'fit'] : ['fit'], $operations);

// The whole file is sent, including the head read for the orientation
// Read from the start, like the HTTP client does
$body = $options['body'];
$this->assertSame($jpeg, $body instanceof StreamInterface ? $body->getContents() : stream_get_contents($body));
$this->assertSame($jpeg, $body instanceof StreamInterface ? (string)$body : stream_get_contents($body, -1, 0));
return $response;
});

Expand Down
Loading