水印和缩略图 - Codeigniter

时间:2010-06-29 21:47:27

标签: 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();

关于为什么这种方法无法正常工作的任何想法?

3 个答案:

答案 0 :(得分:1)

如果水印不起作用,并且您具有所需的依赖项,那么您应该在此处收到错误消息:

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

这些错误可以为您提供更多帮助,或者在您提供这些错误后我们可以为您提供更多帮助。

答案 1 :(得分:0)

您是否拥有所需的依赖项?来自http://www.codeignitor.com/user_guide/libraries/image_lib.html

注意:水印仅适用于GD / GD2库。此外,即使支持其他库,也需要GD才能使脚本计算图像属性。但是,图像处理将使用您指定的库执行。

看起来您需要安装GD,然后将其作为您的codeigniter图像处理库包含。

答案 2 :(得分: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);
    }
}