2016-05-01 9 views
0

Ich habe folgendes image_helperCodeigniter Bild Größe ändern/Matermarking schlägt fehl, wenn in Serie mit dem Titel

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

Wie Sie in der zweiten Funktion sehen kann ich den ersten Anruf das Overlay, um die Größe, bevor es auf das Bild angewendet wird.

Eins, die Eigenschaften der Größe des Overlays sagen immer noch, dass es die Originalgröße ist. Ich glaube nicht, dass dies der Fall ist, da sich die Dateigröße ändert. Warum? Ändert die Größenänderung nicht die angezeigten Eigenschaften?

Zweitens, wenn die Größenänderung aufgerufen wird, schlägt die Wasserzeichenmarkierung fehl und es wird kein mit Wasserzeichen versehenes Bild erstellt. Die Protokolle sagen "Ihr Server unterstützt nicht die GD-Funktion, die für die Verarbeitung dieses Bildtyps erforderlich ist." Wenn die Seite aktualisiert wird (die Größe wird nicht aufgerufen, weil die Überlagerungsdatei für die Größenänderung vorhanden ist), wird das mit Wasserzeichen versehene Bild erstellt.

Wie Sie sehen können, lösche ich die Konfiguration, um sicherzustellen, dass nur diejenigen benötigt werden, die geladen werden, wenn die Funktion implementiert wird, und die Bibliothek mit der Funktion clear() zurückgesetzt wird.

Wie Sie auch sehen können Ich initialisiere vs die Bibliothek wie in dieser Frage gefunden. CodeIgniter/PHP/GD2 Image Manipulation is playing up

Ich verwende die Codeigiter 2 image_lib Bibliothek.

Vielen Dank im Voraus für alle Ideen/Fehlerbehebungswege. Ich habe keine Ideen zu testen.

Antwort

0

Gelöst - die class_exists(), die false zurückgegeben hat, wurde in $ CI-> load-> is_loaded() geändert, wodurch die Bibliothek initialisiert anstatt geladen wurde.