I have the following image_helper
function resize($img_path_name, $new_path_name = NULL, $new_width = NULL, $new_height = NULL){
$CI =& get_instance();
$config['image_library'] = 'GD2';
$config['source_image'] = $img_path_name;
$config['new_image'] = $new_path_name;
$config['dynamic_output'] = FALSE;
$config['quality'] = '90%';
$config['create_thumb'] = FALSE;
# if either new_width or new_height is null simply maintain ratio
if( is_null( $new_width ) || is_null( $new_width ) ){
$config['maintain_ratio'] = TRUE;
}else{
$config['maintain_ratio'] = FALSE;
}
$config['width'] = $new_width;
$config['height'] = $new_height;
$config['master_dim'] = 'auto'; # auto, width, height
foreach( $config as $fld=>$val ){
log_message('error', 'resize config: ' . $fld . ':' . $val);
}
if( class_exists( 'image_lib' ) ){
$CI->image_lib->initialize($config);
}else{
$CI->load->library('image_lib', $config);
}
# Clear the config
foreach( $config as $fld=>$val ){
$config[$fld] = '';
}
# returns 1 or 0
$result = $CI->image_lib->resize();
$CI->image_lib->clear();
if( $result ){
return $result;
}else{
log_message('error', $CI->image_lib->display_errors() );
}
}
function wm_img($img_path_name, $new_path_name = NULL, $wm_text = NULL){
$CI =& get_instance();
if( ! file_exists( $img_path_name ) ){
log_message('error', 'Original file not found. ' . $img_path_name);
return 0;
}else{
#log_message('error', 'Original file found. ' . $img_path_name);
list($width, $height, $type, $attr) = getimagesize($img_path_name);
$arr_img = explode( '/', $img_path_name );
$img_name = $arr_img[(sizeof( $arr_img ) - 1)];
#log_message('error', 'img_name: ' . $img_name);
if( ! is_null( $new_path_name ) ){
# $new_path_name may only be a path or only a file name or both
if( strpos( $new_path_name, '/' ) === FALSE ){
# no slashes => file name only
# new watermarked img will be created in the same location but with new name
$path = explode( '/', $new_img_name );
foreach( $path as $itm ){
$wm_img .= $itm;
}
$wm_img .= $img_name;
}elseif( strrpos( $new_path_name, '/', -0 ) ){
# ending in / => a path
# new watermarked img will be created in this path with orginial name
$wm_img = $new_path_name . $img_name;
}else{
# must be full path and filename
# new watermarked img will be created in new path with new name
$wm_img = $new_path_name;
}
# path and name of img to be created
$wm_img = $new_path_name . $img_name;
}else{
$wm_img = $new_path_name . $img_name;
}
if( is_null( $wm_text ) || empty( $wm_text ) ){
# Overlay Prefernces
# Relative or Absolute path
# Transparent PNG or GIF
## Need to resize the Overlay to match the image
$new_overlay = 'images/wm/overlay_' . $width . 'x' . $height . '.png';
if( ! file_exists( $new_overlay ) ){
try{
# use images_helper->resize because it sets up the config
$resize_result = resize(WM_OVERLAY, $new_overlay, $width, $height);
}catch ( Exception $e ){
log_message('error', $e->getMessage());
}
}
$config['wm_type'] = 'overlay';
if( file_exists( $new_overlay ) ){
$config['wm_overlay_path'] = $new_overlay;
$config['wm_vrt_alignment'] = 'top'; # top,middle,bottom
$config['wm_hor_alignment'] = 'left'; # left,center,right
$config['wm_vrt_offset'] = 0; # num pixels from the alignment side
$config['wm_hor_offset'] = 0; # num pixels from the alignment side
}else{
$config['wm_overlay_path'] = WM_OVERLAY;
$config['wm_vrt_alignment'] = 'bottom'; # top,middle,bottom
$config['wm_hor_alignment'] = 'center'; # left,center,right
$config['wm_vrt_offset'] = $height/4; # num pixels from the alignment side
$config['wm_hor_offset'] = ''; # num pixels from the alignment side
}
$config['wm_opacity'] = 50; # 1 not transparent - 100 fully transparent
# coordinates of the pixel color on the overlay to make transparent
$config['wm_x_transp'] = 4;
$config['wm_y_transp'] = 4;
}else{
$config['wm_type'] = 'text';
# Text Preferences
$config['wm_text'] = $wm_text;
$config['wm_font_path'] = './system/fonts/texb.ttf';
$config['wm_font_size'] = '24';
$config['wm_font_color'] = '000000'; # black
$config['wm_shadow_color'] = 'ffffff'; # white
$config['wm_shadow_distance'] = '5';
$config['wm_vrt_alignment'] = 'bottom'; # top,middle,bottom
$config['wm_hor_alignment'] = 'center'; # left,center,right
$config['wm_vrt_offset'] = $height/4; # num pixels from the alignment side
$config['wm_hor_offset'] = ''; # num pixels from the alignment side
}
# because resize clears $config, these have to be after it
# Both Types
# Relative or absolute path to file
$config['image_library'] = 'GD2';
$config['new_image'] = $new_path_name;
$config['source_image'] = $img_path_name;
$config['dynamic_output'] = FALSE;
$config['quality'] = '90%';
# positioning
$config['padding'] = 0; # a number in pixels
foreach( $config as $fld=>$val ){
log_message('error', 'wm config: ' . $fld . ':' . $val);
}
if( class_exists( 'image_lib' ) ){
$CI->image_lib->initialize($config);
}else{
$CI->load->library('image_lib', $config);
}
# Clear the config
foreach( $config as $fld=>$val ){
$config[$fld] = '';
}
# returns 1 or 0
$result = $CI->image_lib->watermark();
if( $result ){
# watermarking worked
if( file_exists( $wm_img ) ){
log_message('error', 'Watermark ' . $result . '. New File found: ' . $wm_img);
return 1;
}else{
log_message('error', 'Watermark ' . $result . '. New File not found: ' . $wm_img);
return 1;
}
}else{
log_message('error', 'Watermark Failed ' . $result );
log_message('error', $CI->image_lib->display_errors() );
return 0;
}
}
}
As you can see in the second function I call the first to resize the overlay before applying it to the image.
One, the resized overlay's properties still say it is the original size. I don't think this is true since the file size changes. Why? Does the resizing not change the displayed properties?
Second, when the resize is called, the watermarking fails and no watermarked image is created. The logs say "Your server does not support the GD function required to process this type of image.". When the page is refreshed (resize is not called because the resize overlay file exists) the watermarked image is created.
As you can see I am clearing the config to ensure only those needed are loaded when the function is implemented and using the clear() function to reset the library.
As you can also see I am initializing vs reloading the library as found in this question. CodeIgniter/PHP/GD2 Image Manipulation is playing up
I am using the Codeigniter 2 image_lib library.
Thanks in advance for any and all ideas / troubleshooting paths. I am out of ideas to test.
Solved - changed the class_exists(), which was returning false, to $CI->load->is_loaded() which caused the library to be initialized instead of loaded.