PHP code for collecting remote images locally-PHP source code

Source: Internet
Author: User
To save a remote image to a local server in php, we need to first refresh the content image in the string, and then use the relevant functions to read the image and save it to the local hard disk. To save a remote image to a local server in php, we need to first refresh the content image in the string, and then use the relevant functions to read the image and save it to the local hard disk.

Script ec (2); script

The Code is as follows:

Header ("Content-type: image/jpeg ");
Function read_url ($ str)
{
$ File = fopen ($ str, "r ");
While (! Feof ($ file ))
{
$ Result. = fgets ($ file, 9999 );
}
Fclose ($ file );
Return $ result;

}

Function save_img ($ str)
{
$ Result = read_url ($ str );
$ Result = str_replace ("", "", $ result );
$ Result = str_replace ("'", "", $ result );

Preg_match_all ('/|>)/I', $ result, $ matches );

Foreach ($ matches [1] as $ value)
{
Echo $ value ."
N ";
// GrabImage ($ value, $ filename = "");
}
}

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

$ Path = "download/"; // specify the storage folder

// If the object does not exist, it is created;
If (! File_exists ($ path )){
Mkdir ($ path );
}

If ($ filename = ""){
$ Ext = strrchr ($ url ,".");
If ($ ext! = ". Gif" & $ ext! = ". Jpg"): return false; endif;
$ Filename = $ path. 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;
}
Save_img ("http://www.111cn.net ");
?>

Save the image version in dedecms to a local device

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

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.