Create Image From Url Any File Type

后端 未结 7 1677
无人及你
无人及你 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 17:57

    I use this function. It supports all types of urls and stream wrappers and all image types php can handle.

    /**
     * creates a image ressource from file (or url)
     *
     * @version: 1.1 (2014-05-02)
     *
     * $param string:    $filename                    url or local path to image file
     * @param [bool:     $use_include_path]           As of PHP 5 the FILE_USE_INCLUDE_PATH constant can be used to trigger include path search.
     * @param [resource: $context]                    A valid context resource created with stream_context_create(). If you don't need to use a custom context, you can skip this parameter by NULL
     * @param [&array:   $info]                       Array with result info: $info["image"] = imageinformation from getimagesize, $info["http"] = http_response_headers (if array was populated)
     *
     * @see: http://php.net/manual/function.file-get-contents.php
     * @see: http://php.net/manual/function.getimagesize.php
     *
     * @return bool|resource                       false, wenn aus Dateiinhalt keine gueltige PHP-Bildresource erstellt werden konnte (z.b. bei BMP-Datei)
     * @throws InvalidArgumentException                Wenn Datei kein gueltiges Bild ist, oder nicht gelesen werden kann
     *
     */
    function createImageFromFile($filename, $use_include_path = false, $context = null, &$info = null)
    {
      // try to detect image informations -> info is false if image was not readable or is no php supported image format (a  check for "is_readable" or fileextension is no longer needed)
      $info = array("image"=>getimagesize($filename));
      $info["image"] = getimagesize($filename);
      if($info["image"] === false) throw new InvalidArgumentException("\"".$filename."\" is not readable or no php supported format");
      else
      {
        // fetches fileconten from url and creates an image ressource by string data
        // if file is not readable or not supportet by imagecreate FALSE will be returnes as $imageRes
        $imageRes = imagecreatefromstring(file_get_contents($filename, $use_include_path, $context));
        // export $http_response_header to have this info outside of this function
        if(isset($http_response_header)) $info["http"] = $http_response_header;
        return $imageRes;
      }
    }
    

    Usage (simple example):

    $image = createImageFromFile("http://sample.com/image.png");
    

    Usage (complex example):

    // even sources with php extensions are supported and e.g. Proxy connections and other context Options
    // see http://php.net/manual/function.stream-context-create.php for examples
    $options = array("http"=> 
                      array("proxy" => "tcp://myproxy:8080",
                            "request_fulluri" => true
                           )
                      );
    $context = stream_context_create($options);
    
    $image = createImageFromFile("http://de3.php.net/images/logo.php", null, $context,$info);
    
    // ... your code to resize or modify the image
    

提交回复
热议问题