ImageUploadTransformationsPlugin.php 3.7 KB

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