| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450 |
- <?php
- class DB_Image {
- public static function calculateThumbImageSize($width, $height, $maxWidth, $maxHeight) {
- $width = intval($width);
- $height = intval($height);
- $maxWidth = intval($maxWidth);
- $maxHeight = intval($maxHeight);
- if ($width <= 0 || $height <= 0 || $maxWidth <= 0 || $maxHeight <= 0) {
- throw new Exception("Wrong params");
- }
- list($newWidth, $newHeight) = array($width, $height);
- if ($width >= $maxWidth || $height >= $maxWidth) {
- $ratioWidth = $maxWidth / $width;
- $ratioHeight = $maxHeight / $height;
- if ($ratioWidth > $ratioHeight) {
- $ratio = $ratioHeight;
- } else {
- $ratio = $ratioWidth;
- }
- $newWidth = intval($width * $ratio);
- $newHeight = intval($height * $ratio);
- }
- return array($newWidth, $newHeight);
- }
- public static function resizeImageFromBlob($imageBlob, $type, $maxWidth, $maxHeight) {
- $resizedImageBlob = null;
- list($w, $h) = self::getImageGeometryFromBlob($imageBlob);
- list($newWidth, $newHeight) = self::calculateThumbImageSize($w, $h, $maxWidth, $maxHeight);
- if ($newW == $w && $newH == $h) {
- return $imageBlob;
- }
- if (class_exists('Imagick')) {
- $im = new Imagick();
- $im->readImageBlob($imageBlob);
- $im->thumbnailImage($newWidth, $newHeight, true);
- $resizedImageBlob = $im->getImageBlob();
- } else {
- $im = imagecreatefromstring($imageBlob);
- $image_resized = self::resize_image_from_data($im, $newWidth, $newHeight);
- ob_start();
- if ($type == 'image/png') {
- imagepng($image_resized);
- } else if ($type == 'image/jpeg' || $type == 'image/jpg') {
- imagejpeg($image_resized);
- } else if ($type == 'image/gif') {
- imagegif($image_resized);
- }
- $resizedImageBlob = ob_get_contents();
- ob_clean();
- }
- return $resizedImageBlob;
- }
- public static function getImageGeometryFromBlob($imageBlob) {
- if (class_exists('Imagick')) {
- $im = new Imagick();
- $im->readImageBlob($imageBlob);
- $geometry = $im->getImageGeometry();
- $w = $geometry['width'];
- $h = $geometry['height'];
- } else {
- $im = imagecreatefromstring($imageBlob);
- $w = imagesx($im);
- $h = imagesy($im);
- }
- return array($w, $h);
- }
- public static function getImage($remote_table, $remote_id, $number = 0) {
- $db = DB::getDB();
- $sql = "select `ID`, `TYPE`, `SIZE`, `IMAGE`, `WIDTH`, `HEIGHT`
- , UNIX_TIMESTAMP(`A_CREATE_DATE`) as A_CREATE_DATE_TS
- from `".self::conf_get_table_name()."`
- where
- `REMOTE_ID`='{$remote_id}'
- and `REMOTE_TABLE`='{$remote_table}'
- order by `ID` asc
- limit 1 offset {$number}
- ";
- $res = $db->query($sql);
- if (!$db->num_rows($res)) {
- return null;
- }
- return $db->fetch($res);
- }
- public static function getImageById($imageId) {
- $db = DB::getDB();
- $sql = "select `ID`, `TYPE`, `SIZE`, `IMAGE`, `WIDTH`, `HEIGHT`
- , UNIX_TIMESTAMP(`A_CREATE_DATE`) as A_CREATE_DATE_TS
- from `".self::conf_get_table_name()."`
- where
- `ID`='{$imageId}'
- ";
- $res = $db->query($sql);
- return $db->fetch($res);
- }
- public static function tableIsAllowed($remote_table) {
- $remote_tables = self::conf_get('remote_tables');
- return in_array($remote_table, $remote_tables);
- }
- public static function conf_get($key) {
- static $conf;
- if (empty($conf)) {
- $conf['table_name'] = 'CRM_IMAGE';
- $conf['max_size'] = 1024 * 1024 * 16;// MAX for MEDIUMBLOB field
- $conf['allowed_types'] = array();
- $conf['allowed_types'][] = 'image/png';
- $conf['allowed_types'][] = 'image/jpeg';
- $conf['allowed_types'][] = 'image/gif';
- $conf['remote_tables'] = array();
- $conf['remote_tables'][] = 'CRM_LISTA_ZASOBOW';
- $conf['remote_tables'][] = 'CRM_PROCES';
- $conf['remote_tables'][] = 'CRM_WSKAZNIK';
- $conf['desc_options'] = array();
- $conf['desc_options']['CRM_LISTA_ZASOBOW'] = array();
- $conf['desc_options']['CRM_PROCES'] = array();
- $conf['desc_options']['CRM_WSKAZNIK'] = array();
- $conf['desc_options_conf'] = array();
- $conf['desc_options_conf']['CRM_LISTA_ZASOBOW'] = array();
- $conf['desc_options_conf']['CRM_PROCES'] = array();
- $conf['desc_options_conf']['CRM_WSKAZNIK'] = array();
- $options = array();
- $options['screenshot'] = "screenshot (800x600)";
- $options['icon'] = "icon (16x16)";
- $conf['desc_options']['CRM_LISTA_ZASOBOW'] = $options;
- $conf['desc_options']['CRM_PROCES'] = $options;
- $conf['desc_options']['CRM_WSKAZNIK'] = $options;
- $desc_options_conf = array();
- $desc_options_conf['screenshot']['max_width'] = 800;
- $desc_options_conf['screenshot']['max_height'] = 600;
- $desc_options_conf['icon']['max_width'] = 16;
- $desc_options_conf['icon']['max_height'] = 16;
- $conf['desc_options_conf']['CRM_LISTA_ZASOBOW'] = $desc_options_conf;
- $conf['desc_options_conf']['CRM_PROCES'] = $desc_options_conf;
- $conf['desc_options_conf']['CRM_WSKAZNIK'] = $desc_options_conf;
- }
- if (array_key_exists($key, $conf)) {
- return $conf[$key];
- }
- return null;
- }
- public static function conf_get_table_name() {// TODO: RM Legacy
- return self::getStorageTableName();
- }
- public static function getStorageTableName() {
- return self::conf_get('table_name');
- }
- public static function conf_get_options($table) {// TODO: RM Legacy
- return self::getTableConfig($table);
- }
- public static function getTableConfig($table) {
- $ret = array();
- $arr = self::conf_get('desc_options');
- if (array_key_exists($table, $arr)) {
- return $arr[$table];
- }
- return $ret;
- }
- public static function conf_get_options_conf($table) {// TODO: RM Not used ?
- $ret = array();
- $arr = self::conf_get('desc_options_conf');
- if (array_key_exists($table, $arr)) {
- return $arr[$table];
- }
- return $ret;
- }
- public static function conf_get_option_conf($table, $key) {// TODO: used only in upload_image
- $ret = array();
- $arr = self::conf_get('desc_options_conf');
- if (array_key_exists($table, $arr)) {
- if (array_key_exists($key, $arr[$table])) {
- return $arr[$table][$key];
- }
- }
- return $ret;
- }
- public static function is_allowed_type($type) {// TODO: used only in upload_image
- $allowed_types = self::conf_get('allowed_types');
- if (in_array($type, $allowed_types)) {
- return true;
- }
- return false;
- }
- public static function readImageBlobFromFile($file, $mimeType) {
- $imBlob = null;
- if (class_exists('Imagick')) {
- $im = new Imagick($file);
- $imBlob = $im->getImageBlob();
- } else {
- if ($mimeType == 'image/jpeg') {
- $im = imagecreatefromjpeg($file);
- } else if ($mimeType == 'image/png') {
- $im = imagecreatefrompng($file);
- } else if ($mimeType == 'image/gif') {
- $im = imagecreatefromgif($file);
- }
- ob_start();
- if ($mimeType == 'image/png') {
- imagepng($im);
- } else if ($mimeType == 'image/jpeg' || $mimeType == 'image/jpg') {
- imagejpeg($im);
- } else if ($mimeType == 'image/gif') {
- imagegif($im);
- }
- $imBlob = ob_get_contents();
- ob_clean();
- }
- return $imBlob;
- }
- public static function read_image_data($file, $mime_type) {// TODO: used only in upload_image
- $im = null;
- if ($mime_type == 'image/jpeg') {
- $im = imagecreatefromjpeg($file);
- } else if ($mime_type == 'image/png') {
- $im = imagecreatefrompng($file);
- } else if ($mime_type == 'image/gif') {
- $im = imagecreatefromgif($file);
- }
- return $im;
- }
- public static function get_images($remote_table, $remote_id) {// TODO: RM Legacy
- return self::getImages($remote_table, $remote_id);
- }
- public static function getImages($remote_table, $remote_id) {
- $db = DB::getDB();
- $images = array();
- $sql = "select
- t.`ID`, t.`NAME`, t.`SIZE`, t.`TYPE`, t.`WIDTH`, t.`HEIGHT`
- , t.`DEST`, t.`A_CREATE_DATE`
- , t.`REMOTE_TABLE`, t.`REMOTE_ID`
- , UNIX_TIMESTAMP(`A_CREATE_DATE`) as A_CREATE_DATE_TS
- from `".self::conf_get_table_name()."` as t
- where
- t.`REMOTE_TABLE`='{$remote_table}'
- and t.`REMOTE_ID`='{$remote_id}'
- ";
- $res = $db->query($sql);
- while ($r = $db->fetch($res)) {
- $images[] = $r;
- }
- return $images;
- }
- /**
- * Try to upload image to DB.
- * @returns Array of errors
- */
- public static function upload_image($remote_table, $remote_id, $req_file_data) {// TODO: RM -> moved to uploadImage
- return self::uploadImage($remote_table, $remote_id, $req_file_data);
- }
- public static function uploadImage($remote_table, $remote_id, $req_file_data) {
- $errors = array();
- $maxsize = self::conf_get('max_size');
- if (is_uploaded_file($req_file_data['tmp_name'])) {
- if ($req_file_data['size'] < $maxsize) {
- $fp = fopen($req_file_data['tmp_name'], 'r');
- $imgData = fread($fp, filesize($req_file_data['tmp_name']));
- $imgData = addslashes($imgData);
- fclose($fp);
- $size = getimagesize($req_file_data['tmp_name']);
- $sqlObj = array();
- $sqlObj['DEST'] = (isset($_REQUEST['DEST']))? $_REQUEST['DEST'] : '';
- $sqlObj['TYPE'] = $size['mime'];
- $sqlObj['IMAGE'] = $imgData;
- $sqlObj['WIDTH'] = $size[0];
- $sqlObj['HEIGHT'] = $size[1];
- $sqlObj['NAME'] = $req_file_data['name'];
- $sqlObj['SIZE'] = $req_file_data['size'];
- $sqlObj['REMOTE_TABLE'] = $remote_table;
- $sqlObj['REMOTE_ID'] = $remote_id;
- // check if type is allowed to upload
- if (!self::is_allowed_type($sqlObj['TYPE'])) {
- $errors[] = 'File type "'.$sqlObj['TYPE'].'" is not allowed.';
- return $errors;
- }
- // convert image if selected option
- if ($sqlObj['DEST']) {
- $options_conf = self::conf_get_option_conf($remote_table, $sqlObj['DEST']);
- if (!empty($options_conf)) {
- $max_width = V::get('max_width', 0, $options_conf, 'int');
- $max_height = V::get('max_height', 0, $options_conf, 'int');
- if ($max_height > 0 && $max_width > 0) {
- $imBlob = self::readImageBlobFromFile($req_file_data['tmp_name'], $sqlObj['TYPE']);
- $sqlObj['IMAGE'] = self::resizeImageFromBlob($imBlob, $sqlObj['TYPE'], $max_width, $max_height);
- list($sqlObj['WIDTH'], $sqlObj['HEIGHT']) = self::getImageGeometryFromBlob($sqlObj['IMAGE']);
- $sqlObj['SIZE'] = strlen($sqlObj['IMAGE']);
- $sqlObj['IMAGE'] = addslashes($sqlObj['IMAGE']);
- }
- }
- }
- $sql_arr = array();
- foreach ($sqlObj as $key => $val) {
- $sql_arr["`{$key}`"] = "'{$val}'";
- }
- $sql = "insert into `".self::conf_get_table_name()."` (".implode(",", array_keys($sql_arr)).") values (".implode(",", array_values($sql_arr)).") ; ";
- $db = DB::getDB();
- if (!$db->query($sql)) {
- $errors[] = 'Unable to upload file - sql error';
- }
- self::update_remote_table_stat($remote_table, $remote_id);
- }
- }
- else {
- // if the file is not less than the maximum allowed, print an error
- $errors[] = 'File exceeds the Maximum File limit';
- }
- return $errors;
- }
- public static function delete_image($id, $remote_table, $remote_id) {// TODO: RM Legacy
- return self::deleteImage($id, $remote_table, $remote_id);
- }
- public static function deleteImage($id, $remote_table, $remote_id) {
- $db = DB::getDB();
- $sql = "delete from `".self::conf_get_table_name()."`
- where
- `ID`='{$id}'
- and `REMOTE_ID`='{$remote_id}'
- and `REMOTE_TABLE`='{$remote_table}'
- limit 1
- ";
- //echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">sql delete = ';print_r($sql);echo'</pre>';
- $db->query($sql);
- $affected = $db->affected_rows();
- self::update_remote_table_stat($remote_table, $remote_id);
- return $affected;
- }
- /**
- * Updates field ITEM_IMAGE_REMOTE in remote table.
- * TODO: check if field exists!
- */
- public static function update_remote_table_stat($remote_table, $remote_id) {// TODO: used only in this file
- // update stat `ITEM_IMAGE_REMOTE`
- $sql = "update `{$remote_table}`
- set `A_HAS_IMAGE`=(
- select count(1)
- from `".self::conf_get('table_name')."`
- where
- `REMOTE_ID`='{$remote_id}'
- and `REMOTE_TABLE`='{$remote_table}'
- )
- where `ID`='{$remote_id}'
- limit 1;
- ";
- $db = DB::getDB();
- $res = $db->query($sql);
- $affected = $db->affected_rows();
- }
- public static function resize_image($file, $w, $h, $crop = FALSE) {
- // TODO: image type
- $im = imagecreatefromjpeg($file);
- return self::resize_image_from_data($im, $w, $h, $crop);
- }
- public static function resize_image_from_data($im, $w, $h, $crop = FALSE) {
- if ($w == 0) $w = $h;
- if ($h == 0) $h = $w;
- $width = imagesx($im);
- $height = imagesy($im);
- if ($width < $w && $height < $h) {
- return $im;
- }
- $r = $width / $height;
- if ($crop) {
- if ($width > $height) {
- $width = ceil($width-($width*($r-$w/$h)));
- } else {
- $height = ceil($height-($height*($r-$w/$h)));
- }
- $newwidth = $w;
- $newheight = $h;
- } else {
- if ($w/$h > $r) {
- $newwidth = $h*$r;
- $newheight = $h;
- } else {
- $newheight = $w/$r;
- $newwidth = $w;
- }
- }
- $dst = imagecreatetruecolor($newwidth, $newheight);
- imagecopyresampled($dst, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
- return $dst;
- }
- public static function show_size($size) {// TODO: RM -> moved to showSize
- return self::showSize($size);
- }
- public static function showSize($size) {
- $ret_unit = 'B';
- if ($size > 1024 * 1024) {
- $size = $size / (1024 * 1024);
- $ret_unit = 'MB';
- } else if ($size > 1024) {
- $size = $size / 1024;
- $ret_unit = 'KB';
- }
- return number_format($size, 2, '.', ' ').''.$ret_unit;
- }
- }
|