1 | initial version |
To store an image/jpeg HTTP response as a file using PHP, you can follow the below steps:
The below PHP code can be used to store the image:
$imageUrl = 'http://example.com/image.jpg';
$response = file_get_contents($imageUrl); // or use cURL to fetch image
if ($imageInfo = @getimagesizefromstring($response)) {
$newFilename = 'new-image.jpg';
$destination = 'path/to/save/' . $newFilename;
if (file_put_contents($destination, $response)) {
echo 'Image saved as ' . $newFilename;
} else {
echo 'Failed to save image';
}
} else {
echo 'Invalid image format';
}
Note that you should replace the $imageUrl variable with the actual image URL and provide the correct path for saving the image. Also, make sure that the directory where you are saving the image has write permission.