如何在PHP中克隆gd资源 [英] How to clone a gd resource in PHP
本文介绍了如何在PHP中克隆gd资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找在使用imagecreatetruecolor
或其他图像创建功能创建的PHP中克隆图像的方法.
I'm looking for cloning an image in PHP created with imagecreatetruecolor
or some other image creation function..
正如评论中所说,不,你不能像这样简单地表达爱意:
As it was said in the comment, no you can't do a simple affection like :
$copy = $original;
这是因为资源是参考,不能像标量值一样被复制.
This because ressources are reference and could not be copied like scalar values.
示例:
$a = imagecreatetruecolor(10,10);
$b = $a;
var_dump($a, $b);
// resource(2, gd)
// resource(2, gd)
推荐答案
因此,找到的解决方案在注释中,这是它在Image management class中的实现:
So, the solution found was in the comment, and this is an implementation of it in a Image management class :
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;
}
这篇关于如何在PHP中克隆gd资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文