search
Homephp教程php手册从文章中提取图片,把图片保存到本地,自动提取缩略图

开发二代旅游网站程序和CMS的时候,有一个需求,就是从网上复制的内容,里面包含图片的,需要对把图片提取出来,并且保存到本地,并且把图片的URL地址本地化,以下是实现的代码。
开发二代旅游网站程序和CMS的时候,有一个需求,就是从网上复制的内容,里面包含图片的,需要对把图片提取出来,并且保存到本地,并且把图片的URL地址本地化,以下是实现的代码,功能和效果可以参考二代旅游CMS官网(http://www.erdaicms.com):

/* 远程图片本地化 $body为html原内容 */<br> function auto_save_image($body){<br>     <br>     <br>         $img_array = explode('&',$body);<br>         $img_array = array();<br>         preg_match_all("/(src)=[\"|\'| ]{0,}(http:\/\/(.*)\.(gif|jpg|jpeg|bmp|png|JPEG|GIF|PNG))[\"|\'| ]{0,}/isU", $body, $img_array);<br>         $img_array = array_unique($img_array[2]);//也可以自动匹配<br>     <br>     <br>         set_time_limit(0);<br>         $imgPath = "Uploads/article/".date("Y-m-d")."/";<br>         $milliSecond = strftime("%H%M%S",time());<br>         if(!is_dir($imgPath)) @mkdir($imgPath,0777);<br>         foreach($img_array as $key =>$value)<br>         {<br>                 $value = trim($value);<br>                 $get_file = @file_get_contents($value);<br>                 <br>                 <br>             <br>                 $rndFileName = $imgPath."/".$milliSecond.$key.".".substr($value,-3,3);<br>                 if($get_file)<br>                 {<br>                         $fp = @fopen($rndFileName,"w");<br>                         @fwrite($fp,$get_file);<br>                         @fclose($fp);<br>                 }<br>                 $body = @ereg_replace($value, '/'.$rndFileName, $body);<br>         }<br>      <br>         return $body;<br>     }/* 提取图片数组 */function auto_return_image($body){<br>     <br>         $img_array = explode('&',$body);<br>         $img_array = array();<br>         preg_match_all("/(src)=[\"|\'| ]{0,}(\/(.*)\.(gif|jpg|jpeg|bmp|png|JPEG|GIF|PNG))[\"|\'| ]{0,}/isU", $body, $img_array);<br>         $img_array = array_unique($img_array[2]);//也可以自动匹配<br>         <br>         return  $img_array;<br> }最终效果如下:
从文章中提取图片,把图片保存到本地,自动提取缩略图

附件 1.png ( 10.84 KB 下载:13 次 )

AD:真正免费,域名+虚机+企业邮箱=0元

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.