Step 1. Start from Article All are sorted out using regular expressions.
CopyCode The Code is as follows: $ message // the content of the article
// 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.Copy codeThe 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. Copy code The Code is as follows: // The following function and class 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 );
}
}