setImage($file, $max_width, $max_height, $percent, $jpeg_quality); if(!empty($table)) $this->db_table = $table; } function setImage($file, $max_width = 0, $max_height = 0, $percent = 0, $jpeg_quality = 85) { if (!file_exists($file)) { $this->errmsg = "File doesn't exists"; $this->error = true; return false; } else if (!is_readable($file)) { $this->errmsg = "File is not readable"; $this->error = true; return false; } if (strstr(strtolower($file), ".gif")) { $this->format = "GIF"; } else if (strstr(strtolower($file), ".jpg") || strstr(strtolower($file), ".jpeg")) { $this->format = "JPEG"; } else if (strstr(strtolower($file), ".png")) { $this->format = "PNG"; } else { $this->errmsg = "Unknown file format"; $this->error = TRUE; return FALSE; } if ($max_width == 0 && $max_height == 0 && $percent == 0) { $percent = 100; } $this->max_width = $max_width; $this->max_height = $max_height; $this->percent = $percent; $this->file = $file; $this->jpeg_quality = $jpeg_quality; return true; } function calc_width($width, $height) { $new_width = $this->max_width; $new_wp = (100 * $new_width) / $width; $new_height = ($height * $new_wp) / 100; return array($new_width, $new_height); } function calc_height($width, $height) { $new_height = $this->max_height; $new_hp = (100 * $new_height) / $height; $new_width = ($width * $new_hp) / 100; return array($new_width, $new_height); } function calc_percent($width, $height) { $new_width = ($width * $this->percent) / 100; $new_height = ($height * $this->percent) / 100; return array($new_width, $new_height); } function return_value($array) { $array[0] = intval($array[0]); $array[1] = intval($array[1]); return $array; } function set_max_width($width){ $this->max_width = $width; } function set_max_height($height){ $this->max_height = $height; } function calc_image_size($width, $height) { $new_size = array($width, $height); if ($this->max_width > 0 && $width > $this->max_width) { $new_size = $this->calc_width($width, $height); if ($this->max_height > 0 && $new_size[1] > $this->max_height) { $new_size = $this->calc_height($new_size[0], $new_size[1]); } return $this->return_value($new_size); } if ($this->max_height > 0 && $height > $this->max_height) { $new_size = $this->calc_height($width, $height); return $this->return_value($new_size); } if ($this->percent > 0) { $new_size = $this->calc_percent($width, $height); return $this->return_value($new_size); } return $this->return_value($new_size); } function show_error_image() { header("Content-type: image/png"); $err_img = ImageCreate(220, 25); $bg_color = ImageColorAllocate($err_img, 0, 0, 0); $fg_color1 = ImageColorAllocate($err_img, 255, 255, 255); $fg_color2 = ImageColorAllocate($err_img, 255, 0, 0); ImageString($err_img, 3, 6, 6, "ERROR:", $fg_color2); ImageString($err_img, 3, 55, 6, $this->errmsg, $fg_color1); ImagePng($err_img); ImageDestroy($err_img); } function show($name = "") { if ($this->error) { $this->show_error_image(); return; } $size = GetImageSize($this->file); $new_size = $this->calc_image_size($size[0], $size[1]); # # Good idea from Mariano Cano Pérez # Requires GD 2.0.1 (PHP >= 4.0.6) # if (function_exists("ImageCreateTrueColor")) { $new_image = ImageCreateTrueColor($new_size[0], $new_size[1]); } else { $new_image = ImageCreate($new_size[0], $new_size[1]); } switch ($this->format) { case "GIF": $old_image = ImageCreateFromGif($this->file); break; case "JPEG": $old_image = ImageCreateFromJpeg($this->file); break; case "PNG": $old_image = ImageCreateFromPng($this->file); break; } # # Good idea from Michael Wald # Requires GD 2.0.1 (PHP >= 4.0.6) # if (function_exists("ImageCopyResampled")) { ImageCopyResampled($new_image, $old_image, 0, 0, 0, 0, $new_size[0], $new_size[1], $size[0], $size[1]); } else { ImageCopyResized($new_image, $old_image, 0, 0, 0, 0, $new_size[0], $new_size[1], $size[0], $size[1]); } switch ($this->format) { case "GIF": if (!empty($name)) { ImageGif($new_image, $name); } else { header("Content-type: image/gif"); ImageGif($new_image); } break; case "JPEG": if (!empty($name)) { ImageJpeg($new_image, $name, $this->jpeg_quality); } else { header("Content-type: image/jpeg"); ImageJpeg($new_image, "", $this->jpeg_quality); } break; case "PNG": if (!empty($name)) { ImagePng($new_image, $name); } else { header("Content-type: image/png"); ImagePng($new_image); } break; } ImageDestroy($new_image); ImageDestroy($old_image); return; } function save($name) { $this->show($name); } function save_db($img, $cityID, $desc, $userID){ $sql="insert into ".$this->db_table."(image, city_id, description, userID) values('".$img."','".$cityID."','".mysql_real_escape_string($desc)."','".$userID."')"; //echo $sql; return mysql_query($sql); } function getPendingTotal(){ if($this->totalPending == 0){ $sql="select count(*) as cnt from ".$this->db_table." where status='pending'"; $res=mysql_query($sql); if($row=mysql_fetch_array_deutf8($res)){ $this->totalPending = $row['cnt']; } } return $this->totalPending; } function getPendingList(){ $sql="select ci.*, ct.id as cityID, ct.city, ct.country_id, cc.country_name from ".$this->db_table." as ci inner join city_cities as ct on ci.city_id = ct.id inner join city_countries as cc on cc.country_id=ct.country_id where ci.status='pending'"; //echo $sql; $res=mysql_query($sql); while($row=mysql_fetch_array_deutf8($res)){ $pending[]=$row; } return $pending; } function setAttr($attrName,$attrVal){ $this->$attrName = $attrVal; } function setIDUser($id,$userID = '',$status='active'){ if($status == 'any') $stat=" and status='active' or status='pending'"; else $stat = " and status='".$status."'"; $sql="select * from ".$this->db_table." where id=$id".$stat; // and status='".$status."'"; if($userID != '') $sql .= " and userID='$userID'"; //echo $sql; $res=mysql_query($sql); $row=mysql_fetch_array_deutf8($res); //var_dump($row); $this->image = $row; } function getImage(){ return $this->image; } function getImages($cityID, $page = 0, $limit, $admin = false){ if(!$admin) $sql = "select * from ".$this->db_table." where status='active' and city_id = $cityID"; else $sql = "select * from ".$this->db_table." where status != 'deactivated' and city_id = $cityID"; if($limit){ $from = $page*$limit; $sql .= " limit ".$from.", ".$limit; } //echo $sql; $res = mysql_query($sql); while($row=mysql_fetch_array_deutf8($res)) $images[]=$row; return $images; } function getTotal($cityID,$status='active'){ $cnt_sql="select count(*) as cnt from ".$this->db_table." where status='$status' and city_id = $cityID"; $cres=mysql_query($cnt_sql); $crow=mysql_fetch_array_deutf8($cres); return $crow['cnt']; } function getMostPopular($cityID, $limit = ''){ $sql="select id, image, city_id, userID, (grade_1 * 3) + (grade_2 * 1) + (grade_3 * -1) as grade from ".$this->db_table." where city_id=$cityID and status='active' order by grade desc"; if($limit != '') $sql .= " limit ".$limit; //echo $sql; $res=mysql_query($sql); while($row=mysql_fetch_array_deutf8($res)) $images[]=$row; return $images; } function vote($imageID, $grade){ $sql = "update ".$this->db_table." set $grade = $grade + 1 where id=$imageID"; return mysql_query($sql); } function delete($imageID){ $sql = "update ".$this->db_table." set status='deactivated' where id = ".$imageID; return mysql_query($sql); } function activate($imageID){ $sql = "update ".$this->db_table." set status='active' where id = ".$imageID; return mysql_query($sql); } } ?>