Quick Search:
 
 PHP Code: Saving Remote Images With PHP Jump to:  
Category: >> PHP Code >> Saving Remote Images With PHP  

<< lastnext >>

Snippet Name: Saving Remote Images With PHP

Description: Two ways to save remote images with PHP. The second way is useful if the allow_url_fopen function is disabled.

Also see:
» Saving Remote Images With PHP
» Include All Files In A Directory
» More File Type Detection in PHP
» Get Current Page URL
» Get Current Directory Name
» Measure script run time
» Random Image from Directory
» Text to GIF
» Write to File example
» Simple image scaling
» Show current time as PNG-image
» Check if an image exists (GD)
» Check if a file exists
» Gradient Image Generator
» Export table to Excel or MS Word file
» Show basic image info
» Write text onto an image
» Storing images in a database
» Make grayscaled image
» Count lines in file
» Copy File From Server
» CAPTCHA with GD Image Library
» Truncate URLs for clean appearance.
» Calculate File Size In Directory

Comment: (none)

Language: PHP
Highlight Mode: PHP
Last Modified: March 16th, 2009

// get a remote image and save it 
// $path example: '/home/www/images/myimage.jpg'
 
$remote_img = 'http://www.somwhere.com/images/image.jpg';
$img = imagecreatefromjpeg($remote_img);
$path = 'images/';
imagejpeg($img, $path);
 
 
// Alternative Image Saving Using cURL - useful if 
// the allow_url_fopen function is disabled. 
// $fullpathexample: '/home/www/images/myimage.jpg'
 
FUNCTION save_image($img, $fullpath){
 
	$ch = curl_init ($img);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
	$rawdata=curl_exec($ch);
	curl_close ($ch);
	IF(FILE_EXISTS($fullpath)){
		UNLINK($fullpath);
	}
	$fp = FOPEN($fullpath,'x');
	FWRITE($fp, $rawdata);
	FCLOSE($fp);
 
}


 
   Home |    Search |    Code Library |    Sponsors |    Privacy |    Terms of Use |    Contact Us © 2003 - 2024 psoug.org