Page 1 of 1

PHP 5 passing by reference, copying, and cloning question

Posted: Wed Sep 20, 2006 7:59 pm
by neophyte
I'm working with PHP5 which passes by reference by default and not by copying.

I have two image resources variables: imgResource and imgNew.

After the image is resized I want to do a little clean up and copy the newly resized image resource from imgNew to imgResource and then destroy imgNew.

However the following code seems to pass by reference.

Thus when I destroy imgNew I also destroy imgResource.

I tried Clone() and (clone) but imgResource isn't an object neither is imgNew and it returns an error.

My question:

What are my options for copying?

Here's the snippet in question.

Code: Select all

if (@imagecopyresampled ( $this->img->getImageNew(), $this->img->imgResource, 0, 0, 0, 0,  $newWidth, $newHeight, $this->img->getImageWidth(), $this->img->getImageHeight() )){
					$this->img->imgResource =  $this->img->imgNew;
					imagedestroy($this->img->imgNew);
				}
I've got the idea of putting the image resource inside an object inside the object. But I'm just wondering If I have other options.

Thanks

Posted: Thu Sep 21, 2006 3:50 am
by ibbo
When you say pass by reference you are refering to something like

$this->method(&$var); which has been depreciated in php5 and would complian to you about your doing so.

Does it not work by copying your img into local variable and not another class variable like it appears? Whats the point of dropping a class img var and copying it into another var of the same object.

Thats appears to be doing more work and using more resources.

Ibbo

Posted: Thu Sep 21, 2006 9:02 am
by neophyte
ibbo wrote:When you say pass by reference you are refering to something like

$this->method(&$var); which has been depreciated in php5 and would complian to you about your doing so.
PHP5 assignments ('=') are made by reference by default. Where as in PHP4 it is copy by default.

After I do this:

Code: Select all

$this->img->imgResource =  $this->img->imgNew;
Both images properties have the same resource number.
ibbo wrote:Does it not work by copying your img into local variable and not another class variable like it appears? Whats the point of dropping a class img var and copying it into another var of the same object.
Imagine you have a completed imagecopyresampled() resizing an image. You now have two image resources. Now you want to perform additional transformations to the new resized image such as a watermark and then add some type. You have to keep track of which resource is which for final output and which resource to apply the new transformations to. To me it is better to always have the resource to work on in one place hence the copy and destruction. Otherwise you'd end up with some sort of array with: resource 1, resource2, resource3 and so forth.

Posted: Fri Sep 22, 2006 4:46 am
by ibbo
"Both images properties have the same resource number"

Thats interesting as I would have laid money on them been assigned a seperate address block in memory when created.

"Imagine you have a completed imagecopyresampled() resizing an image. You now have two image resources. Now you want to perform additional transformations to the new resized image such as a watermark and then add some type. You have to keep track of which resource is which for final output and which resource to apply the new transformations to. To me it is better to always have the resource to work on in one place hence the copy and destruction. Otherwise you'd end up with some sort of array with: resource 1, resource2, resource3 and so forth."

Hang on did you not say they both have the same resourceID. Is that the parent object resourceID or both of these images?

Anyway my point been is if you have an image manipulation object as I assumed. Then when creating and manipulating your image resource I would have simply created a local resource (not another image resource) to manipulate the new Image to add watermarks etc to it.

Then if needed it could be copied back to its original parent object variable and used further or destroyed (whichever).

ibbo