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