DB_Image.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. <?php
  2. class DB_Image {
  3. public static function calculateThumbImageSize($width, $height, $maxWidth, $maxHeight) {
  4. $width = intval($width);
  5. $height = intval($height);
  6. $maxWidth = intval($maxWidth);
  7. $maxHeight = intval($maxHeight);
  8. if ($width <= 0 || $height <= 0 || $maxWidth <= 0 || $maxHeight <= 0) {
  9. throw new Exception("Wrong params");
  10. }
  11. list($newWidth, $newHeight) = array($width, $height);
  12. if ($width >= $maxWidth || $height >= $maxWidth) {
  13. $ratioWidth = $maxWidth / $width;
  14. $ratioHeight = $maxHeight / $height;
  15. if ($ratioWidth > $ratioHeight) {
  16. $ratio = $ratioHeight;
  17. } else {
  18. $ratio = $ratioWidth;
  19. }
  20. $newWidth = intval($width * $ratio);
  21. $newHeight = intval($height * $ratio);
  22. }
  23. return array($newWidth, $newHeight);
  24. }
  25. public static function resizeImageFromBlob($imageBlob, $type, $maxWidth, $maxHeight) {
  26. $resizedImageBlob = null;
  27. list($w, $h) = self::getImageGeometryFromBlob($imageBlob);
  28. list($newWidth, $newHeight) = self::calculateThumbImageSize($w, $h, $maxWidth, $maxHeight);
  29. if ($newW == $w && $newH == $h) {
  30. return $imageBlob;
  31. }
  32. if (class_exists('Imagick')) {
  33. $im = new Imagick();
  34. $im->readImageBlob($imageBlob);
  35. $im->thumbnailImage($newWidth, $newHeight, true);
  36. $resizedImageBlob = $im->getImageBlob();
  37. } else {
  38. $im = imagecreatefromstring($imageBlob);
  39. $image_resized = self::resize_image_from_data($im, $newWidth, $newHeight);
  40. ob_start();
  41. if ($type == 'image/png') {
  42. imagepng($image_resized);
  43. } else if ($type == 'image/jpeg' || $type == 'image/jpg') {
  44. imagejpeg($image_resized);
  45. } else if ($type == 'image/gif') {
  46. imagegif($image_resized);
  47. }
  48. $resizedImageBlob = ob_get_contents();
  49. ob_clean();
  50. }
  51. return $resizedImageBlob;
  52. }
  53. public static function getImageGeometryFromBlob($imageBlob) {
  54. if (class_exists('Imagick')) {
  55. $im = new Imagick();
  56. $im->readImageBlob($imageBlob);
  57. $geometry = $im->getImageGeometry();
  58. $w = $geometry['width'];
  59. $h = $geometry['height'];
  60. } else {
  61. $im = imagecreatefromstring($imageBlob);
  62. $w = imagesx($im);
  63. $h = imagesy($im);
  64. }
  65. return array($w, $h);
  66. }
  67. public static function getImage($remote_table, $remote_id, $number = 0) {
  68. $db = DB::getDB();
  69. $sql = "select `ID`, `TYPE`, `SIZE`, `IMAGE`, `WIDTH`, `HEIGHT`
  70. , UNIX_TIMESTAMP(`A_CREATE_DATE`) as A_CREATE_DATE_TS
  71. from `".self::conf_get_table_name()."`
  72. where
  73. `REMOTE_ID`='{$remote_id}'
  74. and `REMOTE_TABLE`='{$remote_table}'
  75. order by `ID` asc
  76. limit 1 offset {$number}
  77. ";
  78. $res = $db->query($sql);
  79. if (!$db->num_rows($res)) {
  80. return null;
  81. }
  82. return $db->fetch($res);
  83. }
  84. public static function getImageById($imageId) {
  85. $db = DB::getDB();
  86. $sql = "select `ID`, `TYPE`, `SIZE`, `IMAGE`, `WIDTH`, `HEIGHT`
  87. , UNIX_TIMESTAMP(`A_CREATE_DATE`) as A_CREATE_DATE_TS
  88. from `".self::conf_get_table_name()."`
  89. where
  90. `ID`='{$imageId}'
  91. ";
  92. $res = $db->query($sql);
  93. return $db->fetch($res);
  94. }
  95. public static function tableIsAllowed($remote_table) {
  96. $remote_tables = self::conf_get('remote_tables');
  97. return in_array($remote_table, $remote_tables);
  98. }
  99. public static function conf_get($key) {
  100. static $conf;
  101. if (empty($conf)) {
  102. $conf['table_name'] = 'CRM_IMAGE';
  103. $conf['max_size'] = 1024 * 1024 * 16;// MAX for MEDIUMBLOB field
  104. $conf['allowed_types'] = array();
  105. $conf['allowed_types'][] = 'image/png';
  106. $conf['allowed_types'][] = 'image/jpeg';
  107. $conf['allowed_types'][] = 'image/gif';
  108. $conf['remote_tables'] = array();
  109. $conf['remote_tables'][] = 'CRM_LISTA_ZASOBOW';
  110. $conf['remote_tables'][] = 'CRM_PROCES';
  111. $conf['remote_tables'][] = 'CRM_WSKAZNIK';
  112. $conf['desc_options'] = array();
  113. $conf['desc_options']['CRM_LISTA_ZASOBOW'] = array();
  114. $conf['desc_options']['CRM_PROCES'] = array();
  115. $conf['desc_options']['CRM_WSKAZNIK'] = array();
  116. $conf['desc_options_conf'] = array();
  117. $conf['desc_options_conf']['CRM_LISTA_ZASOBOW'] = array();
  118. $conf['desc_options_conf']['CRM_PROCES'] = array();
  119. $conf['desc_options_conf']['CRM_WSKAZNIK'] = array();
  120. $options = array();
  121. $options['screenshot'] = "screenshot (800x600)";
  122. $options['icon'] = "icon (16x16)";
  123. $conf['desc_options']['CRM_LISTA_ZASOBOW'] = $options;
  124. $conf['desc_options']['CRM_PROCES'] = $options;
  125. $conf['desc_options']['CRM_WSKAZNIK'] = $options;
  126. $desc_options_conf = array();
  127. $desc_options_conf['screenshot']['max_width'] = 800;
  128. $desc_options_conf['screenshot']['max_height'] = 600;
  129. $desc_options_conf['icon']['max_width'] = 16;
  130. $desc_options_conf['icon']['max_height'] = 16;
  131. $conf['desc_options_conf']['CRM_LISTA_ZASOBOW'] = $desc_options_conf;
  132. $conf['desc_options_conf']['CRM_PROCES'] = $desc_options_conf;
  133. $conf['desc_options_conf']['CRM_WSKAZNIK'] = $desc_options_conf;
  134. }
  135. if (array_key_exists($key, $conf)) {
  136. return $conf[$key];
  137. }
  138. return null;
  139. }
  140. public static function conf_get_table_name() {// TODO: RM Legacy
  141. return self::getStorageTableName();
  142. }
  143. public static function getStorageTableName() {
  144. return self::conf_get('table_name');
  145. }
  146. public static function conf_get_options($table) {// TODO: RM Legacy
  147. return self::getTableConfig($table);
  148. }
  149. public static function getTableConfig($table) {
  150. $ret = array();
  151. $arr = self::conf_get('desc_options');
  152. if (array_key_exists($table, $arr)) {
  153. return $arr[$table];
  154. }
  155. return $ret;
  156. }
  157. public static function conf_get_options_conf($table) {// TODO: RM Not used ?
  158. $ret = array();
  159. $arr = self::conf_get('desc_options_conf');
  160. if (array_key_exists($table, $arr)) {
  161. return $arr[$table];
  162. }
  163. return $ret;
  164. }
  165. public static function conf_get_option_conf($table, $key) {// TODO: used only in upload_image
  166. $ret = array();
  167. $arr = self::conf_get('desc_options_conf');
  168. if (array_key_exists($table, $arr)) {
  169. if (array_key_exists($key, $arr[$table])) {
  170. return $arr[$table][$key];
  171. }
  172. }
  173. return $ret;
  174. }
  175. public static function is_allowed_type($type) {// TODO: used only in upload_image
  176. $allowed_types = self::conf_get('allowed_types');
  177. if (in_array($type, $allowed_types)) {
  178. return true;
  179. }
  180. return false;
  181. }
  182. public static function readImageBlobFromFile($file, $mimeType) {
  183. $imBlob = null;
  184. if (class_exists('Imagick')) {
  185. $im = new Imagick($file);
  186. $imBlob = $im->getImageBlob();
  187. } else {
  188. if ($mimeType == 'image/jpeg') {
  189. $im = imagecreatefromjpeg($file);
  190. } else if ($mimeType == 'image/png') {
  191. $im = imagecreatefrompng($file);
  192. } else if ($mimeType == 'image/gif') {
  193. $im = imagecreatefromgif($file);
  194. }
  195. ob_start();
  196. if ($mimeType == 'image/png') {
  197. imagepng($im);
  198. } else if ($mimeType == 'image/jpeg' || $mimeType == 'image/jpg') {
  199. imagejpeg($im);
  200. } else if ($mimeType == 'image/gif') {
  201. imagegif($im);
  202. }
  203. $imBlob = ob_get_contents();
  204. ob_clean();
  205. }
  206. return $imBlob;
  207. }
  208. public static function read_image_data($file, $mime_type) {// TODO: used only in upload_image
  209. $im = null;
  210. if ($mime_type == 'image/jpeg') {
  211. $im = imagecreatefromjpeg($file);
  212. } else if ($mime_type == 'image/png') {
  213. $im = imagecreatefrompng($file);
  214. } else if ($mime_type == 'image/gif') {
  215. $im = imagecreatefromgif($file);
  216. }
  217. return $im;
  218. }
  219. public static function get_images($remote_table, $remote_id) {// TODO: RM Legacy
  220. return self::getImages($remote_table, $remote_id);
  221. }
  222. public static function getImages($remote_table, $remote_id) {
  223. $db = DB::getDB();
  224. $images = array();
  225. $sql = "select
  226. t.`ID`, t.`NAME`, t.`SIZE`, t.`TYPE`, t.`WIDTH`, t.`HEIGHT`
  227. , t.`DEST`, t.`A_CREATE_DATE`
  228. , t.`REMOTE_TABLE`, t.`REMOTE_ID`
  229. , UNIX_TIMESTAMP(`A_CREATE_DATE`) as A_CREATE_DATE_TS
  230. from `".self::conf_get_table_name()."` as t
  231. where
  232. t.`REMOTE_TABLE`='{$remote_table}'
  233. and t.`REMOTE_ID`='{$remote_id}'
  234. ";
  235. $res = $db->query($sql);
  236. while ($r = $db->fetch($res)) {
  237. $images[] = $r;
  238. }
  239. return $images;
  240. }
  241. /**
  242. * Try to upload image to DB.
  243. * @returns Array of errors
  244. */
  245. public static function upload_image($remote_table, $remote_id, $req_file_data) {// TODO: RM -> moved to uploadImage
  246. return self::uploadImage($remote_table, $remote_id, $req_file_data);
  247. }
  248. public static function uploadImage($remote_table, $remote_id, $req_file_data) {
  249. $errors = array();
  250. $maxsize = self::conf_get('max_size');
  251. if (is_uploaded_file($req_file_data['tmp_name'])) {
  252. if ($req_file_data['size'] < $maxsize) {
  253. $fp = fopen($req_file_data['tmp_name'], 'r');
  254. $imgData = fread($fp, filesize($req_file_data['tmp_name']));
  255. $imgData = addslashes($imgData);
  256. fclose($fp);
  257. $size = getimagesize($req_file_data['tmp_name']);
  258. $sqlObj = array();
  259. $sqlObj['DEST'] = (isset($_REQUEST['DEST']))? $_REQUEST['DEST'] : '';
  260. $sqlObj['TYPE'] = $size['mime'];
  261. $sqlObj['IMAGE'] = $imgData;
  262. $sqlObj['WIDTH'] = $size[0];
  263. $sqlObj['HEIGHT'] = $size[1];
  264. $sqlObj['NAME'] = $req_file_data['name'];
  265. $sqlObj['SIZE'] = $req_file_data['size'];
  266. $sqlObj['REMOTE_TABLE'] = $remote_table;
  267. $sqlObj['REMOTE_ID'] = $remote_id;
  268. // check if type is allowed to upload
  269. if (!self::is_allowed_type($sqlObj['TYPE'])) {
  270. $errors[] = 'File type "'.$sqlObj['TYPE'].'" is not allowed.';
  271. return $errors;
  272. }
  273. // convert image if selected option
  274. if ($sqlObj['DEST']) {
  275. $options_conf = self::conf_get_option_conf($remote_table, $sqlObj['DEST']);
  276. if (!empty($options_conf)) {
  277. $max_width = V::get('max_width', 0, $options_conf, 'int');
  278. $max_height = V::get('max_height', 0, $options_conf, 'int');
  279. if ($max_height > 0 && $max_width > 0) {
  280. $imBlob = self::readImageBlobFromFile($req_file_data['tmp_name'], $sqlObj['TYPE']);
  281. $sqlObj['IMAGE'] = self::resizeImageFromBlob($imBlob, $sqlObj['TYPE'], $max_width, $max_height);
  282. list($sqlObj['WIDTH'], $sqlObj['HEIGHT']) = self::getImageGeometryFromBlob($sqlObj['IMAGE']);
  283. $sqlObj['SIZE'] = strlen($sqlObj['IMAGE']);
  284. $sqlObj['IMAGE'] = addslashes($sqlObj['IMAGE']);
  285. }
  286. }
  287. }
  288. $sql_arr = array();
  289. foreach ($sqlObj as $key => $val) {
  290. $sql_arr["`{$key}`"] = "'{$val}'";
  291. }
  292. $sql = "insert into `".self::conf_get_table_name()."` (".implode(",", array_keys($sql_arr)).") values (".implode(",", array_values($sql_arr)).") ; ";
  293. $db = DB::getDB();
  294. if (!$db->query($sql)) {
  295. $errors[] = 'Unable to upload file - sql error';
  296. }
  297. self::update_remote_table_stat($remote_table, $remote_id);
  298. }
  299. }
  300. else {
  301. // if the file is not less than the maximum allowed, print an error
  302. $errors[] = 'File exceeds the Maximum File limit';
  303. }
  304. return $errors;
  305. }
  306. public static function delete_image($id, $remote_table, $remote_id) {// TODO: RM Legacy
  307. return self::deleteImage($id, $remote_table, $remote_id);
  308. }
  309. public static function deleteImage($id, $remote_table, $remote_id) {
  310. $db = DB::getDB();
  311. $sql = "delete from `".self::conf_get_table_name()."`
  312. where
  313. `ID`='{$id}'
  314. and `REMOTE_ID`='{$remote_id}'
  315. and `REMOTE_TABLE`='{$remote_table}'
  316. limit 1
  317. ";
  318. //echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">sql delete = ';print_r($sql);echo'</pre>';
  319. $db->query($sql);
  320. $affected = $db->affected_rows();
  321. self::update_remote_table_stat($remote_table, $remote_id);
  322. return $affected;
  323. }
  324. /**
  325. * Updates field ITEM_IMAGE_REMOTE in remote table.
  326. * TODO: check if field exists!
  327. */
  328. public static function update_remote_table_stat($remote_table, $remote_id) {// TODO: used only in this file
  329. // update stat `ITEM_IMAGE_REMOTE`
  330. $sql = "update `{$remote_table}`
  331. set `A_HAS_IMAGE`=(
  332. select count(1)
  333. from `".self::conf_get('table_name')."`
  334. where
  335. `REMOTE_ID`='{$remote_id}'
  336. and `REMOTE_TABLE`='{$remote_table}'
  337. )
  338. where `ID`='{$remote_id}'
  339. limit 1;
  340. ";
  341. $db = DB::getDB();
  342. $res = $db->query($sql);
  343. $affected = $db->affected_rows();
  344. }
  345. public static function resize_image($file, $w, $h, $crop = FALSE) {
  346. // TODO: image type
  347. $im = imagecreatefromjpeg($file);
  348. return self::resize_image_from_data($im, $w, $h, $crop);
  349. }
  350. public static function resize_image_from_data($im, $w, $h, $crop = FALSE) {
  351. if ($w == 0) $w = $h;
  352. if ($h == 0) $h = $w;
  353. $width = imagesx($im);
  354. $height = imagesy($im);
  355. if ($width < $w && $height < $h) {
  356. return $im;
  357. }
  358. $r = $width / $height;
  359. if ($crop) {
  360. if ($width > $height) {
  361. $width = ceil($width-($width*($r-$w/$h)));
  362. } else {
  363. $height = ceil($height-($height*($r-$w/$h)));
  364. }
  365. $newwidth = $w;
  366. $newheight = $h;
  367. } else {
  368. if ($w/$h > $r) {
  369. $newwidth = $h*$r;
  370. $newheight = $h;
  371. } else {
  372. $newheight = $w/$r;
  373. $newwidth = $w;
  374. }
  375. }
  376. $dst = imagecreatetruecolor($newwidth, $newheight);
  377. imagecopyresampled($dst, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  378. return $dst;
  379. }
  380. public static function show_size($size) {// TODO: RM -> moved to showSize
  381. return self::showSize($size);
  382. }
  383. public static function showSize($size) {
  384. $ret_unit = 'B';
  385. if ($size > 1024 * 1024) {
  386. $size = $size / (1024 * 1024);
  387. $ret_unit = 'MB';
  388. } else if ($size > 1024) {
  389. $size = $size / 1024;
  390. $ret_unit = 'KB';
  391. }
  392. return number_format($size, 2, '.', ' ').''.$ret_unit;
  393. }
  394. }