PHP Practical Instance capture Remote Web site picture Save to Local

Source: Internet
Author: User
Tags curl imagejpeg strcmp strlen

PHP Practical Instance capture Remote Web site picture Save to Local
First from the article to all the with the right to dig out. $message//article content

Regular (this is not yet)

$reg = "/]*src=" (http://(. +)/(. +). ( JPG|GIF|BMP|BNP)) "/isu";

Store the IMG address in the $img _array variable

Preg_match_all ($reg, $message, $img _array, Preg_pattern_order);

Filter for duplicate pictures

$img _array = Array_unique ($img _array[1]);


Copy code Step two. Loop the $img_array array. Do picture save and post position replace foreach ($img _array as $img) {

Judge whether it's a picture on your own website

if (' xxx.com '!= get_domain ($img)) {//If this picture is not on your own server

Reading picture files

$Gimg = new GetImage ();

$Gimg->source = $img;

$Gimg->save_to = './data/temp/';

$FILE = $Gimg->download (); Picture moved to Local

Save to Photo album get picture saved location

$img _path = pic_save ($FILE, 0, "");

Text path substitution

$message = Str_replace ($img, $img _path, $message);

}

}

.... At this time $message already the picture has been replaced with its own server local address, and the picture is also saved to its own server.


Copy code//The following function and class 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 the remote bar picture to the 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;

Output object composition is the same as the $_file variable after the same as the normal image upload processing

$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);

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.