/system/src/Grav/Common/Page/Medium/MediumFactory.php

https://gitlab.com/3dplex/3d-plex-main-site · PHP · 205 lines · 138 code · 30 blank · 37 comment · 8 complexity · 8afc14c3a7907c3031f345f4d331c679 MD5 · raw file

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