Duplicate a image variable GD library

霸气de小男生 提交于 2019-12-01 21:17:04

问题


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?


回答1:


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.




回答2:


You can use imagecopy




回答3:


Use This:

function cloneImg($img){
    //get dimensions
    $w = imagesx($img);
    $h = imagesy($img);
     //copy process
    $copy = imagecreatetruecolor($w, $h);
    imagecopy($copy, $img, 0, 0, 0, 0, $w, $h);

    return $copy;

  }


来源:https://stackoverflow.com/questions/3047852/duplicate-a-image-variable-gd-library

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!