So I've been using this approach to resize images uploaded via forms in PHP:
list ($width, $height, $type, $w)=getimagesize($_FILES[$imageName]['tmp_name']);
$info = getimagesize($_FILES[$imageName]['tmp_name']);
This works well - allows resizing & conversion to png.
Now I need to do the same thing - but for downloaded images, e.g. given the url of an image online such as http://colorvisiontesting.com/images/plate%20with%205.jpg
.
From the looks of it this can be done with CURL
, but I can't quite work out how to then create an image object from it. This is what I have so far:
$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($c, CURLOPT_URL, $imageURL);
$curlImage = curl_exec($c);
$err = curl_getinfo($c,CURLINFO_HTTP_CODE);
curl_close($c);
list ($width, $height, $type, $w)=getimagesize($curlImage);
$info = getimagesize($curlImage);
But this is failing at getimagesize
- and I can't work out what the correct approach is here. Any ideas?
The
getimagesize
function works on filename (local or remote) but not on a string which represents the content of your image.You can:
file_put_contents
, for example)getimagesize
function on the remote file (it should work).getimagesizefromstring
(which works the same asgetimagesize
but works in data and not filename)imagecreatefromstring
(which you need if you want to convert your stream/data to image (gd) resource, and then use theimagesx
andimagesy
on the resouce.