ホームページ  >  記事  >  バックエンド開発  >  コンテンツ内の画像アドレスを持つリモート画像を収集して保存する方法_PHP チュートリアル

コンテンツ内の画像アドレスを持つリモート画像を収集して保存する方法_PHP チュートリアル

WBOY
WBOYオリジナル
2016-07-13 09:59:40926ブラウズ

PHPコンテンツ内の画像アドレス付きリモート画像を収集・保存する方法

この記事では主に、リモート画像の収集・保存を実現できるPHPコンテンツ内の画像アドレス付きリモート画像を収集・保存する方法を紹介します。とても実践的なスキルなので、必要な友達は参考にしてください

この記事の例では、PHPでコンテンツ内の画像アドレスを持つリモート画像を収集し、保存する方法を説明します。参考のためにみんなで共有してください。具体的な実装方法は以下の通りです

コードは次のとおりです:

関数 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); カール_クローズ($ch); }
else if ( ini_get('allow_url_fopen') == 1 || strto lower(ini_get('allow_url_fopen')) == 'on' )
{
$file_contents = @file_get_contents($url); }
それ以外は
{
$file_contents = ''; }
$file_contents を返す
}



コードは次のとおりです:

関数 get_remote($body,$title){

$img_array = 配列(); $img_path = realpath("../../../upfile/news/").'/'.date("y/m/d/") //リモート画像保存アドレスを収集 ; //死ぬ($img_path); $img_rpath='/upfile/news/'.date("y/m/d/") //アクセスアドレスを設定します
; $body = ストリップスラッシュ(strto lower($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,60); $ファイル時間 = 時間(); $filename = date("ymdhis",$filetime).rand(1,999).'.'.substr($value,-3,3); if(emptyempty($get_file)){

@睡眠(10); $get_file = my_file_get_contents($value,30); if(emptyempty($get_file)){
$body = preg_replace("/".addcslashes($value,"/")."/isu", '/notfound.jpg', $body); 続けます
}
}
if(!emptyempty($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); @睡眠(6); }
}

}
$body =str_replace("
}

関数 mkdirs($dir)
{
if(!is_dir($dir)){
if(!mkdirs(ディレクトリ名($dir))){
false を返します;}
if(!mkdir($dir,0777)){
false を返します;}
}
true を返します。 }
//使用方法は次のとおりです:

$str ='fasfsdafsa'; echo get_remote($str,'picture');



この記事で説明した内容が皆様の PHP プログラミング設計に役立つことを願っています。






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

www.bkjia.com

tru​​e

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

技術記事

コンテンツ内の画像アドレスを持つリモート画像を収集してphpに保存する方法 この記事では、コレクションを実現できる、コンテンツ内の画像アドレスを持つリモート画像を収集してphpに保存する方法を中心に紹介します。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。