Handle TIFF files with multiple pages

This commit is contained in:
Alex Cabal 2025-02-08 11:53:40 -06:00
parent 5e23837d17
commit 0c2dce3f63
4 changed files with 32 additions and 14 deletions

View file

@ -865,21 +865,21 @@ class Artwork{
', [$this->ArtworkId]); ', [$this->ArtworkId]);
try{ try{
unlink($this->ImageFsPath); @unlink($this->ImageFsPath);
} }
catch(\Safe\Exceptions\FilesystemException){ catch(\Safe\Exceptions\FilesystemException){
// Pass. // Pass.
} }
try{ try{
unlink($this->ThumbFsPath); @unlink($this->ThumbFsPath);
} }
catch(\Safe\Exceptions\FilesystemException){ catch(\Safe\Exceptions\FilesystemException){
// Pass. // Pass.
} }
try{ try{
unlink($this->Thumb2xFsPath); @unlink($this->Thumb2xFsPath);
} }
catch(\Safe\Exceptions\FilesystemException){ catch(\Safe\Exceptions\FilesystemException){
// Pass. // Pass.

View file

@ -50,7 +50,7 @@ abstract class Feed{
file_put_contents($tempFilename, $xmlString); file_put_contents($tempFilename, $xmlString);
exec('se clean ' . escapeshellarg($tempFilename) . ' 2>&1', $output); // Capture the result in case there's an error, otherwise it prints to stdout exec('se clean ' . escapeshellarg($tempFilename) . ' 2>&1', $output); // Capture the result in case there's an error, otherwise it prints to stdout
$output = file_get_contents($tempFilename); $output = file_get_contents($tempFilename);
unlink($tempFilename); @unlink($tempFilename);
// At the moment, `se clean` strips stylesheet declarations. Restore them here. // At the moment, `se clean` strips stylesheet declarations. Restore them here.
if($this->Stylesheet !== null){ if($this->Stylesheet !== null){

View file

@ -1,5 +1,6 @@
<? <?
use function Safe\exec; use function Safe\exec;
use function Safe\glob;
use function Safe\imagecopyresampled; use function Safe\imagecopyresampled;
use function Safe\imagecreatetruecolor; use function Safe\imagecreatetruecolor;
use function Safe\imagejpeg; use function Safe\imagejpeg;
@ -46,23 +47,40 @@ class Image{
* @throws Exceptions\InvalidImageUploadException * @throws Exceptions\InvalidImageUploadException
*/ */
private function GetImageHandleFromTiff(){ private function GetImageHandleFromTiff(){
$tempFilename = sys_get_temp_dir() . '/se-' . pathinfo($this->Path)['filename'] . '.jpg'; $basename = pathinfo($this->Path)['filename'];
$tempDirectory = sys_get_temp_dir();
$tempFilename = $tempDirectory . '/se-' . $basename . '.jpg';
try{ try{
exec('convert '. escapeshellarg($this->Path) . ' ' . escapeshellarg($tempFilename), $shellOutput, $resultCode); exec('convert '. escapeshellarg($this->Path) . ' ' . escapeshellarg($tempFilename), $shellOutput, $resultCode);
if($resultCode !== 0 || !is_file($tempFilename)){ if($resultCode !== 0){
throw new Exceptions\InvalidImageUploadException('Failed to convert TIFF to JPEG'); throw new Exceptions\InvalidImageUploadException('Failed to convert TIFF to JPEG');
} }
$handle = \Safe\imagecreatefromjpeg($tempFilename); // Sometimes TIFF files can have multiple images, or "pages" in one file. In that case, `convert` outputs multiple files named `<file>-0.jpg`, `<file>-1.jpg`, etc., instead of `<file>.jpg`.
// Test for that case here.
$pagedFilename = $tempDirectory . '/se-' . $basename . '-0.jpg';
if(is_file($pagedFilename)){
// This TIFF has pages!
$handle = \Safe\imagecreatefromjpeg($pagedFilename);
}
elseif(is_file($tempFilename)){
// Regular TIFF.
$handle = \Safe\imagecreatefromjpeg($tempFilename);
}
else{
throw new Exceptions\InvalidImageUploadException('Failed to convert TIFF to JPEG');
}
} }
finally{ finally{
try{ foreach(glob($tempDirectory . '/se-' . $basename . '*.jpg') as $filename){
unlink($tempFilename); try{
} @unlink($filename);
catch(Exception){ }
// Pass if file doesn't exist. catch(Exception){
// Pass.
}
} }
} }

View file

@ -35,7 +35,7 @@ function rrmdir(string $src): void{
rrmdir($full); rrmdir($full);
} }
else{ else{
unlink($full); @unlink($full);
} }
} }
} }
@ -99,7 +99,7 @@ function CreateZip(string $filePath, array $ebooks, string $type, string $webRoo
// We have to do a copy, then unlink because `rename()` can't rename across file systems. // We have to do a copy, then unlink because `rename()` can't rename across file systems.
// If the bulk downloads are symlinked to a storage volume, then `rename()` won't work. // If the bulk downloads are symlinked to a storage volume, then `rename()` won't work.
copy($tempFilename, $filePath); copy($tempFilename, $filePath);
unlink($tempFilename); @unlink($tempFilename);
exec('attr -q -s se-ebook-type -V ' . escapeshellarg($type) . ' ' . escapeshellarg($filePath)); exec('attr -q -s se-ebook-type -V ' . escapeshellarg($type) . ' ' . escapeshellarg($filePath));
} }