PHP automatically capture remote images with picture addresses in content to save to local

Source: Internet
Author: User
Keywords Web Programming PHP Tutorials
Tags content date file function function_exists get jpg local

PHP automatically capture remote images with picture addresses in content to save to local

function my_file_get_contents ($url, $timeout =30) {
if (function_exists (' Curl_init '))
{
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, Curlopt_connecttimeout, $timeout);
$file _contents = curl_exec ($ch);
Curl_close ($ch);
}
else if (ini_get (' allow_url_fopen ') = = 1 | | | strtolower (ini_get (' allow_url_fopen ')) = = ' on ')
{
$file _contents = @file_get_contents ($url);
}
Else
{
$file _contents = ';
}
return $file _contents;
}



function Get_remote ($body, $title) {

  $img _array = Array ();
  $img _path = Realpath ("... /.. /.. /upfile/news/"). ' /'. Date ("y/m/d/"); Capture remote picture Save address
 //die ($img _path);
  $img _rpath= '/upfile/news/'. Date ("y/m/d/"); //Set access address
  $body = stripslashes (Strtolower ($body));
 preg_match_all (SRC|SRC) =["|" | {0,} (http://(. *). (gif|jpg|jpeg|png)) /isu ", $body, $img _array);
  $img _array = Array_unique ($img _array[2]);
 foreach ($img _array as $key => $value) {
  $get _file = my_file_get_contents ($value);
  $ FILETIME = time ();  
  $filename = Date ("Ymdhis", $filetime). Rand (1,999). substr ($value, -3,3);
  if (empty $get _file) {
   @sleep;
   $get _file = my_file_get_contents ($ VALUE,30);
   if (empty ($get _file)) {
    $body = preg_replace ("/" Addcslashes ($value, "/"). " /isu ", '/notfound.jpg ', $body);
    re-enters;
   }
 
  if (!empty ($get _file)) {
   if mkdirs ($img _path)
   {
    $ fp = fopen ($img _path. $filename, "w");
    if (fwrite ($fp, $get _file)) {        
      $body = preg_replace ("/" Addcslashes ($value, "/"). " /isu ", $img _rpath. $filename, $body);
   }
    fclose ($FP);
    @sleep (6);
  }  
 }   
 
 }
  $body =str_replace (" <img "," <img ", $body);
 return $body;
 
}

function Mkdirs ($dir)
{
if (!is_dir ($dir)) {
if (!mkdirs (DirName ($dir)) {
return false;
if (!mkdir ($dir, 0777)) {
return false;
}
return true;
}
$str = ' fasfsdafsa<img src=/get_pic/2010/03/2010062300391582.jpg/> ';
Echo get_remote ($str, ' picture ');

This site original tour please specify the source, file upload code will do so.

Related Article

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.