Save image from url with curl PHP
To save an image from a URL using PHP and cURL, you can use the following function:
<?php
function saveImage($image_url, $image_name)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $image_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
$image = imagecreatefromstring($data);
$image_name = preg_replace('/[^A-Za-z0-9\. -]/', '', $image_name);
imagejpeg($image, $image_name, 100);
imagedestroy($image);
}
To use this function, pass the URL of the image as the first argument and the desired name for the saved image as the second argument. For example:
<?php
saveImage("http://example.com/image.jpg", "image.jpg");
This will save the image to the current working directory with the specified name. You can also specify a different directory by including the path in the image name. For example:
<?php
saveImage("http://example.com/image.jpg", "/path/to/directory/image.jpg");