I am trying to save an image from VK to a folder. However the image does not save correctly.
The code I use is:
// imageFromURL (Save an image from a URL)
function imageFromURL($url,$saveto){
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$raw=curl_exec($ch);
curl_close ($ch);
if(file_exists($saveto)){
unlink($saveto);
}
$fp = fopen($saveto,'x');
fwrite($fp, $raw);
fclose($fp);
}
imageFromURL('https://vk.com/captcha.php?sid=698254154192&s=1', 'Image/Captcha.png');
I think it must not be a standard image format? How could I save this and make sure it was a png format image with CURL?
You can use exif_imagetype($image)
to return what type of image a file is. PNG would return 3.
Additionally, you could check prior to saving:
$mime_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
and check for the mime type(s) you may be looking for. Mind you, there is some trust involved there because you're relying on the remote server to report that info correctly.