transformation_wrapper.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. *
  5. * @package PhpMyAdmin
  6. */
  7. /**
  8. *
  9. */
  10. define('IS_TRANSFORMATION_WRAPPER', true);
  11. /**
  12. * Gets a core script and starts output buffering work
  13. */
  14. require_once './libraries/common.inc.php';
  15. require_once './libraries/transformations.lib.php'; // Transformations
  16. $cfgRelation = PMA_getRelationsParam();
  17. /**
  18. * Ensures db and table are valid, else moves to the "parent" script
  19. */
  20. require_once './libraries/db_table_exists.lib.php';
  21. /**
  22. * Sets globals from $_REQUEST
  23. */
  24. $request_params = array(
  25. 'cn',
  26. 'ct',
  27. 'newHeight',
  28. 'newWidth',
  29. 'resize',
  30. 'sql_query',
  31. 'transform_key',
  32. 'where_clause'
  33. );
  34. $size_params = array(
  35. 'newHeight',
  36. 'newWidth',
  37. );
  38. foreach ($request_params as $one_request_param) {
  39. if (isset($_REQUEST[$one_request_param])) {
  40. if (in_array($one_request_param, $size_params)) {
  41. $GLOBALS[$one_request_param] = intval($_REQUEST[$one_request_param]);
  42. if ($GLOBALS[$one_request_param] > 2000) {
  43. $GLOBALS[$one_request_param] = 2000;
  44. }
  45. } else {
  46. $GLOBALS[$one_request_param] = $_REQUEST[$one_request_param];
  47. }
  48. }
  49. }
  50. /**
  51. * Get the list of the fields of the current table
  52. */
  53. PMA_DBI_select_db($db);
  54. if (isset($where_clause)) {
  55. $result = PMA_DBI_query(
  56. 'SELECT * FROM ' . PMA_Util::backquote($table) . ' WHERE ' . $where_clause . ';',
  57. null,
  58. PMA_DBI_QUERY_STORE
  59. );
  60. $row = PMA_DBI_fetch_assoc($result);
  61. } else {
  62. $result = PMA_DBI_query(
  63. 'SELECT * FROM ' . PMA_Util::backquote($table) . ' LIMIT 1;',
  64. null,
  65. PMA_DBI_QUERY_STORE
  66. );
  67. $row = PMA_DBI_fetch_assoc($result);
  68. }
  69. // No row returned
  70. if (! $row) {
  71. exit;
  72. } // end if (no record returned)
  73. $default_ct = 'application/octet-stream';
  74. if ($cfgRelation['commwork'] && $cfgRelation['mimework']) {
  75. $mime_map = PMA_getMime($db, $table);
  76. $mime_options = PMA_transformation_getOptions(
  77. isset($mime_map[$transform_key]['transformation_options'])
  78. ? $mime_map[$transform_key]['transformation_options'] : ''
  79. );
  80. foreach ($mime_options AS $key => $option) {
  81. if (substr($option, 0, 10) == '; charset=') {
  82. $mime_options['charset'] = $option;
  83. }
  84. }
  85. }
  86. // Only output the http headers
  87. $response = PMA_Response::getInstance();
  88. $response->getHeader()->sendHttpHeaders();
  89. // [MIME]
  90. if (isset($ct) && ! empty($ct)) {
  91. $mime_type = $ct;
  92. } else {
  93. $mime_type = (!empty($mime_map[$transform_key]['mimetype'])
  94. ? str_replace('_', '/', $mime_map[$transform_key]['mimetype'])
  95. : $default_ct)
  96. . (isset($mime_options['charset']) ? $mime_options['charset'] : '');
  97. }
  98. PMA_downloadHeader($cn, $mime_type);
  99. if (! isset($resize)) {
  100. if (stripos($mime_type, 'html') === false) {
  101. echo $row[$transform_key];
  102. } else {
  103. echo htmlspecialchars($row[$transform_key]);
  104. }
  105. } else {
  106. // if image_*__inline.inc.php finds that we can resize,
  107. // it sets $resize to jpeg or png
  108. $srcImage = imagecreatefromstring($row[$transform_key]);
  109. $srcWidth = ImageSX($srcImage);
  110. $srcHeight = ImageSY($srcImage);
  111. // Check to see if the width > height or if width < height
  112. // if so adjust accordingly to make sure the image
  113. // stays smaller then the $newWidth and $newHeight
  114. $ratioWidth = $srcWidth/$newWidth;
  115. $ratioHeight = $srcHeight/$newHeight;
  116. if ($ratioWidth < $ratioHeight) {
  117. $destWidth = $srcWidth/$ratioHeight;
  118. $destHeight = $newHeight;
  119. } else {
  120. $destWidth = $newWidth;
  121. $destHeight = $srcHeight/$ratioWidth;
  122. }
  123. if ($resize) {
  124. $destImage = ImageCreateTrueColor($destWidth, $destHeight);
  125. }
  126. // ImageCopyResized($destImage, $srcImage, 0, 0, 0, 0,
  127. // $destWidth, $destHeight, $srcWidth, $srcHeight);
  128. // better quality but slower:
  129. ImageCopyResampled(
  130. $destImage, $srcImage, 0, 0, 0, 0, $destWidth,
  131. $destHeight, $srcWidth, $srcHeight
  132. );
  133. if ($resize == 'jpeg') {
  134. ImageJPEG($destImage, null, 75);
  135. }
  136. if ($resize == 'png') {
  137. ImagePNG($destImage);
  138. }
  139. ImageDestroy($srcImage);
  140. ImageDestroy($destImage);
  141. }
  142. ?>