2010-06-29 43 views
1

我正在創建一個腳本,它創建一個縮略圖,然後將水印原始圖像,但腳本只創建縮略圖並跳過水印。水印和縮略圖 - Codeigniter

$image = $data['json']->{'file_name'}; 
    $data['account'] = $account; 

    $this->_insertintodb($account, $image); 

    //Settings to create thumbnail 
    $config['source_image'] = $data['json']->{'file_path'}; 
    $config['create_thumb'] = TRUE; 
    $config['maintain_ratio'] = TRUE; 
    $config['width'] = 125; 
    $config['height'] = 125; 
    $this->image_lib->initialize($config); 

    if($this->image_lib->resize()) { 
    $prep_thumb = explode('.', $image); 
    $thumb = $prep_thumb[0] . '_thumb.' . $prep_thumb[1]; 
    $this->_moveimage($thumb, $account, TRUE); 
    } 

    $this->image_lib->clear(); 

    //Settings to create watermark overlay 
    $config = array(); 
    $config['source_image'] = $data['json']->{'file_path'}; 
    $config['wm_type'] = 'overlay'; 
    $config['wm_overlay_path'] = getcwd() . '/design/overlay_watermark_transparent.png'; 
    $config['wm_vrt_alignment'] = 'middle'; 
    $config['wm_hor_alignment'] = 'center'; 

    $this->image_lib->initialize($config); 

    if(!$this->image_lib->watermark()){ 
    echo $this->image_lib->display_errors(); 
    } 

    $this->image_lib->clear(); 

任何想法,爲什麼這不能正常工作?

回答

0

你有需要的依賴關係嗎?從http://www.codeignitor.com/user_guide/libraries/image_lib.html

注意:只有使用GD/GD2庫才能使用水印。另外,即使支持其他庫,爲了使腳本計算圖像屬性,GD也是必需的。但是,圖像處理將在您指定的庫中執行。

看起來您需要安裝GD,然後將其作爲您的codeigniter圖像處理庫。

+0

,如果你單獨做任何一個它的工作原理,但是當你嘗試做在一起不 – dennismonsewicz 2010-06-29 22:24:04

+0

我不太明白你的說法。你能澄清一下嗎? – hundredwatt 2010-06-29 23:20:49

+0

如果你自己運行水印功能,它就可以工作。如果你自己運行縮略圖功能,它就可以工作。但是如果你把它們放在一起,它就不會。 – dennismonsewicz 2010-06-30 03:05:25

1

如果水印不工作,你有所需的依賴,你應該在這裏收到錯誤消息:

if(!$this->image_lib->watermark()){ 
    echo $this->image_lib->display_errors(); 
} 

無論這些錯誤可以幫助您更比我們,或者我們也可以幫你更多的是你給我們提供這些。

0

這是爲我工作:

/** 
* public function wmimg_thumb($source_image) 
* 
* Creating water marked - thumb 
* 
* @param string $source_image : The remaining path to the source image, after FCPATH. 
* 
*/ 
function wmimg_thumb($source_image) 
{ 
    if (!file_exists($source_image)) 
    { 
     return "$source_image not exists!"; 
    } 

    $this->load->library('image_lib'); 
    $config['maintain_ratio'] = TRUE; 
    $config['width'] = 145; 
    $config['height'] = 120; 
    $config['image_library'] = 'gd2'; 
    $config['source_image'] = FCPATH . $source_image; 
    $config['create_thumb'] = TRUE; 

    $this->image_lib->initialize($config); 

    if ($this->image_lib->resize()) 
    { 
     $this->image_lib->clear(); 
     // if thumb is created, calculating the name of thumb. 
     $thumb_image = str_replace('.', '_thumb.', $source_image); 

     $img_config['wm_type'] = 'overlay'; 
     $img_config['wm_overlay_path'] = FCPATH . '/assets/watermark.png'; 
     $img_config['wm_x_transp'] = 20; 
     $img_config['wm_y_transp'] = 10; 
     $img_config['wm_opacity'] = 50; 
     $img_config['wm_vrt_alignment'] = 'bottom'; 
     $img_config['wm_hor_alignment'] = 'center'; 
     $img_config['source_image'] = FCPATH . $thumb_image; 
     $this->image_lib->initialize($img_config); 
     $this->image_lib->watermark(); 
    } 

    if (file_exists(FCPATH . $thumb_image)) 
    { 
     // Up to here, there is 2 images, $source_image_thumb.extension and $source_image_thumb_thumb.extension. 
     // Deleting the 1st one, which has no watermarking. And renaming the 2nd one, which will be watermarked. 
     unlink($thumb_image); 
     $wm_thumbname = str_replace('.', '_thumb.', $thumb_image); 
     rename(FCPATH . $wm_thumbname, FCPATH . $thumb_image); 
    } 
}