ImageUploadTransformationsPlugin.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * Abstract class for the image upload input transformations plugins
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Plugins\Transformations\Abs;
  7. use PhpMyAdmin\Plugins\IOTransformationsPlugin;
  8. use PhpMyAdmin\Url;
  9. use stdClass;
  10. use function bin2hex;
  11. use function intval;
  12. /**
  13. * Provides common methods for all of the image upload transformations plugins.
  14. */
  15. abstract class ImageUploadTransformationsPlugin extends IOTransformationsPlugin
  16. {
  17. /**
  18. * Gets the transformation description of the specific plugin
  19. *
  20. * @return string
  21. */
  22. public static function getInfo()
  23. {
  24. return __(
  25. 'Image upload functionality which also displays a thumbnail.'
  26. . ' The options are the width and height of the thumbnail'
  27. . ' in pixels. Defaults to 100 X 100.'
  28. );
  29. }
  30. /**
  31. * Does the actual work of each specific transformations plugin.
  32. *
  33. * @param string $buffer text to be transformed
  34. * @param array $options transformation options
  35. * @param stdClass|null $meta meta information
  36. *
  37. * @return string
  38. */
  39. public function applyTransformation($buffer, array $options = [], ?stdClass $meta = null)
  40. {
  41. return $buffer;
  42. }
  43. /**
  44. * Returns the html for input field to override default textarea.
  45. * Note: Return empty string if default textarea is required.
  46. *
  47. * @param array $column column details
  48. * @param int $row_id row number
  49. * @param string $column_name_appendix the name attribute
  50. * @param array $options transformation options
  51. * @param string $value Current field value
  52. * @param string $text_dir text direction
  53. * @param int $tabindex tab index
  54. * @param int $tabindex_for_value offset for the values tabindex
  55. * @param int $idindex id index
  56. *
  57. * @return string the html for input field
  58. */
  59. public function getInputHtml(
  60. array $column,
  61. $row_id,
  62. $column_name_appendix,
  63. array $options,
  64. $value,
  65. $text_dir,
  66. $tabindex,
  67. $tabindex_for_value,
  68. $idindex
  69. ) {
  70. $html = '';
  71. $src = '';
  72. if (! empty($value)) {
  73. $html = '<input type="hidden" name="fields_prev' . $column_name_appendix
  74. . '" value="' . bin2hex($value) . '">';
  75. $html .= '<input type="hidden" name="fields' . $column_name_appendix
  76. . '" value="' . bin2hex($value) . '">';
  77. $src = Url::getFromRoute('/transformation/wrapper', $options['wrapper_params']);
  78. }
  79. $html .= '<img src="' . $src . '" width="'
  80. . (isset($options[0]) ? intval($options[0]) : '100') . '" height="'
  81. . (isset($options[1]) ? intval($options[1]) : '100') . '" alt="'
  82. . __('Image preview here') . '">';
  83. $html .= '<br><input type="file" name="fields_upload'
  84. . $column_name_appendix . '" accept="image/*" class="image-upload">';
  85. return $html;
  86. }
  87. /**
  88. * Returns the array of scripts (filename) required for plugin
  89. * initialization and handling
  90. *
  91. * @return array javascripts to be included
  92. */
  93. public function getScripts()
  94. {
  95. return ['transformations/image_upload.js'];
  96. }
  97. /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */
  98. /**
  99. * Gets the transformation name of the specific plugin
  100. *
  101. * @return string
  102. */
  103. public static function getName()
  104. {
  105. return 'Image upload';
  106. }
  107. }