Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicate a image variable GD library

Tags:

php

gd

What is the easiest way to duplicate an image variable in PHP.

Normally you can simply do $varnew = $varold.

But with a GD library variable if I did the above and then edited $varnew then $varold would be effected too.

Obviously one way would be to reopen the file or to make a new image and copy it into it. Is there an easier way?

like image 650
Jigs Avatar asked Jun 15 '10 18:06

Jigs


1 Answers

The reason your try didn't work is because the variable only stores a handle (a memory pointer) to the GD image. Both $varnew and $varold will still store the same pointer, thus pointing to the exact same image in memory. You have to use imagecopy or, maybe worse, open the image from file again.

like image 181
Emil Vikström Avatar answered Nov 05 '22 09:11

Emil Vikström