我正在寻找使用 PHPimagecreatetruecolor
或其他图像创建功能创建的克隆图像..
正如评论中所说,不,你不能做一个简单的感情,比如:
$copy = $original;
这是因为资源是引用的,不能像标量值一样被复制。
例子 :
$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, 0, 0, $trans_index);
}
}
//Create the Clone!!
imagecopy($clone, $img, 0, 0, 0, 0, $w, $h);
return $clone;
}
因此,找到的解决方案在评论中,这是它在图像管理类中的实现:
public function __clone() {
$original = $this->_img;
$copy = imagecreatetruecolor($this->_width, $this->_height);
imagecopy($copy, $original, 0, 0, 0, 0, $this->_width, $this->_height);
$this->_img = $copy;
}
更简单的代码,一行,处理透明度:
function clone_img_resource($img) {
return imagecrop($img, array('x'=>0,'y'=>0,'width'=>imagesx($img),'height'=>imagesy($img)));
}
好的,所以我需要以透明度调整大小,我看到这个问题的答案和我的问题的答案相似但不完全相同,所以我将发布我的答案。
我想指出,我修改了HERE的答案以回答我的个人需求和 OP 的问题。(这是关于保持第一张也是唯一一张将被复制/克隆/或调整大小的图像的透明度)
此外:透明度颜色将与该资源保持一致,并将其应用于复制到其上的其他透明图像。我使用的解决方法是在我的示例中使用imagecopy()复制克隆的文件以清除分配的透明度颜色。
$im = imagecreatefromstring(file_get_contents('I/image.png')); //
$imw = imagesx($im); /*w*/ $imh = imagesy($im); /*h*/
$tmpIm = imagecreatetruecolor($imw, $imh); // This is black by default
imagealphablending($tmpIm, false); imagesavealpha($tmpIm, true); // Set these as usual
$wht = imagecolorallocatealpha($tmpIm, 255, 255, 255, 127); // white
imagecolortransparent($tmpIm, $wht); // Setting this seems to be unsettable and will make this color transparent on all images later coppied to it
imagealphablending($tmpIm, false); imagesavealpha($tmpIm, true); // Set these as usual
// That is it... As long as you set the transparency of the background before you copy it should work. That means now at least some of the other answers can work now.
imagecopyresampled($tmpIm, $im, 0, 0, 0, 0, $imw, $imh, $imw, $imh); // Copy or resize whatever floats your boat. The above process is what does the trick.
$newIm = imagecreatetruecolor($imw, $imh); // Begin Clone
imagealphablending($newIm, false); imagesavealpha($newIm, true); // Set these as usual
imagecopyresampled($newIm, $tmpIm, 0, 0, 0, 0, $imw, $imh, $imw, $imh); // Clearing the tranparent color previously set (Workaround)
imagedestroy($tmpIm);
header('Content-Type: image/png');
imagepng($newIm); // see I told you.. Or he did... Whatever, it works...
所以对于这个功能,它会:
function F_img_clone($im,$trgt,$id){
$imw = imagesx($im); $imh = imagesy($im); // Height and Width of Original
if(empty($trgt) === true){
$rsiw = $imw; $rsih = $imh; // if there are no H or W changes
}else{ // if there are H or W changes
if( $imw > $imh ) {
$pcntg = ( ($trgt??1920) / $imw );
}else{
$pcntg = ( ($trgt??1920) / $imh );
}
$rsiw = round($imw * $pcntg); $rsih = round($imh * $pcntg);
}
$ni = imagecreatetruecolor($rsiw, $rsih);// Begin the background
imagealphablending($ni, false); imagesavealpha($ni, true); // To keep the alpha channel
$wht = imagecolorallocatealpha($ni, 255, 255, 255, 127); // white
imagecolortransparent($ni, $wht); // Setting this seems to be unsettable and will make this color transparent on all images later coppied to it
imagealphablending($ni, false); imagesavealpha($ni, true);
imagecopyresampled($ni, $im, 0, 0, 0, 0, $rsiw, $rsih, $imw, $imh);
imagealphablending($ni, true); imagesavealpha($ni, false);
imagedestroy($im);
$imw = imagesx($ni); /*h*/ $imh = imagesy($ni); /*w*/
${$id} = imagecreatetruecolor($imw, $imh); // Begin Clone
imagealphablending(${$id}, false); imagesavealpha(${$id}, true); // Set these as usual
imagecopyresampled(${$id}, $ni, 0, 0, 0, 0, $imw, $imh, $imw, $imh); // Clearing the tranparent color previously set (Workaround)
imagedestroy($ni);
return ${$id};
}
所以将其用作:
$image = imagecreatefromstring(file_get_contents('I/image.png')); //
$clone = F_img_clone($image,'','clone');
原始的 $image 将完好无损。