xxxxxxxxxx
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;
}
xxxxxxxxxx
class MyClass {
public $property;
public $nestedObject;
// Constructor, getters and setters
public function __clone() {
$this->nestedObject = clone $this->nestedObject;
}
}
$originalObject = new MyClass();
$originalObject->property = "Hello";
$originalObject->nestedObject = new stdClass();
$originalObject->nestedObject->nestedProperty = "World";
$clonedObject = clone $originalObject;
// Modifying the cloned object
$clonedObject->property = "Hi";
$clonedObject->nestedObject->nestedProperty = "Everyone";
echo $originalObject->property; // Output: Hello
echo $originalObject->nestedObject->nestedProperty; // Output: World
echo $clonedObject->property; // Output: Hi
echo $clonedObject->nestedObject->nestedProperty; // Output: Everyone