du7133 2016-04-18 06:46
浏览 56

如何更改背景颜色裁剪image_lib Codeigniter

I've been searching many of site to change background color when cropping image in codeigniter but there's no result.

I have an image when I crop using image_lib GD2 Codeigniter the offset image background color is black here's the pic

enter image description here

how to change this? Here's my code:

$config['image_library']    = 'gd2';
$config['source_image']     = './uploads/thumb/thumb_'.$fileName;
$config['width']            = 200;
$config['height']           = 250;
$config['maintain_ratio']   = FALSE;
$config['x_axis']           = '20';
$config['y_axis']           = '0';

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

anyone can help me? Sorry my English is bad, thanks for answer.

  • 写回答

1条回答 默认 最新

  • dongliyan9190 2017-02-02 11:01
    关注

    This is the way I do it:

      public function saveImage($web_id, $user_id, $photo, $index = -1, $path=false, $crop = true, $crop_x = 0, $crop_y = 0, $width = 0, $height = 0)
    {
        /* Image upload */
    $this->ci->load->helper("string");
    $this->ci->load->library('upload');
    if(!$path)
          $path = getcwd().'/assets/img/locations/'.$user_id."/".$web_id;
    else
      $path = getcwd().$path;
    
    $config['upload_path'] = $path;
    $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $config['file_name'] = random_string('alpha', 39);
        $config['overwrite'] = true;
    
    
        $this->ci->upload->initialize($config);
        if (!$this->ci->upload->do_upload($photo, $index))
    {
      /*echo "<br>path: ".$config['upload_path']."<br>";
      echo "user_id: ".$user_id." web_id: ".$web_id." error: ". $this->ci->upload->display_errors();
      die;*/
    
      $photo = '';
    }
        else{
    
            $image_data = $this->ci->upload->data();
            $photo = $image_data['file_name'];
      if($crop)
      {
        $this->ci->load->library('image_lib');
        $config = array(
          'image_library'   => 'gd',
          'source_image'    => $image_data['full_path'],
          'new_image'       => $path,
          'x_axis'          => $crop_x,
          'y_axis'          => $crop_y,
          'width'           => $width,
          'height'          => $height,
          'overwrite'       => true,
          'maintain_ratio'  => false
        );
        $this->ci->image_lib->initialize($config);
        if ( ! $this->ci->image_lib->crop())
        {
          echo $this->ci->image_lib->display_errors();
          die;
        }
      }
      }
        return $photo;
    }
    

    There are many things you don´t need to use, but you can use it as a base.

    I hope it helps you.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用