The first step. First of all use the regular to pull out from the article.
Copy the Code code as follows:
$message//article content
Regular (this is not yet)
$reg = "/]*src=\" (http:\/\/(. +) \ (. +) \. ( JPG|GIF|BMP|BNP)) \ "/isu";
Store the extracted IMG address in the $img _array variable
Preg_match_all ($reg, $message, $img _array, Preg_pattern_order);
Filter for duplicate images
$img _array = Array_unique ($img _array[1]);
The second step. Loop the $img_array array. Make picture save and article position replace
Copy the Code code as follows:
foreach ($img _array as $img) {
Determine if the picture is on your site
if (' xxx.com '! = Get_domain ($img)) {//If this picture is not on your own server
Reading a picture file
$Gimg = new GetImage ();
$Gimg->source = $img;
$Gimg->save_to = './data/temp/';
$FILE = $Gimg->download (); Picture moved to Local
Save to album to get Pictures saved in the location
$img _path = pic_save ($FILE, 0, ");
Text path substitution
$message = Str_replace ($img, $img _path, $message);
}
}
.... At this time $message inside the image has been replaced with the local address of the server, and the picture is saved to its own server.
Copy the Code code as follows:
The following functions and classes are found on the network.
Get 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, etc.
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
From a remote bar picture to a server-local class
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->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 composition is the same as the $_file variable, and it 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 (should be enough)
Curl_setopt_array ($ch, $options);
Curl_exec ($ch);
Curl_close ($ch);
Fclose ($FP);
}
}
The above describes the ACDSee 2009 license code PHP articles in the remote image collection to the local code, including the ACDSee 2009 license code aspects, I hope to be interested in PHP tutorial friends helpful.