ruạṛ
<?php /** * Class for managing Images * @author Callum Muir * @date 17/7/13 */ class Image { public $id = 0; public $path = ""; public $filename = ""; public $width = 0; public $height = 0; public $position = 10; public $type = ""; public $container_id = 1; public $active = false; public $title = ""; private static $db = array ( "id" => "image_id", "type" => "image_type", "container_id" => "container_id", "path" => "image_path", "filename" => "image_filename", "title" => "title", "width" => "width", "height" => "height", "position" => "image_position", "active" => "active" ); private static $types = array ( "id" => "int", "type" => "string", "container_id" => "int", "path" => "path", "filename" => "string", "title" => "string", "width" => "int", "height" => "int", "position" => "int", "active" => "bool" ); /** * Makes an Image from a row retrieved from the database * @param array row The database row as an associative array * @return Image The new Image object */ private static function makeFromRow($row) { $image = new Image(); foreach(self::$db as $property => $field) { switch(self::$types[$property]) { case "int": $value = (int) $row[$field]; break; case "string": $value = (string) $row[$field]; break; case "bool": $value = (bool) $row[$field]; break; case "path": $value = DOC_ROOT . ((string) $row[$field]); break; default: $value = $row[$field]; break; } $image->$property = $value; } return $image; } /** * Makes a single Image from a query * @param string query The query string to be run * @return Image The new Image object * @throws Exception If there's an error in the SQL or there aren't any matching rows */ private static function makeOne($query) { if(!$result = mysql_query($query)) { throw new Exception("Error in Image load SQL: " . mysql_error() . ": " . $query); } if(mysql_num_rows($result) === 0) { throw new Exception("No Images returned for SQL: " . $query); } return self::makeFromRow(mysql_fetch_assoc($result)); } /** * Makes an array of Images from a query * @param string query The query string to be run * @return array(Image) The array of Image objects * @throws Exception If there's an error in the SQL */ private static function makeMany($query) { if(!$result = mysql_query($query)) { throw new Exception("Error in Images load SQL: " . mysql_error() . ": " . $query); } $images = array(); while($row = mysql_fetch_assoc($result)) { $images[] = self::makeFromRow($row); } return $images; } /** * Creates an Image object from a database row * @param int id The database identifier of the table row * @return Image The new Image object */ public static function loadFromDatabaseId($id) { $query = "SELECT " . implode(", ", self::$db) . " " . "FROM image_data " . "WHERE image_id = " . (int) $id; return self::makeOne($query); } /** * Creates an array of Image objects from a group of rows in the database * @param string image_type The type of Image to load * @param int container_id The related thing to load it for * @return array(Image) The resulting array of Images */ public static function loadFromRelatedRows($image_type, $container_id) { $query = "SELECT " . implode(", ", self::$db) . " " . "FROM image_data " . "WHERE image_type = '" . mysql_escape_string($image_type) . "' " . "AND container_id = " . (int) $container_id; return self::makeMany($query); } /** * Creates an Image object from a file * @param string path The path to the file * @param string filename The name of the file * @return Image The new Image object */ public static function makeFromFilename($path, $filename) { $image = new Image(); $image->path = rtrim($path, "/") . "/"; $image->filename = $filename; if(is_uploaded_file($filename)) { $image->path = sys_get_temp_dir(); } $imageSize = getimagesize($image->path . $image->filename); $image->width = $imageSize[0]; $image->height = $imageSize[1]; return $image; } /** * Moves an uploaded image, resizes it if necessary and saves it * @param string tmp_name The temp_name for the image * @param string path The desired path to the saved image * @param string filename The desired filename for the saved image * @param int width The new width for the image * @param int height The new height for the image */ public static function upload($tmp_name, $path, $filename, $width = null, $height = null) { $uploadedFile = self::makeFromFilename(dirname($tmp_name), basename($tmp_name)); if($width === null || $height === null) { $uploadedFile->moveAndRename($path, $filename); $newFile = $uploadedFile; } else { $newFile = $uploadedFile->resize($width, $height, $path, $filename); } return $newFile; } /** * Prepares a property for insertion in the database * @param string propertyName The name of the property * @return ? The prepared value */ private function prepareProperty($propertyName) { $value = 0; switch(self::$types[$propertyName]) { case "int": $value = (int) $this->$propertyName; break; case "string": $value = clean_plain_data((string) $this->$propertyName); break; case "bool": $value = (int) $this->$propertyName; break; case "path": $value = clean_plain_data((string) str_replace(DOC_ROOT, "", $this->$propertyName)); } return $value; } /** * Creates a string to insert into an insertion query * @return string The insertion string */ private function insertString() { $properties = array(); foreach(array_keys(self::$db) as $property) { $properties[] = "'" . self::prepareProperty($property) . "'"; } return implode(", ", $properties); } /** * Creates a string to insert into an update query * @return string The update string */ private function updateString() { $changes = array(); foreach(array_keys(self::$db) as $property) { $changes[] = self::$db[$property] . " = '" . self::prepareProperty($property) . "'"; } return implode(", ", $changes); } /** * Saves a new Image to the database * @throws Exception If the query fails */ private function create() { $query = "INSERT INTO image_data(" . implode(", ", self::$db) . ") " . "VALUES(" . $this->insertString() . ")"; if(!mysql_query($query)) { throw new Exception("Error in Image create SQL: " . mysql_error() . ": " . $query); } $this->id = mysql_insert_id(); } /** * Updates an Image in the database * @throws Exception If the query fails */ private function update() { $query = "UPDATE image_data " . "SET " . $this->updateString() . " " . "WHERE image_id = " . (int) $this->id; if(!mysql_query($query)) { throw new Exception("Error in Image update SQL: " . mysql_error() . ": " . $query); } } /** * Either updates or inserts the image into the database */ public function save() { if($this->id === 0) { $this->create(); } else { $this->update(); } } /** * Deletes the Image from the database and the server * @throws Exception If the query fails or the file is not deleted */ public function delete() { $query = "DELETE FROM image_data " . "WHERE image_id = " . (int) $this->id; if(!mysql_query($query)) { throw new Exception("Error in Image delete SQL: " . mysql_error() . ": " . $query); } @unlink($this->path . $this->filename); if(file_exists($this->path . $this->filename)) { throw new Exception("File still exists."); } } /** * Moves the Image from somewhere on the server to somewhere else on the server * @param string path The new path to the Image * @throws Exception If the move fails */ public function move($path) { $newPath = rtrim($path, "/") . "/"; if(is_uploaded_file($this->filename)) { move_uploaded_file($this->filename, $newPath . $this->filename); } else { rename($this->path . $this->filename, $newPath . $this->filename); } $this->path = $newPath; chmod($this->path . $this->filename, 0644); } /** * Renames the Image * @param string filename The new name of the Image * @throws Exception If the rename fails or the Image is still a temporary file */ public function rename($filename) { if(!is_uploaded_file($this->filename)) { rename($this->path . $this->filename, $this->path . $filename); } else { throw new Exception("Tried to rename temporary file"); } $this->filename = $filename; chmod($this->path . $this->filename, 0644); } /** * Moves and renames the Image * @param string path The new path to the Image * @param string filename The Image's new Filename * @throws Exception If the move and rename fails */ public function moveAndRename($path, $filename) { $newPath = rtrim($path, "/") . "/"; if(is_uploaded_file($this->filename)) { move_uploaded_file($this->filename, $newPath . $filename); } else { rename($this->path . $this->filename, $newPath . $filename); } $this->path = $newPath; $this->filename = $filename; chmod($this->path . $this->filename, 0644); } /** * Creates a resized version of the Image * @param int width The resized Image's max width, or 0 to have no max width * @param int height The resized Image's max height, or 0 to have no max height * @param string path The path to the new Image * @param string filename The filename of the new Image * @return Image The resized Image */ public function resize($width, $height, $path, $filename) { $newPath = rtrim($path, "/") . "/"; thumbnail($this->path . $this->filename, $newPath . $filename, $width, $height); return self::makeFromFilename($newPath, $filename); } /** * Returns the link to the image * @return string The resulting link */ public function link() { return str_replace(DOC_ROOT, "", $this->path) . $this->filename; } /** * Creates an HTML tag for the image * @return string The resulting tag */ public function tag($class = "", $id = "") { return "<img src='" . $this->link() . "' width='" . $this->width . "' height='" . $this->height . "' alt='" . $this->title . "' class='" . $class . "' id='" . $id . "' />\n"; } /** * Replaces the image with another Image * @param Image image The Image to replace this Image with */ public function replaceWith(Image $image) { if($this->path !== $image->path || $this->filename !== $image->filename) { @unlink($this->path . $this->filename); } $this->path = $image->path; $this->filename = $image->filename; $this->width = $image->width; $this->height = $image->height; } } ?>
cải xoăn