Brak opisu

Image.php 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace MailPoet\Newsletter\Renderer\Blocks;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\Newsletter\Renderer\EscapeHelper as EHelper;
  5. use MailPoet\Newsletter\Renderer\StylesHelper;
  6. use MailPoet\WP\Functions as WPFunctions;
  7. class Image {
  8. public function render($element, $columnBaseWidth) {
  9. if (empty($element['src'])) {
  10. return '';
  11. }
  12. if (substr($element['src'], 0, 1) == '/' && substr($element['src'], 1, 1) != '/') {
  13. $element['src'] = WPFunctions::get()->getOption('siteurl') . $element['src'];
  14. }
  15. $element['width'] = str_replace('px', '', $element['width']);
  16. $element['height'] = str_replace('px', '', $element['height']);
  17. $originalWidth = 0;
  18. if (is_numeric($element['width']) && is_numeric($element['height'])) {
  19. $element['width'] = (int)$element['width'];
  20. $element['height'] = (int)$element['height'];
  21. $originalWidth = $element['width'];
  22. $element = $this->adjustImageDimensions($element, $columnBaseWidth);
  23. }
  24. // If image was downsized because of column width set width to aways fill full column (e.g. on mobile)
  25. $style = '';
  26. if ($element['fullWidth'] === true && $originalWidth > $element['width']) {
  27. $style = 'style="width:100%"';
  28. }
  29. $imageTemplate = '
  30. <img src="' . EHelper::escapeHtmlLinkAttr($element['src']) . '" width="' . EHelper::escapeHtmlAttr($element['width']) . '" alt="' . EHelper::escapeHtmlAttr($element['alt']) . '"' . $style . '/>
  31. ';
  32. if (!empty($element['link'])) {
  33. $imageTemplate = '<a href="' . EHelper::escapeHtmlLinkAttr($element['link']) . '">' . trim($imageTemplate) . '</a>';
  34. }
  35. $align = 'center';
  36. if (!empty($element['styles']['block']['textAlign']) && in_array($element['styles']['block']['textAlign'], ['left', 'right'])) {
  37. $align = $element['styles']['block']['textAlign'];
  38. }
  39. $template = '
  40. <tr>
  41. <td class="mailpoet_image ' . (($element['fullWidth'] === false) ? 'mailpoet_padded_vertical mailpoet_padded_side' : '') . '" align="' . EHelper::escapeHtmlAttr($align) . '" valign="top">
  42. ' . trim($imageTemplate) . '
  43. </td>
  44. </tr>';
  45. return $template;
  46. }
  47. public function adjustImageDimensions($element, $columnBaseWidth) {
  48. $paddedWidth = StylesHelper::$paddingWidth * 2;
  49. // scale image to fit column width
  50. if ($element['width'] > $columnBaseWidth) {
  51. $ratio = $element['width'] / $columnBaseWidth;
  52. $element['width'] = $columnBaseWidth;
  53. $element['height'] = (int)ceil($element['height'] / $ratio);
  54. }
  55. // resize image if the image is padded and wider than padded column width
  56. if ($element['fullWidth'] === false &&
  57. $element['width'] > ($columnBaseWidth - $paddedWidth)
  58. ) {
  59. $ratio = $element['width'] / ($columnBaseWidth - $paddedWidth);
  60. $element['width'] = $columnBaseWidth - $paddedWidth;
  61. $element['height'] = (int)ceil($element['height'] / $ratio);
  62. }
  63. return $element;
  64. }
  65. }