Gregwar / Image

A PHP library to handle images
MIT License
1k stars 191 forks source link

Open image from an external URL #170

Open vasileios-tsakalis opened 4 years ago

vasileios-tsakalis commented 4 years ago

Is it possible to open an image from an external source, e.g. an API and then resize it.

thedevale commented 3 years ago

I agree, needing this too

garygreen commented 3 years ago

This would be outside scope of this library imo.

Just use file_get_contents(), or curl, or guzzle, then pass onto gregwar.

basteyy commented 3 years ago

To download a file, you should use a proper library instead of using just file_get_contents. Think about authentications. From my point of view, it's not purposeful to implement such this kind of library in this package.

I would suggest to create a class like:

class DownloadAndDoStuff {

    private function _download(string $remote_image_url, string $local_path = null) {

        if(null === $local_path ) {
            $local_path = dirname(__DIR__);
        }

        $file = curl_file_create($local_path);
        $curl = new anlutro\cURL\cURL;
        $response = $curl->rawPost($remote_image_url, ['file' => $file]);

        return $local_path;
    }

    public function resizeImage(string $remote_image_url) {
        if(!isset($this->remoteFiles[$remote_image_url])) {
            $this->remoteFiles[$remote_image_url] = $this->_download($remote_image_url);
        }

        // File should be here: $this->remoteFiles[$remote_image_url]  do whatever you want

    }

    public function cropImage(string $remote_image_url) {
        if(!isset($this->remoteFiles[$remote_image_url])) {
            $this->remoteFiles[$remote_image_url] = $this->_download($remote_image_url);
        }

        // File should be here: $this->remoteFiles[$remote_image_url]  do whatever you want

    }

    public function flipImage(string $remote_image_url) {
        if(!isset($this->remoteFiles[$remote_image_url])) {
            $this->remoteFiles[$remote_image_url] = $this->_download($remote_image_url);
        }

        // File should be here: $this->remoteFiles[$remote_image_url]  do whatever you want
    }
}

see https://github.com/anlutro/php-curl for more