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

标签 php codeigniter gd2

我一直在搜索许多站点以在 codeigniter 中裁剪图像时更改背景颜色,但没有结果。

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

enter image description here

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

$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;
}

有很多东西您不需要使用,但您可以将其用作基础。

希望对你有帮助。

关于php - 如何更改背景颜色裁剪 image_lib Codeigniter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36687100/

相关文章:

php - 我可以用 PHP 提供 MP3 文件吗?

php - 使用 where in 条件从表中获取最新的 N 条记录

php - 无法使用php邮件功能发送邮件

jquery - 使用 Jeditable 保存编辑值

php - 带有 XAMPP 的 OSX : "imagegd2(): unable to open temporary file"

php - 如何阻止 GD2 在调整图像大小时洗掉颜色?

php - Magento:在查找 magento ID 时根据属性将项目添加到购物车

mySQL 在 codeigniter 中的帮助

php - 检查用户是否在线(更新每个页面访问的事件时间)

php - 如何在 CodeIgniter PHP 应用程序中加快图像大小调整?