Home >Backend Development >PHP Tutorial >PHP collects images in articles and replaces them locally_PHP tutorial

PHP collects images in articles and replaces them locally_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:16:48881browse

This article is a detailed analysis and introduction to the local implementation code of image acquisition and replacement in the PHP collection article. Friends who need it can refer to it. ​ Copy code The code is as follows:


/**
* Get the image path in the replacement article
* @param string $xstr content
* @param string $keyword The file name of the created photo
* @param string $oriweb URL
* @return string
*
​*/
function replaceimg($xstr,$keyword, $oriweb){

//Save path
$d = date('Ymd', time());
$dirslsitss = '/var/www/weblist/uploads/'.$keyword.'/'.$d;//Whether the category exists
If(!is_dir($dirslsitss)) {
​​​​​@mkdir($dirslsitss, 0777);
}

// Match the src of the image
Preg_match_all('#]*>#i', $xstr, $match);

foreach($match[1] as $imgurl){

         $imgurl = $imgurl;

If(is_int(strpos($imgurl, 'http'))){
               $arcurl = $imgurl;
         } else {
               $arcurl = $oriweb.$imgurl;                                               }
          $img=file_get_contents($arcurl);

                             
If(!empty($img)) {

//Save the picture to the server
$fileimgname = time()."-".rand(1000,9999).".jpg";
$filecachs=$dirslsitss."/".$fileimgname;
$fanhuistr = file_put_contents( $filecachs, $img );
              $saveimgfile = "/uploads/$keyword"."/".$d."/".$fileimgname;

                                                                                    $xstr=str_replace($imgurl,$saveimgfile,$xstr);
}
}
Return $xstr;
}

http://www.bkjia.com/PHPjc/372548.html

truehttp: //www.bkjia.com/PHPjc/372548.htmlTechArticleThis article is a detailed analysis and introduction to the local implementation code of image acquisition in the PHP collection article. For reference, please copy the code as follows: /** * Get replacement...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn