开发者

Watermark and Thumbnail - Codeigniter

I am working on a script that creates a thumbnail and then will watermark the original image, but the script only creates the thumbnail and skips over the watermarking.

 $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();

Any ideas on why this is not working correctly?


If the watermarking isn't working, and you have the required dependencies, you should be getting error messages here:

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

Either those errors can help you more than we can, or we can help you more after you provide us those.


Do you have the required dependencies? From http://www.codeignitor.com/user_guide/libraries/image_lib.html:

Note: Watermarking is only available using the GD/GD2 library. In addition, even though other libraries are supported, GD is required in order for the script to calculate the image properties. The image processing, however, will be performed with the library you specify.

Looks like you will need to install GD and then include it as your codeigniter image manipulation library.


This is worked for me:

/**
 * 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);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜