2016-05-01 2 views
0

У меня есть следующий image_helperCodeigniter Image Resize/Matermarking сбой при вызове в серии

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; 
    } 
    } 
} 

Как вы можете видеть на второй функции я называю первым, чтобы изменить размер наложения перед его применением к изображению.

Один, свойства измененного оверлея все еще говорят, что это оригинальный размер. Я не думаю, что это верно, так как размер файла меняется. Зачем? Изменяется ли изменение размера отображаемых свойств?

Во-вторых, когда вызывается изменение размера, водяной знак не работает и не создается водяных знаков. Журналы говорят: «Ваш сервер не поддерживает функцию GD, необходимую для обработки этого типа изображения». Когда страница обновляется (изменение размера не вызывается из-за того, что существует файл с изменением размера) создается водяное изображение.

Как вы можете видеть, я очищаю конфигурацию, чтобы обеспечить загрузку только тех, которые необходимы, когда функция реализована, и с помощью функции clear() для сброса библиотеки.

Как вы можете видеть, я инициализирую и перезагружаю библиотеку, как в этом вопросе. CodeIgniter/PHP/GD2 Image Manipulation is playing up

Я использую библиотеку Codeigniter 2 image_lib.

Заранее благодарим за любые идеи и пути устранения неполадок. У меня нет идей для тестирования.

ответ

0

Решено - изменено значение class_exists(), возвращающее значение false, в значение $ CI-> load-> is_loaded(), которое заставило библиотеку инициализироваться вместо загрузки.