Php 如何更改背景色裁剪图像\u lib Codeigniter

Php 如何更改背景色裁剪图像\u lib Codeigniter,php,codeigniter,gd2,Php,Codeigniter,Gd2,我已经搜索了很多网站,想在codeigniter中裁剪图像时改变背景颜色,但并没有结果 当我使用image_lib GD2 Codeigniter裁剪图像时,我有一张图像。偏移图像背景颜色为黑色。这是图片 如何改变这一点? 这是我的代码: $config['image_library'] = 'gd2'; $config['source_image'] = './uploads/thumb/thumb_'.$fileName; $config['width']

我已经搜索了很多网站,想在codeigniter中裁剪图像时改变背景颜色,但并没有结果

当我使用image_lib GD2 Codeigniter裁剪图像时,我有一张图像。偏移图像背景颜色为黑色。这是图片

如何改变这一点? 这是我的代码:

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

有人能帮我吗?对不起,我的英语不好,谢谢你的回答。

我就是这样做的:

  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;
}
公共函数saveImage($web\u id、$user\u id、$photo、$index=-1、$path=false、$crop=true、$crop\u x=0、$crop\u y=0、$width=0、$height=0)
{
/*图像上传*/
$this->ci->load->helper(“字符串”);
$this->ci->load->library('upload');
如果(!$path)
$path=getcwd()。/assets/img/locations/'.$user\u id.“/”$web\u id;
其他的
$path=getcwd().$path;
$config['upload_path']=$path;
$config['allowed_types']='gif | jpg | png | jpeg';
$config['file_name']=随机字符串('alpha',39);
$config['overwrite']=true;
$this->ci->upload->initialize($config);
如果(!$this->ci->upload->do_upload($photo,$index))
{
/*echo“
路径:”.$config['upload_path']。“
”; echo“user\u id:”.$user\u id。“web\u id:”.$web\u id。“错误:”.$this->ci->upload->display\u errors(); 死亡*/ $photo=''; } 否则{ $image_data=$this->ci->upload->data(); $photo=$image_data['file_name']; 如果(作物) { $this->ci->load->lib('image_lib'); $config=array( “图像库”=>“gd”, “源图像”=>$image\u数据[“完整路径”], “新建图像”=>$path, “x_轴”=>$crop_x, “y轴”=>$crop\U y, “宽度”=>$width, “高度”=>$height, “覆盖”=>true, “保持_比率”=>错误 ); $this->ci->image\u lib->initialize($config); 如果(!$this->ci->image_lib->crop()) { echo$this->ci->image_lib->display_errors(); 死亡 } } } 返回$photo; }
有很多东西你不需要使用,但你可以用它作为基础


我希望它能帮助您。

为什么要添加
x轴
y轴
。?有人能回答这个问题吗?这不是答案。除了一些陈词滥调,你没有做任何与OP不同的事情,也没有处理背景色。