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) { $errors = array(); $maxsize = self::conf_get('max_size'); //echo'
$_FILES = ';print_r($_FILES);echo''; if (is_uploaded_file($req_file_data['tmp_name'])) { // check the file is less than the maximum file size if ($req_file_data['size'] < $maxsize) { // prepare the image for insertion //$imgData = mysql_real_escape_string(file_get_contents($req_file_data['tmp_name']), $conn);// nie dziala mysql_real_escape_string $fp = fopen($req_file_data['tmp_name'], 'r'); $imgData = fread($fp, filesize($req_file_data['tmp_name'])); $imgData = addslashes($imgData); fclose($fp); // get the image info.. $size = getimagesize($req_file_data['tmp_name']); $obj = array(); $obj['DEST'] = (isset($_REQUEST['DEST']))? $_REQUEST['DEST'] : ''; $obj['TYPE'] = $size['mime']; $obj['IMAGE'] = $imgData; $obj['WIDTH'] = $size[0]; $obj['HEIGHT'] = $size[1]; $obj['NAME'] = $req_file_data['name']; $obj['SIZE'] = $req_file_data['size']; $obj['REMOTE_TABLE'] = $remote_table; $obj['REMOTE_ID'] = $remote_id; // check if type is allowed to upload if (!DB_Image::is_allowed_type($obj['TYPE'])) { $errors []= 'File type "'.$obj['TYPE'].'" is not allowed.'; return $errors; } // convert image if selected option if ($obj['DEST']) { $options_conf = DB_Image::conf_get_option_conf( $remote_table, $obj['DEST'] ); if (!empty($options_conf)) { $max_width = 0; $max_height = 0; if (isset($options_conf['max_height']) && $obj['HEIGHT'] > $options_conf['max_height']) { $max_height = $options_conf['max_height']; } if (isset($options_conf['max_width']) && $obj['WIDTH'] > $options_conf['max_width']) { $max_width = $options_conf['max_width']; } if ($max_height || $max_width) { //$img = DB_Image::resize_image($req_file_data['tmp_name'], $max_width, $max_height); //open image, from file to data // read data $im = DB_Image::read_image_data($req_file_data['tmp_name'], $obj['TYPE']); // resize image $im = DB_Image::resize_image_from_data($im, $max_width, $max_height); ob_start(); imagejpeg($im); $obj['IMAGE'] = ob_get_clean(); $obj['SIZE'] = strlen($obj['IMAGE']); $obj['IMAGE'] = addslashes($obj['IMAGE']); $obj['WIDTH'] = imagesx($im); $obj['HEIGHT'] = imagesy($im); } } } //echo'
size = ';print_r($size);echo''; $sql_arr = array(); foreach ($obj as $key => $val) { $sql_arr["`".$key."`"] = "'".$val."'"; }//end foreach $sql = "insert into `".self::conf_get_table_name()."` (".implode(",", array_keys($sql_arr)).") values (".implode(",", array_values($sql_arr)).") ; "; // insert the image //echo'
sql = ';print_r($sql);echo''; $db = DB::getDB(); if (!$db->query($sql)) { $errors []= 'Unable to upload file - sql error'; //echo'
mysql err: '.mysql_errno($conn);var_dump(mysql_error($conn));echo''; //echo'
sql: ';print_r($sql);echo''; } 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) { $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'
sql delete = ';print_r($sql);echo''; $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) { // 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 ) { $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; } }