Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP save image file [duplicate]

Tags:

php

Possible Duplicate:
Saving image from PHP URL

I have an image as a url link from the 3rd party web thumb site (IE http://images.websnapr.com/?size=size&key=Y64Q44QLt12u&url=http://google.com) What I would like to do is run a script that takes the image and saves it in a directory on my server using php. How to do this? would I use File Write?

like image 786
Mick Avatar asked Sep 11 '10 23:09

Mick


People also ask

How to save an image from URL in PHP?

file_put_contents () – This function is used to write remote image data to a file. You can use cURL to save an image from URL using PHP. The following code snippet helps you to copy an image file from URL using cURL in PHP.

How to save remote image to local server using PHP?

The file_get_contents () and file_put_contents () provides an easiest way to save remote image to local server using PHP. The image file can be saved straight to directory from the URL. In the example code snippet, we will provide two ways to save image from URL using PHP.

How to save an image programmatically?

It’s easy to go to the page and use right click button and save the image. But what if you wanted to do it programmatically? The reasons may vary person to person, developer to developer. If set of hundreds of image URLs given and somehow want to save them into the machine, or need to use this concept into the projects.

How to download image from url?

There are two different approaches to download image from url which are listed below: Using basic file handling. Using an HTTP library called cURL. Both of these approaches come with their own set of merits and demerits. Using Basic File Handling: This is the fundamental and easiest way to accomplish the task.


2 Answers

No need to create a GD resource, as someone else suggested.

$input = 'http://images.websnapr.com/?size=size&key=Y64Q44QLt12u&url=http://google.com';
$output = 'google.com.jpg';
file_put_contents($output, file_get_contents($input));

Note: this solution only works if you're setup to allow fopen access to URLs. If the solution above doesn't work, you'll have to use cURL.

like image 129
Tim Cooper Avatar answered Oct 18 '22 00:10

Tim Cooper


Note: you should use the accepted answer if possible. It's better than mine.

It's quite easy with the GD library.

It's built in usually, you probably have it (use phpinfo() to check)

$image = imagecreatefromjpeg("http://images.websnapr.com/?size=size&key=Y64Q44QLt12u&url=http://google.com");

imagejpeg($image, "folder/file.jpg");

The above answer is better (faster) for most situations, but with GD you can also modify it in some form (cropping for example).

$image = imagecreatefromjpeg("http://images.websnapr.com/?size=size&key=Y64Q44QLt12u&url=http://google.com");
imagecopy($image, $image, 0, 140, 0, 0, imagesx($image), imagesy($image));
imagejpeg($image, "folder/file.jpg");

This only works if allow_url_fopen is true (it is by default)

like image 44
Mark Lalor Avatar answered Oct 17 '22 23:10

Mark Lalor