我正在寻找使用imagecreatetruecolor
或其他图像创建功能创建的PHP克隆图像。
正如评论中所说,不,你不能做一个简单的感情,如:
$copy = $original;
这是因为ressources是引用,无法像标量值那样被复制。
示例:
$a = imagecreatetruecolor(10,10);
$b = $a;
var_dump($a, $b);
// resource(2, gd)
// resource(2, gd)
答案 0 :(得分:7)
这个小功能将克隆图像资源,同时保留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;
}
答案 1 :(得分:6)
所以,找到的解决方案是在评论中,这是它在图像管理类中的实现:
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;
}
答案 2 :(得分:2)
更简单的代码,一行,处理透明度:
function clone_img_resource($img) {
return imagecrop($img, array('x'=>0,'y'=>0,'width'=>imagesx($img),'height'=>imagesy($img)));
}
答案 3 :(得分:0)
好,所以我需要透明地调整大小,我看到这个问题的答案和我的问题的答案如何相似但又不完全相同,所以我将发布答案。
我想指出的是,我修改了HERE的答案,以回答我的个人需求和OP的问题。 (这是为了保持1的透明度,并且仅保留将被复制/克隆/或调整大小的图像)
此外:透明色将与该资源一起保留,并将其应用于复制到该资源上的其他透明图像。我使用的解决方法是在示例中使用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将保持不变。