Home > Article > Backend Development > Save web page as image php function class to save remote image to local
//
// Function: Get the remote image and save it locally
//
//
// Make sure you have permission to write the file to the local server
//
//
// Variable description:
// $url is the complete URL address of the remote image and cannot be empty.
// $filename is an optional variable: if empty, the local file name will be automatically generated based on time and date
//
function GrabImage($url,$filename="") {
if($url=="" ):return false;endif;
if($filename=="") {
$ext=strrchr($url,".");
if($ext!=".gif" && $ext!=". jpg"):return false;endif;
$filename=date("dMYHis").$ext;
}
ob_start();
readfile($url);
$img = ob_get_contents();
ob_end_clean();
$size = strlen($img);
$fp2=@fopen($filename, "a");
fwrite($fp2,$img);
fclose($fp2);
return $filename;
}
$img=GrabImage("/upload/20081208002838680.jpg","");
if($img):echo '
The above introduces the function class for saving web pages as images and saving remote images to the local in PHP, including the content of saving web pages as images. I hope it will be helpful to friends who are interested in PHP tutorials.