From a92b903c0e6c2faa2a9480e23e2d3e4b6308878f Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Sat, 24 Dec 2011 19:05:58 +0200 Subject: Improve the Image manipulation library --- system/libraries/Image_lib.php | 150 ++++++++++++++++------------------------- 1 file changed, 58 insertions(+), 92 deletions(-) (limited to 'system') diff --git a/system/libraries/Image_lib.php b/system/libraries/Image_lib.php index 20ca1f055..70a127987 100644 --- a/system/libraries/Image_lib.php +++ b/system/libraries/Image_lib.php @@ -1,4 +1,4 @@ -y_axis = ($this->y_axis == '' OR ! is_numeric($this->y_axis)) ? 0 : $this->y_axis; // Watermark-related Stuff... - if ($this->wm_font_color != '') + if ($this->wm_font_color != '' AND strlen($this->wm_font_color) === 6) { - if (strlen($this->wm_font_color) == 6) - { - $this->wm_font_color = '#'.$this->wm_font_color; - } + $this->wm_font_color = '#'.$this->wm_font_color; } - if ($this->wm_shadow_color != '') + if ($this->wm_shadow_color != '' AND strlen($this->wm_shadow_color) === 6) { - if (strlen($this->wm_shadow_color) == 6) - { - $this->wm_shadow_color = '#'.$this->wm_shadow_color; - } + $this->wm_shadow_color = '#'.$this->wm_shadow_color; } if ($this->wm_overlay_path != '') @@ -381,13 +375,7 @@ class CI_Image_lib { */ public function resize() { - $protocol = 'image_process_'.$this->image_library; - - if (preg_match('/gd2$/i', $protocol)) - { - $protocol = 'image_process_gd'; - } - + $protocol = (strtolower(substr($this->image_library, 0, -3)) === 'gd2') ? 'image_process_gd' : 'image_process_'.$this->image_library; return $this->$protocol('resize'); } @@ -404,13 +392,7 @@ class CI_Image_lib { */ public function crop() { - $protocol = 'image_process_'.$this->image_library; - - if (preg_match('/gd2$/i', $protocol)) - { - $protocol = 'image_process_gd'; - } - + $protocol = (strtolower(substr($this->image_library, 0, -3)) === 'gd2') ? 'image_process_gd' : 'image_process_'.$this->image_library; return $this->$protocol('crop'); } @@ -453,7 +435,6 @@ class CI_Image_lib { if ($this->image_library == 'imagemagick' OR $this->image_library == 'netpbm') { $protocol = 'image_process_'.$this->image_library; - return $this->$protocol('rotate'); } @@ -484,20 +465,14 @@ class CI_Image_lib { // If the target width/height match the source, AND if the new file name is not equal to the old file name // we'll simply make a copy of the original with the new name... assuming dynamic rendering is off. - if ($this->dynamic_output === FALSE) + if ($this->dynamic_output === FALSE AND $this->orig_width == $this->width AND $this->orig_height == $this->height) { - if ($this->orig_width == $this->width AND $this->orig_height == $this->height) + if ($this->source_image != $this->new_image AND @copy($this->full_src_path, $this->full_dst_path)) { - if ($this->source_image != $this->new_image) - { - if (@copy($this->full_src_path, $this->full_dst_path)) - { - @chmod($this->full_dst_path, FILE_WRITE_MODE); - } - } - - return TRUE; + @chmod($this->full_dst_path, FILE_WRITE_MODE); } + + return TRUE; } // Let's set up our values based on the action @@ -601,9 +576,7 @@ class CI_Image_lib { if ( ! preg_match("/convert$/i", $this->library_path)) { - $this->library_path = rtrim($this->library_path, '/').'/'; - - $this->library_path .= 'convert'; + $this->library_path = rtrim($this->library_path, '/').'/convert'; } // Execute the command @@ -808,11 +781,8 @@ class CI_Image_lib { if ($this->rotation_angle == 'hor') { - for ($i = 0; $i < $height; $i++) + for ($i = 0; $i < $height; $i++, $left = 0, $right = $width-1) { - $left = 0; - $right = $width-1; - while ($left < $right) { $cl = imagecolorat($src_img, $left, $i); @@ -828,11 +798,8 @@ class CI_Image_lib { } else { - for ($i = 0; $i < $width; $i++) + for ($i = 0; $i < $width; $i++, $top = 0, $bot = $height-1) { - $top = 0; - $bot = $height-1; - while ($top < $bot) { $ct = imagecolorat($src_img, $i, $top); @@ -993,12 +960,9 @@ class CI_Image_lib { { $this->image_display_gd($src_img); } - else + elseif ( ! $this->image_save_gd($src_img)) { - if ( ! $this->image_save_gd($src_img)) - { - return FALSE; - } + return FALSE; } imagedestroy($src_img); @@ -1065,7 +1029,9 @@ class CI_Image_lib { if ($this->wm_use_truetype == TRUE) { if ($this->wm_font_size == '') - $this->wm_font_size = '17'; + { + $this->wm_font_size = 17; + } $fontwidth = $this->wm_font_size-($this->wm_font_size/4); $fontheight = $this->wm_font_size; @@ -1090,11 +1056,11 @@ class CI_Image_lib { switch ($this->wm_vrt_alignment) { - case "T" : + case 'T': break; - case "M": $y_axis += ($this->orig_height/2)+($fontheight/2); + case 'M': $y_axis += ($this->orig_height/2)+($fontheight/2); break; - case "B": $y_axis += ($this->orig_height - $fontheight - $this->wm_shadow_distance - ($fontheight/2)); + case 'B': $y_axis += ($this->orig_height - $fontheight - $this->wm_shadow_distance - ($fontheight/2)); break; } @@ -1104,32 +1070,34 @@ class CI_Image_lib { // Set horizontal alignment switch ($this->wm_hor_alignment) { - case "L": + case 'L': break; - case "R": - if ($this->wm_use_drop_shadow) - $x_shad += ($this->orig_width - $fontwidth*strlen($this->wm_text)); - $x_axis += ($this->orig_width - $fontwidth*strlen($this->wm_text)); + case 'R': + if ($this->wm_use_drop_shadow) + { + $x_shad += ($this->orig_width - $fontwidth*strlen($this->wm_text)); + $x_axis += ($this->orig_width - $fontwidth*strlen($this->wm_text)); + } break; - case "C": - if ($this->wm_use_drop_shadow) - $x_shad += floor(($this->orig_width - $fontwidth*strlen($this->wm_text))/2); - $x_axis += floor(($this->orig_width -$fontwidth*strlen($this->wm_text))/2); + case 'C': + if ($this->wm_use_drop_shadow) + { + $x_shad += floor(($this->orig_width - $fontwidth*strlen($this->wm_text))/2); + $x_axis += floor(($this->orig_width - $fontwidth*strlen($this->wm_text))/2); + } break; } // Add the text to the source image - if ($this->wm_use_truetype) + if ($this->wm_use_truetype AND $this->wm_use_drop_shadow) { - if ($this->wm_use_drop_shadow) - imagettftext($src_img, $this->wm_font_size, 0, $x_shad, $y_shad, $drp_color, $this->wm_font_path, $this->wm_text); - imagettftext($src_img, $this->wm_font_size, 0, $x_axis, $y_axis, $txt_color, $this->wm_font_path, $this->wm_text); + imagettftext($src_img, $this->wm_font_size, 0, $x_shad, $y_shad, $drp_color, $this->wm_font_path, $this->wm_text); + imagettftext($src_img, $this->wm_font_size, 0, $x_axis, $y_axis, $txt_color, $this->wm_font_path, $this->wm_text); } - else + elseif ($this->wm_use_drop_shadow) { - if ($this->wm_use_drop_shadow) - imagestring($src_img, $this->wm_font_size, $x_shad, $y_shad, $this->wm_text, $drp_color); - imagestring($src_img, $this->wm_font_size, $x_axis, $y_axis, $this->wm_text, $txt_color); + imagestring($src_img, $this->wm_font_size, $x_shad, $y_shad, $this->wm_text, $drp_color); + imagestring($src_img, $this->wm_font_size, $x_axis, $y_axis, $this->wm_text, $txt_color); } // Output the final image @@ -1369,26 +1337,24 @@ class CI_Image_lib { } $vals = getimagesize($path); - $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png'); - - $mime = (isset($types[$vals['2']])) ? 'image/'.$types[$vals['2']] : 'image/jpg'; + $mime = (isset($types[$vals[2]])) ? 'image/'.$types[$vals[2]] : 'image/jpg'; if ($return == TRUE) { - $v['width'] = $vals['0']; - $v['height'] = $vals['1']; - $v['image_type'] = $vals['2']; - $v['size_str'] = $vals['3']; - $v['mime_type'] = $mime; - - return $v; + return array( + 'width' => $vals[0], + 'height' => $vals[1], + 'image_type' => $vals[2], + 'size_str' => $vals[3], + 'mime_type' => $mime + ); } - $this->orig_width = $vals['0']; - $this->orig_height = $vals['1']; - $this->image_type = $vals['2']; - $this->size_str = $vals['3']; + $this->orig_width = $vals[0]; + $this->orig_height = $vals[1]; + $this->image_type = $vals[2]; + $this->size_str = $vals[3]; $this->mime_type = $mime; return TRUE; @@ -1482,10 +1448,10 @@ class CI_Image_lib { { if ( ! extension_loaded('gd')) { - if ( ! dl('gd.so')) - { - return FALSE; - } + /* As it is stated in the PHP manual, dl() is not always available + * and even if so - it could generate an E_WARNING message on failure + */ + return (function_exists('dl') AND @dl('gd.so')); } return TRUE; -- cgit v1.2.3-24-g4f1b From 5342e8b0d8e4c0b585a4757d60f0fd7127704755 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 27 Dec 2011 02:57:52 +0200 Subject: Remove access lines from method descriptions --- system/libraries/Image_lib.php | 24 ------------------------ 1 file changed, 24 deletions(-) (limited to 'system') diff --git a/system/libraries/Image_lib.php b/system/libraries/Image_lib.php index 70a127987..2737503a8 100644 --- a/system/libraries/Image_lib.php +++ b/system/libraries/Image_lib.php @@ -111,7 +111,6 @@ class CI_Image_lib { * * Resets values in case this class is used in a loop * - * @access public * @return void */ public function clear() @@ -154,7 +153,6 @@ class CI_Image_lib { /** * initialize image preferences * - * @access public * @param array * @return bool */ @@ -370,7 +368,6 @@ class CI_Image_lib { * This is a wrapper function that chooses the proper * resize function based on the protocol specified * - * @access public * @return bool */ public function resize() @@ -387,7 +384,6 @@ class CI_Image_lib { * This is a wrapper function that chooses the proper * cropping function based on the protocol specified * - * @access public * @return bool */ public function crop() @@ -404,7 +400,6 @@ class CI_Image_lib { * This is a wrapper function that chooses the proper * rotation function based on the protocol specified * - * @access public * @return bool */ public function rotate() @@ -455,7 +450,6 @@ class CI_Image_lib { * * This function will resize or crop * - * @access public * @param string * @return bool */ @@ -561,7 +555,6 @@ class CI_Image_lib { * * This function will resize, crop or rotate * - * @access public * @param string * @return bool */ @@ -629,7 +622,6 @@ class CI_Image_lib { * * This function will resize, crop or rotate * - * @access public * @param string * @return bool */ @@ -713,7 +705,6 @@ class CI_Image_lib { /** * Image Rotate Using GD * - * @access public * @return bool */ public function image_rotate_gd() @@ -766,7 +757,6 @@ class CI_Image_lib { * * This function will flip horizontal or vertical * - * @access public * @return bool */ public function image_mirror_gd() @@ -845,7 +835,6 @@ class CI_Image_lib { * This is a wrapper function that chooses the type * of watermarking based on the specified preference. * - * @access public * @param string * @return bool */ @@ -866,7 +855,6 @@ class CI_Image_lib { /** * Watermark - Graphic Version * - * @access public * @return bool */ public function overlay_watermark() @@ -976,7 +964,6 @@ class CI_Image_lib { /** * Watermark - Text Version * - * @access public * @return bool */ public function text_watermark() @@ -1123,7 +1110,6 @@ class CI_Image_lib { * This simply creates an image resource handle * based on the type of image being processed * - * @access public * @param string * @return resource */ @@ -1180,7 +1166,6 @@ class CI_Image_lib { * Takes an image resource as input and writes the file * to the specified destination * - * @access public * @param resource * @return bool */ @@ -1241,7 +1226,6 @@ class CI_Image_lib { /** * Dynamically outputs an image * - * @access public * @param resource * @return void */ @@ -1277,7 +1261,6 @@ class CI_Image_lib { * This function lets us re-proportion the width/height * if users choose to maintain the aspect ratio when resizing. * - * @access public * @return void */ public function image_reproportion() @@ -1318,7 +1301,6 @@ class CI_Image_lib { * * A helper function that gets info about the file * - * @access public * @param string * @return mixed */ @@ -1376,7 +1358,6 @@ class CI_Image_lib { * 'new_height' => '' * ); * - * @access public * @param array * @return array */ @@ -1424,7 +1405,6 @@ class CI_Image_lib { * $array['ext'] = '.jpg'; * $array['name'] = 'my.cool'; * - * @access public * @param array * @return array */ @@ -1441,7 +1421,6 @@ class CI_Image_lib { /** * Is GD Installed? * - * @access public * @return bool */ public function gd_loaded() @@ -1462,7 +1441,6 @@ class CI_Image_lib { /** * Get GD version * - * @access public * @return mixed */ public function gd_version() @@ -1483,7 +1461,6 @@ class CI_Image_lib { /** * Set error message * - * @access public * @param string * @return void */ @@ -1515,7 +1492,6 @@ class CI_Image_lib { /** * Show error messages * - * @access public * @param string * @return string */ -- cgit v1.2.3-24-g4f1b From 64dbdfb60e0556177061db2eecdf899111ae4ac9 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 30 Dec 2011 14:14:07 +0200 Subject: Added support for 3-length hex color values format and a number of validation improvements --- system/libraries/Image_lib.php | 87 ++++++++++++++++++++++++------------------ 1 file changed, 49 insertions(+), 38 deletions(-) (limited to 'system') diff --git a/system/libraries/Image_lib.php b/system/libraries/Image_lib.php index 2737503a8..d4fb51923 100644 --- a/system/libraries/Image_lib.php +++ b/system/libraries/Image_lib.php @@ -67,8 +67,8 @@ class CI_Image_lib { public $wm_padding = 0; // Padding around text public $wm_hor_offset = 0; // Lets you push text to the right public $wm_vrt_offset = 0; // Lets you push text down - public $wm_font_color = '#ffffff'; // Text color - public $wm_shadow_color = ''; // Dropshadow color + protected $wm_font_color = '#ffffff'; // Text color + protected $wm_shadow_color = ''; // Dropshadow color public $wm_shadow_distance = 2; // Dropshadow distance public $wm_opacity = 50; // Image opacity: 1 - 100 Only works with image @@ -85,7 +85,7 @@ class CI_Image_lib { public $create_fnc = 'imagecreatetruecolor'; public $copy_fnc = 'imagecopyresampled'; public $error_msg = array(); - public $wm_use_drop_shadow = FALSE; + protected $wm_use_drop_shadow = FALSE; public $wm_use_truetype = FALSE; /** @@ -165,7 +165,30 @@ class CI_Image_lib { { foreach ($props as $key => $val) { - $this->$key = $val; + if (property_exists($this, $key)) + { + if (in_array($key, array('wm_font_color', 'wm_shadow_color'))) + { + if ($val != '' AND preg_match('/^#?([0-9a-f]{3}|[0-9a-f]{6})$/i', $val, $matches)) + { + if (strlen($matches[1]) === 6) + { + $val = '#'.$val; + } + else + { + $val = str_split($val, 1); + $val = '#'.$val[0].$val[0].$val[1].$val[1].$val[2].$val[2]; + } + } + else + { + continue; + } + } + + $this->$key = $val; + } } } @@ -332,16 +355,6 @@ class CI_Image_lib { $this->y_axis = ($this->y_axis == '' OR ! is_numeric($this->y_axis)) ? 0 : $this->y_axis; // Watermark-related Stuff... - if ($this->wm_font_color != '' AND strlen($this->wm_font_color) === 6) - { - $this->wm_font_color = '#'.$this->wm_font_color; - } - - if ($this->wm_shadow_color != '' AND strlen($this->wm_shadow_color) === 6) - { - $this->wm_shadow_color = '#'.$this->wm_shadow_color; - } - if ($this->wm_overlay_path != '') { $this->wm_overlay_path = str_replace("\\", "/", realpath($this->wm_overlay_path)); @@ -351,6 +364,10 @@ class CI_Image_lib { { $this->wm_use_drop_shadow = TRUE; } + elseif ($this->wm_use_drop_shadow == TRUE AND $this->wm_shadow_color == '') + { + $this->wm_use_drop_shadow = FALSE; + } if ($this->wm_font_path != '') { @@ -982,21 +999,6 @@ class CI_Image_lib { // Fetch source image properties $this->get_image_properties(); - // Set RGB values for text and shadow - $this->wm_font_color = str_replace('#', '', $this->wm_font_color); - $this->wm_shadow_color = str_replace('#', '', $this->wm_shadow_color); - - $R1 = hexdec(substr($this->wm_font_color, 0, 2)); - $G1 = hexdec(substr($this->wm_font_color, 2, 2)); - $B1 = hexdec(substr($this->wm_font_color, 4, 2)); - - $R2 = hexdec(substr($this->wm_shadow_color, 0, 2)); - $G2 = hexdec(substr($this->wm_shadow_color, 2, 2)); - $B2 = hexdec(substr($this->wm_shadow_color, 4, 2)); - - $txt_color = imagecolorclosest($src_img, $R1, $G1, $B1); - $drp_color = imagecolorclosest($src_img, $R2, $G2, $B2); - // Reverse the vertical offset // When the image is positioned at the bottom // we don't want the vertical offset to push it @@ -1075,16 +1077,25 @@ class CI_Image_lib { break; } - // Add the text to the source image - if ($this->wm_use_truetype AND $this->wm_use_drop_shadow) - { - imagettftext($src_img, $this->wm_font_size, 0, $x_shad, $y_shad, $drp_color, $this->wm_font_path, $this->wm_text); - imagettftext($src_img, $this->wm_font_size, 0, $x_axis, $y_axis, $txt_color, $this->wm_font_path, $this->wm_text); - } - elseif ($this->wm_use_drop_shadow) + if ($this->wm_use_drop_shadow) { - imagestring($src_img, $this->wm_font_size, $x_shad, $y_shad, $this->wm_text, $drp_color); - imagestring($src_img, $this->wm_font_size, $x_axis, $y_axis, $this->wm_text, $txt_color); + // Set RGB values for text and shadow + $txt_color = str_split(substr($this->wm_font_color, 1, 6)); + $txt_color = imagecolorclosest($src_img, hexdec($txt_color[0]), hexdec($txt_color[1]), hexdec($txt_color[2])); + $drp_color = str_split(substr($this->wm_shadow_color, 1, 6)); + $drp_color = imagecolorclosest($src_img, hexdec($drp_color[0]), hexdec($drp_color[2]), hexdec($drp_color[3])); + + // Add the text to the source image + if ($this->wm_use_truetype) + { + imagettftext($src_img, $this->wm_font_size, 0, $x_shad, $y_shad, $drp_color, $this->wm_font_path, $this->wm_text); + imagettftext($src_img, $this->wm_font_size, 0, $x_axis, $y_axis, $txt_color, $this->wm_font_path, $this->wm_text); + } + else + { + imagestring($src_img, $this->wm_font_size, $x_shad, $y_shad, $this->wm_text, $drp_color); + imagestring($src_img, $this->wm_font_size, $x_axis, $y_axis, $this->wm_text, $txt_color); + } } // Output the final image -- cgit v1.2.3-24-g4f1b From a948f07bde83e149d9fc75e9aaf7260efba39b55 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 30 Dec 2011 14:22:50 +0200 Subject: Remove a str_split() from the previous commit - we can access string characters like we do in an indexed array anyways --- system/libraries/Image_lib.php | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) (limited to 'system') diff --git a/system/libraries/Image_lib.php b/system/libraries/Image_lib.php index d4fb51923..72621c9b4 100644 --- a/system/libraries/Image_lib.php +++ b/system/libraries/Image_lib.php @@ -171,15 +171,7 @@ class CI_Image_lib { { if ($val != '' AND preg_match('/^#?([0-9a-f]{3}|[0-9a-f]{6})$/i', $val, $matches)) { - if (strlen($matches[1]) === 6) - { - $val = '#'.$val; - } - else - { - $val = str_split($val, 1); - $val = '#'.$val[0].$val[0].$val[1].$val[1].$val[2].$val[2]; - } + $val = (strlen($matches[1]) === 6) ? '#'.$val : '#'.$val[0].$val[0].$val[1].$val[1].$val[2].$val[2]; } else { -- cgit v1.2.3-24-g4f1b From 1d97f291fa1988794b0e6ac2b90a0b1f8c83df25 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 30 Dec 2011 14:24:59 +0200 Subject: Another fix on a previous commit from this request --- system/libraries/Image_lib.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'system') diff --git a/system/libraries/Image_lib.php b/system/libraries/Image_lib.php index 72621c9b4..609cb7f98 100644 --- a/system/libraries/Image_lib.php +++ b/system/libraries/Image_lib.php @@ -171,7 +171,8 @@ class CI_Image_lib { { if ($val != '' AND preg_match('/^#?([0-9a-f]{3}|[0-9a-f]{6})$/i', $val, $matches)) { - $val = (strlen($matches[1]) === 6) ? '#'.$val : '#'.$val[0].$val[0].$val[1].$val[1].$val[2].$val[2]; + $val = (strlen($matches[1]) === 6) ? + '#'.$matches[1] : '#'.$matches[1][0].$matches[1][0].$matches[1][1].$matches[1][1].$matches[1][2].$matches[1][2]; } else { -- cgit v1.2.3-24-g4f1b From 665af0cbb98372f8521298aafcde9e0c9023123e Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 30 Dec 2011 14:39:29 +0200 Subject: Some alignment for readability --- system/libraries/Image_lib.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'system') diff --git a/system/libraries/Image_lib.php b/system/libraries/Image_lib.php index 609cb7f98..7beac0ba0 100644 --- a/system/libraries/Image_lib.php +++ b/system/libraries/Image_lib.php @@ -171,8 +171,9 @@ class CI_Image_lib { { if ($val != '' AND preg_match('/^#?([0-9a-f]{3}|[0-9a-f]{6})$/i', $val, $matches)) { - $val = (strlen($matches[1]) === 6) ? - '#'.$matches[1] : '#'.$matches[1][0].$matches[1][0].$matches[1][1].$matches[1][1].$matches[1][2].$matches[1][2]; + $val = (strlen($matches[1]) === 6) + ? '#'.$matches[1] + : '#'.$matches[1][0].$matches[1][0].$matches[1][1].$matches[1][1].$matches[1][2].$matches[1][2]; } else { -- cgit v1.2.3-24-g4f1b