Php code for saving remote images to a local server

Source: Internet
Author: User
Sometimes we want to save some beautiful images, but we don't want to save them manually, especially for large batches of storage. At this time, we need to write a program to help us do this, this article describes how php

Sometimes we want to save some beautiful images, but we don't want to save them manually, especially for large batches of storage. At this time, we need to write a program to help us do this, this article describes how php

This article describes how php can localize remote images.

<? Php // site root directory $ pai_basedir = dirname (_ FILE _); // stop Directory attribute $ pai_dir_purview = '2016 '; /* Get Request Remote Content Function */$ cookie_file = dirname (_ FILE __). '/cookie.txt'; // COOKIE storage address function pget ($ url, $ ref = false, $ head = false) {$ curl = curl_init (); // start a CURL session curl_setopt ($ curl, CURLOPT_URL, $ url); // the address to be accessed curl_setopt ($ curl, CURLOPT_SSL_VERIFYPEER, 0 ); // check the certificate source curl_setopt ($ curl, CURLOPT_SSL_VERIFYHOST, 1); // check SSL from the certificate Whether the encryption algorithm has curl_setopt ($ curl, CURLOPT_USERAGENT, $ _ SERVER ['HTTP _ USER_AGENT ']); // simulate the user's browser curl_setopt ($ curl, CURLOPT_FOLLOWLOCATION, 1 ); // use automatic redirect if ($ ref) {curl_setopt ($ curl, CURLOPT_REFERER, $ ref); // The resulting Referer} else {curl_setopt ($ curl, CURLOPT_AUTOREFERER, 1 ); // automatically set Referer} curl_setopt ($ curl, CURLOPT_HTTPGET, 1); // send a regular Post request curl_setopt ($ curl, CURLOPT_COOKIEJAR, $ GLOBALS ['cookie _ file']); // store C Ookie information file name curl_setopt ($ curl, CURLOPT_COOKIEFILE, $ GLOBALS ['cookie _ file']); // read the cookie information stored above curl_setopt ($ curl, CURLOPT_TIMEOUT, 30); // set the timeout limit to prevent endless loops curl_setopt ($ curl, CURLOPT_HEADER, 0); // display the returned Header area content curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1 ); // The obtained information is returned as a file stream $ tmpInfo = curl_exec ($ curl); // the operation if (curl_errno ($ curl) {echo 'errno' is executed '. curl_error ($ curl);} if ($ head) {$ data ['head'] = curl_getin Fo ($ curl) ;}curl_close ($ curl); // key CURL session $ data ['data'] = $ tmpInfo; return $ data; // return data}/*** create all directories ** @ param string $ truepath real address * @ param string $ mmode mode * @ return bool */if (! Function_exists ('mkdirall') {function MkdirAll ($ truepath) {global $ partition _dir_purview; if (! File_exists ($ truepath) {mkdir ($ truepath, $ pai_dir_purview); chmod ($ truepath, $ pai_dir_purview); return true;} else {return true ;}}} /*** obtain the external resource in the article body ** @ access public * @ param string $ body content * @ return string */function GetCurContent ($ body) {global $ pai_multi_site, $ pai_basehost, $ pai_basedir, $ pai_image_dir, $ arcID, $ cuserLogin, $ dsql; $ pai_uploaddir = '/C'; $ basehost = "http: //". $ _ SERVER ["HTTP_HOST"]; $ img_array = array (); preg_match_all ("/src = [\" | '| \ s] {0,} (http: \/([^>] *) \. (gif | jpg | png)/isU ", $ body, $ img_array); $ img_array = array_unique ($ img_array [1]); $ imgUrl = $ pai_uploaddir.' http://www.jb51.net/ '. Date ("ymd", time (); $ imgPath = $ pai_basedir. $ imgUrl; if (! Is_dir ($ imgPath .' http://www.jb51.net/ ') {MkdirAll ($ imgPath, $ GLOBALS ['cfg _ dir_purview']); CloseFtp () ;}$ milliSecond = date ('his', time ()); foreach ($ img_array as $ key => $ value) {if (preg_match ("#". $ basehost. "# I", $ value) {continue;} if (preg_match ("#". $ basehost. "# I", $ value) {continue;} if (! Preg_match ("# ^ http: \// # I", $ value) {continue;} $ http = pget ($ value, '$ value', true ); $ itype = ($ http ['head'] ['content _ type']); if (! Preg_match ("#\. (jpg | gif | png) # I ", $ itype) {if ($ itype = 'image/gif') {$ itype = ". gif ";} else if ($ itype = 'image/png ') {$ itype = ". png ";} else {$ itype = '.jpg '; }}$ milliSecondN = rand ). rand (1000,9999); $ value = trim ($ value); $ rndFileName = $ imgPath.' http://www.jb51.net/ '. $ MilliSecondN.'-'. $ key. $ itype; $ fileurl = $ imgUrl .' http://www.jb51.net/ '. $ MilliSecondN. '-'. $ key. $ itype; $ tp = fopen ($ rndFileName, 'wb'); fwrite ($ tp, $ http ['data']); fclose ($ tp ); if (file_exists ($ pai_basedir. $ fileurl) {$ info = ''; $ imginfos = GetImageSize ($ rndFileName, $ info); $ fsize = filesize ($ rndFileName); $ body = str_replace ($ value, $ fileurl, $ body) ;}return $ body ;}// call method echo GetCurContent ($ body );

The above is all the code to implement remote image localization. I hope it will be helpful for your learning.

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.