加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

如何在PHP中克隆gd资源

发布时间:2020-12-13 18:18:02 所属栏目:PHP教程 来源:网络整理
导读:我正在寻找使用imagecreatetruecolor或其他一些图像创建功能创建的 PHP克隆图像. 正如在评论中所说,不,你不能做一个简单的感情,如: $copy = $original; 这是因为ressources是引用,无法像标量值那样被复制. 示例: $a = imagecreatetruecolor(10,10);$b = $a
我正在寻找使用imagecreatetruecolor或其他一些图像创建功能创建的 PHP克隆图像.

正如在评论中所说,不,你不能做一个简单的感情,如:

$copy = $original;

这是因为ressources是引用,无法像标量值那样被复制.

示例:

$a = imagecreatetruecolor(10,10);
$b = $a;

var_dump($a,$b);

// resource(2,gd)

// resource(2,gd)
这个小函数将克隆图像资源,同时保留alpha通道(透明度).
function _clone_img_resource($img) {

  //Get width from image.
  $w = imagesx($img);
  //Get height from image.
  $h = imagesy($img);
  //Get the transparent color from a 256 palette image.
  $trans = imagecolortransparent($img);

  //If this is a true color image...
  if (imageistruecolor($img)) {

    $clone = imagecreatetruecolor($w,$h);
    imagealphablending($clone,false);
    imagesavealpha($clone,true);
  }
  //If this is a 256 color palette image...
  else {

    $clone = imagecreate($w,$h);

    //If the image has transparency...
    if($trans >= 0) {

      $rgb = imagecolorsforindex($img,$trans);

      imagesavealpha($clone,true);
      $trans_index = imagecolorallocatealpha($clone,$rgb['red'],$rgb['green'],$rgb['blue'],$rgb['alpha']);
      imagefill($clone,$trans_index);
    }
  }

  //Create the Clone!!
  imagecopy($clone,$img,$w,$h);

  return $clone;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读