DB_Image.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <?php
  2. class DB_Image {
  3. public static function conf_get( $key ) {
  4. static $conf;
  5. if (empty($conf)) {
  6. $conf['table_name'] = 'CRM_IMAGE';
  7. $conf['max_size'] = 1024 * 1024 * 16;// MAX for MEDIUMBLOB field
  8. $conf['allowed_types'] = array();
  9. $conf['allowed_types'] []= 'image/png';
  10. $conf['allowed_types'] []= 'image/jpeg';
  11. $conf['allowed_types'] []= 'image/gif';
  12. $conf['remote_tables'] = array();
  13. $conf['remote_tables'] []= 'CRM_LISTA_ZASOBOW';
  14. $conf['remote_tables'] []= 'CRM_PROCES';
  15. $conf['remote_tables'] []= 'CRM_WSKAZNIK';
  16. $conf['desc_options'] = array();
  17. $conf['desc_options']['CRM_LISTA_ZASOBOW'] = array();
  18. $conf['desc_options']['CRM_PROCES'] = array();
  19. $conf['desc_options']['CRM_WSKAZNIK'] = array();
  20. $conf['desc_options_conf'] = array();
  21. $conf['desc_options_conf']['CRM_LISTA_ZASOBOW'] = array();
  22. $conf['desc_options_conf']['CRM_PROCES'] = array();
  23. $conf['desc_options_conf']['CRM_WSKAZNIK'] = array();
  24. $options = array();
  25. $options['screenshot'] = "screenshot (800x600)";
  26. $options['icon'] = "icon (16x16)";
  27. $conf['desc_options']['CRM_LISTA_ZASOBOW'] = $options;
  28. $conf['desc_options']['CRM_PROCES'] = $options;
  29. $conf['desc_options']['CRM_WSKAZNIK'] = $options;
  30. $desc_options_conf = array();
  31. $desc_options_conf['screenshot']['max_width'] = 800;
  32. $desc_options_conf['screenshot']['max_height'] = 600;
  33. $desc_options_conf['icon']['max_width'] = 16;
  34. $desc_options_conf['icon']['max_height'] = 16;
  35. $conf['desc_options_conf']['CRM_LISTA_ZASOBOW'] = $desc_options_conf;
  36. $conf['desc_options_conf']['CRM_PROCES'] = $desc_options_conf;
  37. $conf['desc_options_conf']['CRM_WSKAZNIK'] = $desc_options_conf;
  38. }
  39. if (array_key_exists($key, $conf)) {
  40. return $conf[ $key ];
  41. }
  42. return null;
  43. }
  44. public static function conf_get_table_name() {
  45. return self::conf_get('table_name');
  46. }
  47. /*
  48. * Get options for table.
  49. */
  50. public static function conf_get_options( $table ) {
  51. $ret = array();
  52. $arr = self::conf_get('desc_options');
  53. if (array_key_exists($table, $arr)) {
  54. return $arr[$table];
  55. }
  56. return $ret;
  57. }
  58. /*
  59. * Get config for all options.
  60. */
  61. public static function conf_get_options_conf( $table ) {
  62. $ret = array();
  63. $arr = self::conf_get('desc_options_conf');
  64. if (array_key_exists($table, $arr)) {
  65. return $arr[$table];
  66. }
  67. return $ret;
  68. }
  69. /*
  70. * Get config for selected option.
  71. */
  72. public static function conf_get_option_conf( $table, $key ) {
  73. $ret = array();
  74. $arr = self::conf_get('desc_options_conf');
  75. if (array_key_exists($table, $arr)) {
  76. if (array_key_exists($key, $arr[$table])) {
  77. return $arr[$table][$key];
  78. }
  79. }
  80. return $ret;
  81. }
  82. public static function is_allowed_type( $type ) {
  83. $allowed_types = self::conf_get('allowed_types');
  84. if (in_array($type, $allowed_types)) {
  85. return true;
  86. }
  87. return false;
  88. }
  89. public static function read_image_data( $file, $mime_type ){
  90. $im = null;
  91. if ($mime_type == 'image/jpeg') {
  92. $im = imagecreatefromjpeg( $file );
  93. } else if ($mime_type == 'image/png') {
  94. $im = imagecreatefrompng( $file );
  95. } else if ($mime_type == 'image/gif') {
  96. $im = imagecreatefromgif( $file );
  97. }
  98. return $im;
  99. }
  100. public static function get_images($remote_table, $remote_id) {
  101. $db = DB::getDB();
  102. $images = array();
  103. $sql = "select
  104. t.`ID`, t.`NAME`, t.`SIZE`, t.`TYPE`, t.`WIDTH`, t.`HEIGHT`
  105. , t.`DEST`, t.`A_CREATE_DATE`
  106. , t.`REMOTE_TABLE`, t.`REMOTE_ID`
  107. , UNIX_TIMESTAMP(`A_CREATE_DATE`) as A_CREATE_DATE_TS
  108. from `".self::conf_get_table_name()."` as t
  109. where
  110. t.`REMOTE_TABLE`='".$remote_table."'
  111. and t.`REMOTE_ID`='".$remote_id."'
  112. ";
  113. $res = $db->query($sql);
  114. while ($r = $db->fetch( $res )) {
  115. $images []= $r;
  116. }
  117. return $images;
  118. }
  119. /**
  120. * Try to upload image to DB.
  121. * @returns Array of errors
  122. */
  123. public static function upload_image($remote_table, $remote_id, $req_file_data) {
  124. $errors = array();
  125. $maxsize = self::conf_get('max_size');
  126. //echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">$_FILES = ';print_r($_FILES);echo'</pre>';
  127. if (is_uploaded_file($req_file_data['tmp_name'])) {
  128. // check the file is less than the maximum file size
  129. if ($req_file_data['size'] < $maxsize) {
  130. // prepare the image for insertion
  131. //$imgData = mysql_real_escape_string(file_get_contents($req_file_data['tmp_name']), $conn);// nie dziala mysql_real_escape_string
  132. $fp = fopen($req_file_data['tmp_name'], 'r');
  133. $imgData = fread($fp, filesize($req_file_data['tmp_name']));
  134. $imgData = addslashes($imgData);
  135. fclose($fp);
  136. // get the image info..
  137. $size = getimagesize($req_file_data['tmp_name']);
  138. $obj = array();
  139. $obj['DEST'] = (isset($_REQUEST['DEST']))? $_REQUEST['DEST'] : '';
  140. $obj['TYPE'] = $size['mime'];
  141. $obj['IMAGE'] = $imgData;
  142. $obj['WIDTH'] = $size[0];
  143. $obj['HEIGHT'] = $size[1];
  144. $obj['NAME'] = $req_file_data['name'];
  145. $obj['SIZE'] = $req_file_data['size'];
  146. $obj['REMOTE_TABLE'] = $remote_table;
  147. $obj['REMOTE_ID'] = $remote_id;
  148. // check if type is allowed to upload
  149. if (!DB_Image::is_allowed_type($obj['TYPE'])) {
  150. $errors []= 'File type "'.$obj['TYPE'].'" is not allowed.';
  151. return $errors;
  152. }
  153. // convert image if selected option
  154. if ($obj['DEST']) {
  155. $options_conf = DB_Image::conf_get_option_conf( $remote_table, $obj['DEST'] );
  156. if (!empty($options_conf)) {
  157. $max_width = 0;
  158. $max_height = 0;
  159. if (isset($options_conf['max_height']) && $obj['HEIGHT'] > $options_conf['max_height']) {
  160. $max_height = $options_conf['max_height'];
  161. }
  162. if (isset($options_conf['max_width']) && $obj['WIDTH'] > $options_conf['max_width']) {
  163. $max_width = $options_conf['max_width'];
  164. }
  165. if ($max_height || $max_width) {
  166. //$img = DB_Image::resize_image($req_file_data['tmp_name'], $max_width, $max_height);
  167. //open image, from file to data
  168. // read data
  169. $im = DB_Image::read_image_data($req_file_data['tmp_name'], $obj['TYPE']);
  170. // resize image
  171. $im = DB_Image::resize_image_from_data($im, $max_width, $max_height);
  172. ob_start();
  173. imagejpeg($im);
  174. $obj['IMAGE'] = ob_get_clean();
  175. $obj['SIZE'] = strlen($obj['IMAGE']);
  176. $obj['IMAGE'] = addslashes($obj['IMAGE']);
  177. $obj['WIDTH'] = imagesx($im);
  178. $obj['HEIGHT'] = imagesy($im);
  179. }
  180. }
  181. }
  182. //echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">size = ';print_r($size);echo'</pre>';
  183. $sql_arr = array();
  184. foreach ($obj as $key => $val) {
  185. $sql_arr["`".$key."`"] = "'".$val."'";
  186. }//end foreach
  187. $sql = "insert into `".self::conf_get_table_name()."` (".implode(",", array_keys($sql_arr)).") values (".implode(",", array_values($sql_arr)).") ; ";
  188. // insert the image
  189. //echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">sql = ';print_r($sql);echo'</pre>';
  190. $db = DB::getDB();
  191. if (!$db->query($sql)) {
  192. $errors []= 'Unable to upload file - sql error';
  193. //echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">mysql err: '.mysql_errno($conn);var_dump(mysql_error($conn));echo'</pre>';
  194. //echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">sql: ';print_r($sql);echo'</pre>';
  195. }
  196. self::update_remote_table_stat( $remote_table, $remote_id );
  197. }
  198. }
  199. else {
  200. // if the file is not less than the maximum allowed, print an error
  201. $errors []= 'File exceeds the Maximum File limit';
  202. }
  203. return $errors;
  204. }
  205. public static function delete_image($id, $remote_table, $remote_id) {
  206. $db = DB::getDB();
  207. $sql = "delete from `".self::conf_get_table_name()."`
  208. where
  209. `ID`='".$id."'
  210. and `REMOTE_ID`='".$remote_id."'
  211. and `REMOTE_TABLE`='".$remote_table."'
  212. limit 1
  213. ";
  214. //echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">sql delete = ';print_r($sql);echo'</pre>';
  215. $db->query( $sql );
  216. $affected = $db->affected_rows();
  217. self::update_remote_table_stat( $remote_table, $remote_id );
  218. return $affected;
  219. }
  220. /**
  221. * Updates field ITEM_IMAGE_REMOTE in remote table.
  222. * TODO: check if field exists!
  223. */
  224. public static function update_remote_table_stat($remote_table, $remote_id) {
  225. // update stat `ITEM_IMAGE_REMOTE`
  226. $sql = "update `".$remote_table."`
  227. set `A_HAS_IMAGE`=(
  228. select count(1)
  229. from `".self::conf_get('table_name')."`
  230. where
  231. `REMOTE_ID`='".$remote_id."'
  232. and `REMOTE_TABLE`='".$remote_table."'
  233. )
  234. where `ID`='".$remote_id."'
  235. limit 1;
  236. ";
  237. $db = DB::getDB();
  238. $res = $db->query( $sql );
  239. $affected = $db->affected_rows();
  240. }
  241. public static function resize_image($file, $w, $h, $crop=FALSE) {
  242. // TODO: image type
  243. $im = imagecreatefromjpeg($file);
  244. return self::resize_image_from_data($im, $w, $h, $crop);
  245. }
  246. public static function resize_image_from_data($im, $w, $h, $crop=FALSE) {
  247. if ($w == 0) $w = $h;
  248. if ($h == 0) $h = $w;
  249. $width = imagesx($im);
  250. $height = imagesy($im);
  251. if ($width < $w && $height < $h) {
  252. return $im;
  253. }
  254. $r = $width / $height;
  255. if ($crop) {
  256. if ($width > $height) {
  257. $width = ceil($width-($width*($r-$w/$h)));
  258. } else {
  259. $height = ceil($height-($height*($r-$w/$h)));
  260. }
  261. $newwidth = $w;
  262. $newheight = $h;
  263. } else {
  264. if ($w/$h > $r) {
  265. $newwidth = $h*$r;
  266. $newheight = $h;
  267. } else {
  268. $newheight = $w/$r;
  269. $newwidth = $w;
  270. }
  271. }
  272. $dst = imagecreatetruecolor($newwidth, $newheight);
  273. imagecopyresampled($dst, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  274. return $dst;
  275. }
  276. public static function show_size( $size ) {
  277. $ret_unit = 'B';
  278. if ($size > 1024 * 1024) {
  279. $size = $size / (1024 * 1024);
  280. $ret_unit = 'MB';
  281. } else if ($size > 1024) {
  282. $size = $size / 1024;
  283. $ret_unit = 'KB';
  284. }
  285. return number_format($size, 2, '.', ' ').''.$ret_unit;
  286. }
  287. }