PageRenderTime 76ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/filesystem/Image.php

http://github.com/silverstripe/sapphire
PHP | 134 lines | 73 code | 14 blank | 47 comment | 12 complexity | ff2f2eecaa3478083726b3a3198cc215 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT, CC-BY-3.0, GPL-2.0, AGPL-1.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Represents an Image
  4. *
  5. * @package framework
  6. * @subpackage filesystem
  7. */
  8. class Image extends File implements ShortcodeHandler {
  9. public function __construct($record = null, $isSingleton = false, $model = null, $queryParams = array()) {
  10. parent::__construct($record, $isSingleton, $model, $queryParams);
  11. $this->File->setAllowedCategories('image/supported');
  12. }
  13. public function getCMSFields() {
  14. $fields = parent::getCMSFields();
  15. $fields->insertAfter(
  16. 'LastEdited',
  17. new ReadonlyField("Dimensions", _t('AssetTableField.DIM','Dimensions') . ':')
  18. );
  19. return $fields;
  20. }
  21. public function getIsImage() {
  22. return true;
  23. }
  24. /**
  25. * Replace"[image id=n]" shortcode with an image reference.
  26. * Permission checks will be enforced by the file routing itself.
  27. *
  28. * @param array $args Arguments passed to the parser
  29. * @param string $content Raw shortcode
  30. * @param ShortcodeParser $parser Parser
  31. * @param string $shortcode Name of shortcode used to register this handler
  32. * @param array $extra Extra arguments
  33. * @return string Result of the handled shortcode
  34. */
  35. public static function handle_shortcode($args, $content, $parser, $shortcode, $extra = array()) {
  36. // Find appropriate record, with fallback for error handlers
  37. $record = static::find_shortcode_record($args, $errorCode);
  38. if($errorCode) {
  39. $record = static::find_error_record($errorCode);
  40. }
  41. if (!$record) {
  42. return null; // There were no suitable matches at all.
  43. }
  44. // Check if a resize is required
  45. $src = $record->Link();
  46. if($record instanceof Image) {
  47. $width = isset($args['width']) ? $args['width'] : null;
  48. $height = isset($args['height']) ? $args['height'] : null;
  49. $hasCustomDimensions = ($width && $height);
  50. if ($hasCustomDimensions && (($width != $record->getWidth()) || ($height != $record->getHeight()))) {
  51. $resized = $record->ResizedImage($width, $height);
  52. // Make sure that the resized image actually returns an image
  53. if($resized) {
  54. $src = $resized->getURL();
  55. }
  56. }
  57. }
  58. // Build the HTML tag
  59. $attrs = array_merge(
  60. // Set overrideable defaults
  61. ['src' => '', 'alt' => $record->Title],
  62. // Use all other shortcode arguments
  63. $args,
  64. // But enforce some values
  65. ['id' => '', 'src' => $src]
  66. );
  67. // Clean out any empty attributes
  68. $attrs = array_filter($attrs, function($v) {return (bool)$v;});
  69. // Condense to HTML attribute string
  70. $attrsStr = join(' ', array_map(function($name) use ($attrs) {
  71. return Convert::raw2att($name) . '="' . Convert::raw2att($attrs[$name]) . '"';
  72. }, array_keys($attrs)));
  73. return '<img ' . $attrsStr . ' />';
  74. }
  75. /**
  76. * Regenerates "[image id=n]" shortcode with new src attribute prior to being edited within the CMS.
  77. *
  78. * @param array $args Arguments passed to the parser
  79. * @param string $content Raw shortcode
  80. * @param ShortcodeParser $parser Parser
  81. * @param string $shortcode Name of shortcode used to register this handler
  82. * @param array $extra Extra arguments
  83. * @return string Result of the handled shortcode
  84. */
  85. public static function regenerate_shortcode($args, $content, $parser, $shortcode, $extra = array()) {
  86. // Check if there is a suitable record
  87. $record = static::find_shortcode_record($args);
  88. if($record) {
  89. $args['src'] = $record->getURL();
  90. }
  91. // Rebuild shortcode
  92. $parts = array();
  93. foreach($args as $name => $value) {
  94. $htmlValue = Convert::raw2att($value ?: $name);
  95. $parts[] = sprintf('%s="%s"', $name, $htmlValue);
  96. }
  97. return sprintf("[%s %s]", $shortcode, implode(' ', $parts));
  98. }
  99. /**
  100. * Helper method to regenerate all shortcode links.
  101. *
  102. * @param string $value HTML value
  103. * @return string value with links resampled
  104. */
  105. public static function regenerate_html_links($value) {
  106. // Create a shortcode generator which only regenerates links
  107. $regenerator = ShortcodeParser::get('regenerator');
  108. return $regenerator->parse($value);
  109. }
  110. public function PreviewLink($action = null) {
  111. // Since AbsoluteLink can whitelist protected assets,
  112. // do permission check first
  113. if(!$this->canView()) {
  114. return false;
  115. }
  116. $link = $this->AbsoluteLink();
  117. $this->extend('updatePreviewLink', $link, $action);
  118. return $link;
  119. }
  120. }