From fb64ee90ef2a1da4b568e4ca7f22e0771f3d8660 Mon Sep 17 00:00:00 2001 From: matt Date: Mon, 16 Oct 2006 06:06:18 +0000 Subject: [PATCH] Some helper functions for themes and images git-svn-id: http://svn.automattic.com/wordpress/trunk@4401 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/admin-functions.php | 49 +++++++++++++++++++++++++++++++++++- wp-includes/theme.php | 30 +++++++++++++++++++++- 2 files changed, 77 insertions(+), 2 deletions(-) diff --git a/wp-admin/admin-functions.php b/wp-admin/admin-functions.php index 49a4aa76f8..e673aa31f8 100644 --- a/wp-admin/admin-functions.php +++ b/wp-admin/admin-functions.php @@ -2073,4 +2073,51 @@ function update_home_siteurl($old_value, $value) { add_action('update_option_home', 'update_home_siteurl', 10, 2); add_action('update_option_siteurl', 'update_home_siteurl', 10, 2); -?> +function wp_crop_image($src_file, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false) { + if ( ctype_digit($src_file) ) // Handle int as attachment ID + $src_file = get_attached_file($src_file); + + $src = wp_load_image($src_file); + + if ( !is_resource($src) ) + return $src; + + $dst = imagecreatetruecolor($dst_w, $dst_h); + + if ( $src_abs ) { + $src_w -= $src_x; + $src_h -= $src_y; + } + + imageantialias($dst, true); + imagecopyresampled($dst, $src, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); + + if ( !$dst_file ) + $dst_file = str_replace(basename($src_file), 'cropped-'.basename($src_file), $src_file); + + $dst_file = preg_replace('/\\.[^\\.]+$/', '.jpg', $dst_file); + + if ( imagejpeg($dst, $dst_file) ) + return $dst_file; + else + return false; +} + +function wp_load_image($file) { + if ( ctype_digit($file) ) + $file = get_attached_file($file); + + if ( !file_exists($file) ) + return "File '$file' doesn't exist?"; + + $contents = file_get_contents($file); + + $image = imagecreatefromstring($contents); + + if ( !is_resource($image) ) + return "File '$file' is not image?"; + + return $image; +} + +?> \ No newline at end of file diff --git a/wp-includes/theme.php b/wp-includes/theme.php index e5a1c8db0b..1662d460e4 100644 --- a/wp-includes/theme.php +++ b/wp-includes/theme.php @@ -427,4 +427,32 @@ function validate_current_theme() { return true; } -?> +function get_theme_mod($name, $default = false) { + $theme = get_current_theme(); + + $mods = get_option("mods_$theme"); + + if ( isset($mods[$name]) ) + return $mods[$name]; + + return sprintf($default, get_template_directory_uri()); +} + +function set_theme_mod($name, $value) { + $theme = get_current_theme(); + + $mods = get_option("mods_$theme"); + + $mods[$name] = $value; + + update_option("mods_$theme", $mods); + wp_cache_delete("mods_$theme", 'options'); +} + +function remove_theme_mods() { + $theme = get_current_theme(); + + delete_option("mods_$theme"); +} + +?> \ No newline at end of file