PHP save image file
Possible Duplicate:
Saving image from PHP URL using PHP
Hi 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. Can anyone point me in the right way to do this please? would I use File Write?
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.
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)
上一篇: 如何在不增加文件大小的情况下在PHP中重复旋转图像
下一篇: PHP保存图像文件