Copy codeThe Code is as follows:
<? Php
Header ("Content-type: image/jpeg ");
Function read_url ($ str)
{
$ File = fopen ($ str, "r ");
While (! Feof ($ file ))
{
$ Result. = fgets ($ file, 9999 );
}
Fclose ($ file );
Return $ result;
}
Function save_img ($ str)
{
$ Result = read_url ($ str );
$ Result = str_replace ("\" "," ", $ result );
$ Result = str_replace ("\ '", "", $ result );
Preg_match_all ('/ |>)/I ', $ result, $ matches );
Foreach ($ matches [1] as $ value)
{
Echo $ value. "<br> \ n ";
// GrabImage ($ value, $ filename = "");
}
}
// $ Url is the complete URL of the remote image. It cannot be blank.
// $ Filename is an optional variable. If it is null, the local file name is based on the time and date.
// Automatically generated.
Function GrabImage ($ url, $ filename = ""){
If ($ url = ""): return false; endif;
$ Path = "download/"; // specify the storage folder
// If the object does not exist, it is created;
If (! File_exists ($ path )){
Mkdir ($ path );
}
If ($ filename = ""){
$ Ext = strrchr ($ url ,".");
If ($ ext! = ". Gif" & $ ext! = ". Jpg"): return false; endif;
$ Filename = $ path. date ("dMYHis"). $ ext;
}
Ob_start ();
Readfile ($ url );
$ Img = ob_get_contents ();
Ob_end_clean ();
$ Size = strlen ($ img );
$ Fp2 = @ fopen ($ filename, "");
Fwrite ($ fp2, $ img );
Fclose ($ fp2 );
Return $ filename;
}
Save_img ("http://www.jb51.net ");
?>