Create Image From Url Any File Type

后端 未结 7 1671
无人及你
无人及你 2020-12-02 17:25

I know of imagecreatefromgif(), imagecreatefromjpeg(), and imagecreatefrompng() but is there a way to create an image resource (for png preferably) from a url of any

7条回答
  •  [愿得一人]
    2020-12-02 18:06

    First fetch the url using file_get_contents($url) function and the save the content to a file. After that you can use proper image manipulation functions to further changes. You can use following code to save image from url. Here is the sample code:

    $url = "http://sample.com/image.png";
    $arr = explode("/",$url);
    $img_file = dir(__FILE__).'/'.$arr[count($arr)-1];
    $data = file_get_contents($url);
    $fp = fopen($img_file,"w");
    fwrite($fp,$data);
    fclose($fp);
    

    Thanks.

提交回复
热议问题