transformation_wrapper.php 3.9 KB

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