1

我有以下 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;
    }
  }
}

正如您在第二个函数中看到的那样,我调用第一个函数来调整叠加层的大小,然后再将其应用于图像。

一,调整后的叠加层的属性仍然说它是原始大小。我不认为这是真的,因为文件大小发生了变化。为什么?调整大小不会改变显示的属性吗?

其次,当调用resize时,加水印失败,没有水印图像被创建。日志显示“您的服务器不支持处理此类图像所需的 GD 功能。”。刷新页面时(不调用调整大小,因为调整大小覆盖文件存在)水印图像被创建。

如您所见,我正在清除配置以确保在实现函数时仅加载所需的配置,并使用 clear() 函数重置库。

如您所见,我正在初始化与重新加载此问题中的库。CodeIgniter/PHP/GD2 Image Manipulation 正在兴起

我正在使用 Codeigniter 2 image_lib 库。

在此先感谢您提供任何和所有想法/故障排除途径。我没有想法去测试。

4

1 回答 1

0

已解决 - 将返回 false 的 class_exists() 更改为 $CI->load->is_loaded(),这会导致库被初始化而不是加载。

于 2016-05-01T17:39:47.260 回答