Today, I wrote this function to share it with my friends. you can obtain remote images and save them to your local computer. In fact, you can refer to this function in many php management systems. Step 1. extract all the regular expressions from the article.
The code is as follows:
$ Message // Document Content
// Regular (this is not yet)
$ Reg = "/] * src = \" (http :\/\/(. + )\/(. + )\. (jpg | gif | bmp | bnp en) \ "/isU ";
// Store the extracted img address in the $ img_array variable.
Preg_match_all ($ reg, $ message, $ img_array, PREG_PATTERN_ORDER );
// Filter duplicate images
$ Img_array = array_unique ($ img_array [1]);
Step 2. loop through the $ img_array. save the image and replace the position of the article.
The code is as follows:
Foreach ($ img_array as $ img ){
// Determine whether it is an image on your website
If ('XXX. com '! = Get_domain ($ img) {// if this image is not on your server
// Read Image files
$ Gimg = new GetImage ();
$ Gimg-> source = $ img;
$ Gimg-> save_to = './data/temp /';
$ FILE = $ Gimg-> download (); // move the image to the local device.
// Save the image to the position where the image is saved.
$ Img_path = pic_save ($ FILE, 0 ,'');
// Text path replacement
$ Message = str_replace ($ img, $ img_path, $ message );
}
}
... At this time, the image in $ message has been replaced with the local address of your server, and the image is saved to your server.
The code is as follows:
// The following functions and classes are found on the network.
// Obtain the domain name from the url
Function get_domain ($ url ){
$ Pattern = "/[\ w-] + \. (com | net | org | gov | cc | biz | info | cn )(\. (cn | hk ))*/";
Preg_match ($ pattern, $ url, $ matches );
If (count ($ matches)> 0 ){
Return $ matches [0];
} Else {
$ Rs = parse_url ($ url );
$ Main_url = $ rs ["host"];
If (! Strcmp (long2ip (sprintf ("% u", ip2long ($ main_url), $ main_url )){
Return $ main_url;
} Else {
$ Arr = explode (".", $ main_url );
$ Count = count ($ arr );
$ EndArr = array ("com", "net", "org", "3322"); // com.cn net.cn
If (in_array ($ arr [$ count-2], $ endArr )){
$ Domain = $ arr [$ count-3]. ".". $ arr [$ count-2]. ".". $ arr [$ count-1];
} Else {
$ Domain = $ arr [$ count-2]. ".". $ arr [$ count-1];
}
Return $ domain;
} // End if (! Strcmp ...)
} // End if (count ...)
} // End function
// Classes uploaded from remote bar pictures to the local server
Class GetImage {
Var $ source;
Var $ save_to;
Var $ quality;
Function download ($ method = 'curl '){
$ Info = @ GetImageSize ($ this-> source );
$ Mime = $ info ['Mime '];
// What sort of image?
$ Type = substr (strrchr ($ mime, '/'), 1 );
Switch ($ type ){
Case 'jpeg ':
$ Image_create_func = 'imagecreatefromjpeg ';
$ Image_save_func = 'imagejpeg ';
$ New_image_ext = 'jpg ';
// Best Quality: 100
$ Quality = isSet ($ this-> quality )? $ This-& gt; quality: 100;
Break;
Case 'PNG ':
$ Image_create_func = 'imagecreatefrompng ';
$ Image_save_func = 'imagepng ';
$ New_image_ext = 'PNG ';
// Compression Level: from 0 (no compression) to 9
$ Quality = isSet ($ this-> quality )? $ This-> quality: 0;
Break;
Case 'bmp ':
$ Image_create_func = 'imagecreatefrombmp ';
$ Image_save_func = 'imagebmp ';
$ New_image_ext = 'bmp ';
Break;
Case 'GIF ':
$ Image_create_func = 'imagecreatefromgif ';
$ Image_save_func = 'imagegif ';
$ New_image_ext = 'GIF ';
Break;
Case 'vnd. wap. wbmp ':
$ Image_create_func = 'imagecreatefromwbmp ';
$ Image_save_func = 'imagewbmp ';
$ New_image_ext = 'bmp ';
Break;
Case 'xbm ':
$ Image_create_func = 'imagecreatefromxbm ';
$ Image_save_func = 'imagexbm ';
$ New_image_ext = 'xbm ';
Break;
Default:
$ Image_create_func = 'imagecreatefromjpeg ';
$ Image_save_func = 'imagejpeg ';
$ New_image_ext = 'jpg ';
}
If (isSet ($ this-> set_extension )){
$ Ext = strrchr ($ this-> source ,".");
$ Strlen = strlen ($ ext );
$ New_name = basename (substr ($ this-> source, 0,-$ strlen). '.'. $ new_image_ext;
} Else {
$ New_name = basename ($ this-> source );
}
$ Save_to = $ this-> save_to. "/blog_insert_temp _". time (). mt_rand (1, 99). ".". $ new_image_ext;
// The output object is the same as the $ _ FILE variable, and the output object is the same as the normal image upload process.
$ Img_info ['name'] = basename ($ this-> source );
$ Img_info ['type'] = $ mime;
$ Img_info ['size'] = 1000;
$ Img_info ['tmp _ name'] = $ save_to;
$ Img_info ['error'] = 0;
If ($ method = 'curl '){
$ Save_image = $ this-> LoadImageCURL ($ save_to );
} Elseif ($ method = 'gd '){
$ Img = $ image_create_func ($ this-> source );
If (isSet ($ quality )){
$ Save_image = $ image_save_func ($ img, $ save_to, $ quality );
} Else {
$ Save_image = $ image_save_func ($ img, $ save_to );
}
}
Return $ img_info;
}
Function LoadImageCURL ($ save_to ){
$ Ch = curl_init ($ this-> source );
$ Fp = fopen ($ save_to, "wb ");
// Set URL and other appropriate options
$ Options = array (CURLOPT_FILE => $ fp,
CURLOPT_HEADER => 0,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_TIMEOUT => 60); // 1 minute timeout (shocould be enough)
Curl_setopt_array ($ ch, $ options );
Curl_exec ($ ch );
Curl_close ($ ch );
Fclose ($ fp );
}
}