我正在寻找使用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;
}