MediumFactory.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * @package Grav\Common\Page
  4. *
  5. * @copyright Copyright (C) 2015 - 2019 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common\Page\Medium;
  9. use Grav\Common\Grav;
  10. use Grav\Common\Data\Blueprint;
  11. use Grav\Framework\Form\FormFlashFile;
  12. class MediumFactory
  13. {
  14. /**
  15. * Create Medium from a file
  16. *
  17. * @param string $file
  18. * @param array $params
  19. * @return Medium
  20. */
  21. public static function fromFile($file, array $params = [])
  22. {
  23. if (!file_exists($file)) {
  24. return null;
  25. }
  26. $parts = pathinfo($file);
  27. $path = $parts['dirname'];
  28. $filename = $parts['basename'];
  29. $ext = $parts['extension'];
  30. $basename = $parts['filename'];
  31. $config = Grav::instance()['config'];
  32. $media_params = $config->get('media.types.' . strtolower($ext));
  33. if (!\is_array($media_params)) {
  34. return null;
  35. }
  36. $params += $media_params;
  37. // Add default settings for undefined variables.
  38. $params += (array)$config->get('media.types.defaults');
  39. $params += [
  40. 'type' => 'file',
  41. 'thumb' => 'media/thumb.png',
  42. 'mime' => 'application/octet-stream',
  43. 'filepath' => $file,
  44. 'filename' => $filename,
  45. 'basename' => $basename,
  46. 'extension' => $ext,
  47. 'path' => $path,
  48. 'modified' => filemtime($file),
  49. 'thumbnails' => []
  50. ];
  51. $locator = Grav::instance()['locator'];
  52. $file = $locator->findResource("image://{$params['thumb']}");
  53. if ($file) {
  54. $params['thumbnails']['default'] = $file;
  55. }
  56. return static::fromArray($params);
  57. }
  58. /**
  59. * Create Medium from an uploaded file
  60. *
  61. * @param FormFlashFile $uploadedFile
  62. * @param array $params
  63. * @return Medium
  64. */
  65. public static function fromUploadedFile(FormFlashFile $uploadedFile, array $params = [])
  66. {
  67. $parts = pathinfo($uploadedFile->getClientFilename());
  68. $filename = $parts['basename'];
  69. $ext = $parts['extension'];
  70. $basename = $parts['filename'];
  71. $file = $uploadedFile->getTmpFile();
  72. $path = dirname($file);
  73. $config = Grav::instance()['config'];
  74. $media_params = $config->get('media.types.' . strtolower($ext));
  75. if (!\is_array($media_params)) {
  76. return null;
  77. }
  78. $params += $media_params;
  79. // Add default settings for undefined variables.
  80. $params += (array)$config->get('media.types.defaults');
  81. $params += [
  82. 'type' => 'file',
  83. 'thumb' => 'media/thumb.png',
  84. 'mime' => 'application/octet-stream',
  85. 'filepath' => $file,
  86. 'filename' => $filename,
  87. 'basename' => $basename,
  88. 'extension' => $ext,
  89. 'path' => $path,
  90. 'modified' => filemtime($file),
  91. 'thumbnails' => []
  92. ];
  93. $locator = Grav::instance()['locator'];
  94. $file = $locator->findResource("image://{$params['thumb']}");
  95. if ($file) {
  96. $params['thumbnails']['default'] = $file;
  97. }
  98. return static::fromArray($params);
  99. }
  100. /**
  101. * Create Medium from array of parameters
  102. *
  103. * @param array $items
  104. * @param Blueprint|null $blueprint
  105. * @return Medium
  106. */
  107. public static function fromArray(array $items = [], Blueprint $blueprint = null)
  108. {
  109. $type = $items['type'] ?? null;
  110. switch ($type) {
  111. case 'image':
  112. return new ImageMedium($items, $blueprint);
  113. case 'thumbnail':
  114. return new ThumbnailImageMedium($items, $blueprint);
  115. case 'animated':
  116. case 'vector':
  117. return new StaticImageMedium($items, $blueprint);
  118. case 'video':
  119. return new VideoMedium($items, $blueprint);
  120. case 'audio':
  121. return new AudioMedium($items, $blueprint);
  122. default:
  123. return new Medium($items, $blueprint);
  124. }
  125. }
  126. /**
  127. * Create a new ImageMedium by scaling another ImageMedium object.
  128. *
  129. * @param ImageMedium $medium
  130. * @param int $from
  131. * @param int $to
  132. * @return Medium|array
  133. */
  134. public static function scaledFromMedium($medium, $from, $to)
  135. {
  136. if (! $medium instanceof ImageMedium) {
  137. return $medium;
  138. }
  139. if ($to > $from) {
  140. return $medium;
  141. }
  142. $ratio = $to / $from;
  143. $width = $medium->get('width') * $ratio;
  144. $height = $medium->get('height') * $ratio;
  145. $prev_basename = $medium->get('basename');
  146. $basename = str_replace('@'.$from.'x', '@'.$to.'x', $prev_basename);
  147. $debug = $medium->get('debug');
  148. $medium->set('debug', false);
  149. $medium->setImagePrettyName($basename);
  150. $file = $medium->resize($width, $height)->path();
  151. $medium->set('debug', $debug);
  152. $medium->setImagePrettyName($prev_basename);
  153. $size = filesize($file);
  154. $medium = self::fromFile($file);
  155. if ($medium) {
  156. $medium->set('size', $size);
  157. }
  158. return ['file' => $medium, 'size' => $size];
  159. }
  160. }