1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2021 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Factories; 21 22use Fig\Http\Message\StatusCodeInterface; 23use Fisharebest\Webtrees\Auth; 24use Fisharebest\Webtrees\Contracts\ImageFactoryInterface; 25use Fisharebest\Webtrees\Contracts\UserInterface; 26use Fisharebest\Webtrees\MediaFile; 27use Fisharebest\Webtrees\Mime; 28use Fisharebest\Webtrees\Registry; 29use Fisharebest\Webtrees\Webtrees; 30use Imagick; 31use Intervention\Image\Constraint; 32use Intervention\Image\Exception\NotReadableException; 33use Intervention\Image\Exception\NotSupportedException; 34use Intervention\Image\Image; 35use Intervention\Image\ImageManager; 36use League\Flysystem\FilesystemException; 37use League\Flysystem\FilesystemOperator; 38use League\Flysystem\UnableToReadFile; 39use League\Flysystem\UnableToRetrieveMetadata; 40use Psr\Http\Message\ResponseInterface; 41use RuntimeException; 42use Throwable; 43 44use function addcslashes; 45use function basename; 46use function extension_loaded; 47use function get_class; 48use function pathinfo; 49use function response; 50use function strlen; 51use function view; 52 53use const PATHINFO_EXTENSION; 54 55/** 56 * Make an image (from another image). 57 */ 58class ImageFactory implements ImageFactoryInterface 59{ 60 // Imagick can detect the quality setting for images. GD cannot. 61 protected const GD_DEFAULT_IMAGE_QUALITY = 90; 62 protected const GD_DEFAULT_THUMBNAIL_QUALITY = 70; 63 64 protected const WATERMARK_FILE = 'resources/img/watermark.png'; 65 66 protected const THUMBNAIL_CACHE_TTL = 8640000; 67 68 protected const INTERVENTION_DRIVERS = ['imagick', 'gd']; 69 70 protected const INTERVENTION_FORMATS = [ 71 'image/jpeg' => 'jpg', 72 'image/png' => 'png', 73 'image/gif' => 'gif', 74 'image/tiff' => 'tif', 75 'image/bmp' => 'bmp', 76 'image/webp' => 'webp', 77 ]; 78 79 /** 80 * Send the original file - either inline or as a download. 81 * 82 * @param FilesystemOperator $filesystem 83 * @param string $path 84 * @param bool $download 85 * 86 * @return ResponseInterface 87 */ 88 public function fileResponse(FilesystemOperator $filesystem, string $path, bool $download): ResponseInterface 89 { 90 try { 91 $data = $filesystem->read($path); 92 93 try { 94 $content_type = $filesystem->mimeType($path); 95 } catch (UnableToRetrieveMetadata $ex) { 96 $content_type = Mime::DEFAULT_TYPE; 97 } 98 99 $headers = [ 100 'Content-Type' => $content_type, 101 ]; 102 103 if ($download) { 104 $headers['Content-Disposition'] = 'attachment; filename="' . addcslashes(basename($path), '"'); 105 } 106 107 return response($data, StatusCodeInterface::STATUS_OK, $headers); 108 } catch (FilesystemException | UnableToReadFile $ex) { 109 return $this->replacementImageResponse((string) StatusCodeInterface::STATUS_NOT_FOUND) 110 ->withHeader('X-Thumbnail-Exception', get_class($ex) . ': ' . $ex->getMessage()); 111 } 112 } 113 114 /** 115 * Send a thumbnail. 116 * 117 * @param FilesystemOperator $filesystem 118 * @param string $path 119 * @param int $width 120 * @param int $height 121 * @param string $fit 122 * 123 * 124 * @return ResponseInterface 125 */ 126 public function thumbnailResponse( 127 FilesystemOperator $filesystem, 128 string $path, 129 int $width, 130 int $height, 131 string $fit 132 ): ResponseInterface { 133 try { 134 $image = $this->imageManager()->make($filesystem->readStream($path)); 135 $image = $this->autorotateImage($image); 136 $image = $this->resizeImage($image, $width, $height, $fit); 137 138 $format = static::INTERVENTION_FORMATS[$image->mime()] ?? 'jpg'; 139 $quality = $this->extractImageQuality($image, static::GD_DEFAULT_THUMBNAIL_QUALITY); 140 $data = (string) $image->encode($format, $quality); 141 142 return $this->imageResponse($data, $image->mime(), ''); 143 } catch (NotReadableException $ex) { 144 return $this->replacementImageResponse('.' . pathinfo($path, PATHINFO_EXTENSION)) 145 ->withHeader('X-Thumbnail-Exception', get_class($ex) . ': ' . $ex->getMessage()); 146 } catch (FilesystemException | UnableToReadFile $ex) { 147 return $this->replacementImageResponse((string) StatusCodeInterface::STATUS_NOT_FOUND); 148 } catch (Throwable $ex) { 149 return $this->replacementImageResponse((string) StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR) 150 ->withHeader('X-Thumbnail-Exception', get_class($ex) . ': ' . $ex->getMessage()); 151 } 152 } 153 154 /** 155 * Create a full-size version of an image. 156 * 157 * @param MediaFile $media_file 158 * @param bool $add_watermark 159 * @param bool $download 160 * 161 * @return ResponseInterface 162 */ 163 public function mediaFileResponse(MediaFile $media_file, bool $add_watermark, bool $download): ResponseInterface 164 { 165 $filesystem = Registry::filesystem()->media($media_file->media()->tree()); 166 $filename = $media_file->filename(); 167 168 if (!$add_watermark || !$media_file->isImage()) { 169 return $this->fileResponse($filesystem, $filename, $download); 170 } 171 172 try { 173 $image = $this->imageManager()->make($filesystem->readStream($filename)); 174 $image = $this->autorotateImage($image); 175 176 $watermark_image = $this->createWatermark($image->width(), $image->height(), $media_file); 177 178 $image = $this->addWatermark($image, $watermark_image); 179 180 $download_filename = $download ? basename($filename) : ''; 181 182 $format = static::INTERVENTION_FORMATS[$image->mime()] ?? 'jpg'; 183 $quality = $this->extractImageQuality($image, static::GD_DEFAULT_IMAGE_QUALITY); 184 $data = (string) $image->encode($format, $quality); 185 186 return $this->imageResponse($data, $image->mime(), $download_filename); 187 } catch (NotReadableException $ex) { 188 return $this->replacementImageResponse(pathinfo($filename, PATHINFO_EXTENSION)) 189 ->withHeader('X-Image-Exception', $ex->getMessage()); 190 } catch (FilesystemException | UnableToReadFile $ex) { 191 return $this->replacementImageResponse((string) StatusCodeInterface::STATUS_NOT_FOUND); 192 } catch (Throwable $ex) { 193 return $this->replacementImageResponse((string) StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR) 194 ->withHeader('X-Image-Exception', $ex->getMessage()); 195 } 196 } 197 198 /** 199 * Create a smaller version of an image. 200 * 201 * @param MediaFile $media_file 202 * @param int $width 203 * @param int $height 204 * @param string $fit 205 * @param bool $add_watermark 206 * 207 * @return ResponseInterface 208 */ 209 public function mediaFileThumbnailResponse( 210 MediaFile $media_file, 211 int $width, 212 int $height, 213 string $fit, 214 bool $add_watermark 215 ): ResponseInterface { 216 // Where are the images stored. 217 $filesystem = Registry::filesystem()->media($media_file->media()->tree()); 218 219 // Where is the image stored in the filesystem. 220 $path = $media_file->filename(); 221 222 try { 223 $mime_type = $filesystem->mimeType($path); 224 225 $key = implode(':', [ 226 $media_file->media()->tree()->name(), 227 $path, 228 $filesystem->lastModified($path), 229 (string) $width, 230 (string) $height, 231 $fit, 232 (string) $add_watermark, 233 ]); 234 235 $closure = function () use ($filesystem, $path, $width, $height, $fit, $add_watermark, $media_file): string { 236 $image = $this->imageManager()->make($filesystem->readStream($path)); 237 $image = $this->autorotateImage($image); 238 $image = $this->resizeImage($image, $width, $height, $fit); 239 240 if ($add_watermark) { 241 $watermark = $this->createWatermark($image->width(), $image->height(), $media_file); 242 $image = $this->addWatermark($image, $watermark); 243 } 244 245 $format = static::INTERVENTION_FORMATS[$image->mime()] ?? 'jpg'; 246 $quality = $this->extractImageQuality($image, static::GD_DEFAULT_THUMBNAIL_QUALITY); 247 248 return (string) $image->encode($format, $quality); 249 }; 250 251 // Images and Responses both contain resources - which cannot be serialized. 252 // So cache the raw image data. 253 $data = Registry::cache()->file()->remember($key, $closure, static::THUMBNAIL_CACHE_TTL); 254 255 return $this->imageResponse($data, $mime_type, ''); 256 } catch (NotReadableException $ex) { 257 return $this->replacementImageResponse('.' . pathinfo($path, PATHINFO_EXTENSION)) 258 ->withHeader('X-Thumbnail-Exception', get_class($ex) . ': ' . $ex->getMessage()); 259 } catch (FilesystemException | UnableToReadFile $ex) { 260 return $this->replacementImageResponse((string) StatusCodeInterface::STATUS_NOT_FOUND); 261 } catch (Throwable $ex) { 262 return $this->replacementImageResponse((string) StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR) 263 ->withHeader('X-Thumbnail-Exception', get_class($ex) . ': ' . $ex->getMessage()); 264 } 265 } 266 267 /** 268 * Does a full-sized image need a watermark? 269 * 270 * @param MediaFile $media_file 271 * @param UserInterface $user 272 * 273 * @return bool 274 */ 275 public function fileNeedsWatermark(MediaFile $media_file, UserInterface $user): bool 276 { 277 $tree = $media_file->media()->tree(); 278 279 return Auth::accessLevel($tree, $user) > $tree->getPreference('SHOW_NO_WATERMARK'); 280 } 281 282 /** 283 * Does a thumbnail image need a watermark? 284 * 285 * @param MediaFile $media_file 286 * @param UserInterface $user 287 * 288 * @return bool 289 */ 290 public function thumbnailNeedsWatermark(MediaFile $media_file, UserInterface $user): bool 291 { 292 return $this->fileNeedsWatermark($media_file, $user); 293 } 294 295 /** 296 * Create a watermark image, perhaps specific to a media-file. 297 * 298 * @param int $width 299 * @param int $height 300 * @param MediaFile $media_file 301 * 302 * @return Image 303 */ 304 public function createWatermark(int $width, int $height, MediaFile $media_file): Image 305 { 306 return $this->imageManager() 307 ->make(Webtrees::ROOT_DIR . static::WATERMARK_FILE) 308 ->resize($width, $height, static function (Constraint $constraint) { 309 $constraint->aspectRatio(); 310 }); 311 } 312 313 /** 314 * Add a watermark to an image. 315 * 316 * @param Image $image 317 * @param Image $watermark 318 * 319 * @return Image 320 */ 321 public function addWatermark(Image $image, Image $watermark): Image 322 { 323 return $image->insert($watermark, 'center'); 324 } 325 326 /** 327 * Send a replacement image, to replace one that could not be found or created. 328 * 329 * @param string $text HTTP status code or file extension 330 * 331 * @return ResponseInterface 332 */ 333 public function replacementImageResponse(string $text): ResponseInterface 334 { 335 // We can't create a PNG/BMP/JPEG image, as the GD/IMAGICK libraries may be missing. 336 $svg = view('errors/image-svg', ['status' => $text]); 337 338 // We can't send the actual status code, as browsers won't show images with 4xx/5xx. 339 return response($svg, StatusCodeInterface::STATUS_OK, [ 340 'Content-Type' => 'image/svg+xml', 341 ]); 342 } 343 344 /** 345 * @param string $data 346 * @param string $mime_type 347 * @param string $filename 348 * 349 * @return ResponseInterface 350 */ 351 protected function imageResponse(string $data, string $mime_type, string $filename): ResponseInterface 352 { 353 $headers = [ 354 'Content-Type' => $mime_type, 355 ]; 356 357 if ($filename !== '') { 358 $headers['Content-Disposition'] = 'attachment; filename="' . addcslashes(basename($filename), '"'); 359 } 360 361 return response($data, StatusCodeInterface::STATUS_OK, $headers); 362 } 363 364 /** 365 * @return ImageManager 366 * @throws RuntimeException 367 */ 368 protected function imageManager(): ImageManager 369 { 370 foreach (static::INTERVENTION_DRIVERS as $driver) { 371 if (extension_loaded($driver)) { 372 return new ImageManager(['driver' => $driver]); 373 } 374 } 375 376 throw new RuntimeException('No PHP graphics library is installed. Need Imagick or GD'); 377 } 378 379 /** 380 * Apply EXIF rotation to an image. 381 * 382 * @param Image $image 383 * 384 * @return Image 385 */ 386 protected function autorotateImage(Image $image): Image 387 { 388 try { 389 // Auto-rotate using EXIF information. 390 return $image->orientate(); 391 } catch (NotSupportedException $ex) { 392 // If we can't auto-rotate the image, then don't. 393 return $image; 394 } 395 } 396 397 /** 398 * Resize an image. 399 * 400 * @param Image $image 401 * @param int $width 402 * @param int $height 403 * @param string $fit 404 * 405 * @return Image 406 */ 407 protected function resizeImage(Image $image, int $width, int $height, string $fit): Image 408 { 409 switch ($fit) { 410 case 'crop': 411 return $image->fit($width, $height); 412 case 'contain': 413 return $image->resize($width, $height, static function (Constraint $constraint) { 414 $constraint->aspectRatio(); 415 $constraint->upsize(); 416 }); 417 } 418 419 return $image; 420 } 421 422 /** 423 * Extract the quality/compression parameter from an image. 424 * 425 * @param Image $image 426 * @param int $default 427 * 428 * @return int 429 */ 430 protected function extractImageQuality(Image $image, int $default): int 431 { 432 $core = $image->getCore(); 433 434 if ($core instanceof Imagick) { 435 return $core->getImageCompressionQuality() ?: $default; 436 } 437 438 return $default; 439 } 440} 441