采集远程图片图片并且替换图片地址代码
现在用php采集远程图片是很简单的事情,但是我们要把采集过来的数据进行过滤,这恐怕得用到正则表达式了,下面的程序是采集远程图片图片并且替换图片地址代码。
采集远程图片图片并且替换图片地址代码
/*
现在用php教程采集远程图片是很简单的事情,但是我们要把采集过来的数据进行过滤,这恐怕得用到正则表达式了,下面的程序是采集远程图片图片并且替换图片地址代码。
*/
$dir_date=date(y/m/d);//日期文件夹
$dir_name="images/$dir_date";//文件夹路径
function mkdirm($path) //取文件夹名
{
if (!file_exists($path)) //判断是否存在
{
mkdirm(dirname($path)); //返回路径中的目录部分
mkdir($path, 0777); //创建文件夹和权限
}
}
mkdirm($dir_name);//创建文件夹
//$files = file_get_contents(http://www.ywsp.com);
$files =<img src="php教程" alt="普通任务" /><img width="272px" height="60px" style="padding-top: 10px;" src="php教程;
// 图片地址转换一下 $p=preg_replace(//image//, http://qq.ip138.com/image/, $pg[1]);
$reg = "/<img[^>]*src="(http://(.+)/(.+).(jpg|gif|bmp|bnp))"/isu";
$img=preg_match_all($reg,$files,$imgs);
for ($i=0;$i<count($imgs[0]);$i++)
{
//echo "".$imgs[1][$i]." <br>n";
//保存图片
$opts = array(
http=>array(
header=>"referer: php教程"
)
);
$context = stream_context_create($opts);
header( content-type: image/jpeg );
$fileresource = @file_get_contents( $imgs[1][$i],false,$context) ;
$image_old.=""".$imgs[1][$i].""".","; //先把所有网址变为一个数如1,2,3,4
$image_new.=""".$dir_name."/".mktime().$i.".jpg".""".","; //先把所有新的图片地址变为一个数如1,2,3,4
file_put_contents($dir_name."/".mktime().$i.".jpg", $fileresource);
//保存图片
}
$image_old=substr($image_old,0,(strlen($image_old)-1));//把网址最后个逗号去掉
$image_new=substr($image_new,0,(strlen($image_new)-1));//把新网址最后个逗号去掉
//print_r($p);
$image_old=explode(",",$image_old);//分割多个
$image_new=explode(",",$image_new);//分割多个
$files=str_replace($image_old,$image_new,$files); //把网址跟新的图片网址调用进去替换
echo $files;
本文地址:http://www.phprm.com/frame/php1005091.html
转载随意,但请附上文章地址:-)