From 2502240ce4ad25e9fb60ee2468764e25346d2917 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Tue, 23 Dec 2008 04:14:07 +0000 Subject: Add very simple graphics toolkits. Track a set of rules in Graphics_Rule_Model which specify how we turn original images into thumbnails and resizes. There's one set of rules that applies to every image in the Gallery. Track the state of thumbs and resizes with a "dirty" bit. The new graphics helper manages the rules and can rebuild the thumbs and resizes for any images that are considered "dirty". Introduce the concept of an "album cover" which is an item that an album points to. We'll use that item as the source for the album's thumbnail/resize. Conflated with this change (sorry!) I also changed the Var table to use module_name instead of module_id. This may be marginally less efficient, but it's much easier to follow in the database. --- core/helpers/graphics.php | 121 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 core/helpers/graphics.php (limited to 'core/helpers/graphics.php') diff --git a/core/helpers/graphics.php b/core/helpers/graphics.php new file mode 100644 index 00000000..b38515fb --- /dev/null +++ b/core/helpers/graphics.php @@ -0,0 +1,121 @@ +module_name = $module_name; + $rule->target = $target; + $rule->operation = $operation; + $rule->priority = $priority; + $rule->args = serialize($args); + $rule->save(); + } + + /** + * Remove all rules for this module + * @param string $module_name + */ + public static function remove_rules($module_name) { + $db = Database::instance(); + $db->query("DELETE FROM `graphics_rules` WHERE `module_name` = '$module_name'"); + } + + /** + * Rebuild the thumb and resize for the given item. + * @param Item_Model $item + */ + public static function generate($item) { + if ($item->type == "album") { + $cover = $item->album_cover(); + if (!$cover) { + return; + } + $input_file = $cover->file_path(); + } else { + $input_file = $item->file_path(); + } + + $ops = array(); + if ($item->thumb_dirty) { + $ops["thumb"] = $item->thumb_path(); + } + if ($item->resize_dirty) { + $ops["resize"] = $item->resize_path(); + } + + if (!$ops) { + return; + } + + foreach (array("thumb" => $item->thumb_path(), + "resize" => $item->resize_path()) as $target => $output_file) { + foreach (ORM::factory("graphics_rule") + ->where("target", $target) + ->orderby("priority", "asc") + ->find_all() as $rule) { + $args = array_merge(array($input_file, $output_file), unserialize($rule->args)); + call_user_func_array(array("graphics", $rule->operation), $args); + } + } + + $dims = getimagesize($item->thumb_path()); + $item->thumb_width = $dims[0]; + $item->thumb_height = $dims[1]; + $item->thumb_dirty = 0; + + $dims = getimagesize($item->resize_path()); + $item->resize_width = $dims[0]; + $item->resize_height = $dims[1]; + $item->resize_dirty = 0; + $item->save(); + } + + /** + * Wrapper around Image::resize + * @param string $input_file + * @param string $output_file + * @param integer $width + * @param integer $height + * @param integer $master Master Dimension constant from the Image class + */ + public static function resize($input_file, $output_file, $width, $height, $master) { + Image::factory($input_file) + ->resize($width, $height, $master) + ->save($output_file); + } +} -- cgit v1.2.3