X
X
XenK2018-02-25 14:21:49
PHP
XenK, 2018-02-25 14:21:49

Error getting image?

I'm trying to get an image from a link, but I'm getting an error:

'imagecreatefromstring(): Data is not in a recognized format'

The code:
private static function saveImageUrl($url)
    {
        header('Content-Type: image/png');
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.11 Safari/534.16');
        return curl_exec($ch);
    }

    public function uploadImg($fromUrl) 
    {
        return imagecreatefromstring(static::saveImageUrl($fromUrl));
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
ivankomolin, 2018-02-27
@ivankomolin

First, look at what static::saveImageUrl($fromUrl) returns.
Most likely it does not return the content of the picture, hence the error.
And header('Content-Type: image/png') is not needed in this case.
In general, judging by the "parsing" tag, you are robbing pictures from other sites. This is very bad. Slow and resource hungry.
Pictures are files, why are you loading the contents of these files into memory via php.
Load them through the console with the same curl or wget directly to the disk.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question