Php retrieves a remote image and saves it locally. In php, we often use some simple collection functions to automatically save images or resources from remote servers to the local servers, below I will give you a detailed description. in php, we often use some simple collection functions to automatically save images or resources from remote servers to local servers, next I will introduce the remote image in detail and save it to a local machine.
Example 1
| The code is as follows: |
|
|
/*
* Function: php supports multiple methods to download remote images and save them to a local device.
* Parameter: file url, saving file name, used for download
* When the name of the saved file is null, the original name of the remote file is used.
*/
Function getImage ($ url, $ filename = '', $ type = 0 ){
If ($ url = '') {return false ;}
If ($ filename = ''){
$ Ext = strrchr ($ url ,'.');
If ($ ext! Using '.gif '& $ ext! Else '.jpg ') {return false ;}
$ Filename = time (). $ ext;
}
// File storage path
If ($ type ){
$ Ch = curl_init ();
$ Timeout = 5;
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, $ timeout );
$ Img = curl_exec ($ ch );
Curl_close ($ ch );
} Else {
Ob_start ();
Readfile ($ url );
$ Img = ob_get_contents ();
Ob_end_clean ();
}
$ Size = strlen ($ img );
// File size
$ Fp2 = @ fopen ($ filename, 'A ');
Fwrite ($ fp2, $ img );
Fclose ($ fp2 );
Return $ filename;
} |
Example 2
| The code is as follows: |
|
|
//
// Function: obtain the remote image and save it to the local device.
//
//
// Make sure you have the permission to write files to the local server
//
//
// Variable description:
// $ 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;
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, "");
Fwrite ($ fp2, $ img );
Fclose ($ fp2 );
Return $ filename;
}
$ Img = GrabImage ("http://www.bkjia.com ","");
If ($ img): echo'
'; Else: echo "false"; endif;
?> |
In dedecms:
| The code is as follows: |
|
If (! Empty ($ saveremoteimg ))
{
$ Body = stripslashes ($ body );
$ Img_array = array ();
Preg_match_all ("/(src | SRC) = [" | '|] {0,} (http ://(. *). (gif | jpg | jpeg | bmp | png)/isU ", $ body, $ img_array );
$ Img_array = array_unique ($ img_array [2]);
Set_time_limit (0 );
$ ImgUrl = $ img_dir. "/". strftime ("% Y % m % d", time ());
$ ImgPath = $ base_dir. $ imgUrl;
$ MilliSecond = strftime ("% H % M % S", time ());
If (! Is_dir ($ imgPath) @ mkdir ($ imgPath, 0777 );
Foreach ($ img_array as $ key => $ value)
{
$ Value = trim ($ value );
$ Get_file = @ file_get_contents ($ value );
$ RndFileName = $ imgPath. "/". $ milliSecond. $ key. ".". substr ($ value,-3, 3 );
$ Fileurl = $ imgUrl. "/". $ milliSecond. $ key. ".". substr ($ value,-3, 3 );
If ($ get_file)
{
$ Fp = @ fopen ($ rndFileName, "w ");
@ Fwrite ($ fp, $ get_file );
@ Fclose ($ fp );
}
$ Body = ereg_replace ($ value, $ fileurl, $ body );
}
$ Body = addslashes ($ body );
}
?> |
Example 4
| The code is as follows: |
|
|
//
// Function: obtain the remote image and save it to the local device.
//
//
// Make sure you have the permission to write files to the local server
//
//
// Variable description:
// $ 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 automatically generated based on the time and date.
Function GrabImage ($ url, $ filename = ''){
If ($ url = ''): return false; endif;
If ($ filename = ''){
$ Ext = strrchr ($ url ,'.');
If ($ ext! Using '.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 ('http: // www. ccc. cc/static/image/common/logo.png ','');
If ($ img) {echo'
';} Else {echo 'false ';}
?>
|
...