>  기사  >  백엔드 개발  >  콘텐츠에 포함된 이미지 주소로 원격 이미지를 수집하고 저장하는 방법_php 팁

콘텐츠에 포함된 이미지 주소로 원격 이미지를 수집하고 저장하는 방법_php 팁

WBOY
WBOY원래의
2016-05-16 20:26:42958검색

이 글의 예시에서는 PHP 콘텐츠에 이미지 주소가 포함된 원격 이미지를 수집하고 저장하는 방법을 설명합니다. 참고할 수 있도록 모든 사람과 공유하세요. 구체적인 구현 방법은 다음과 같습니다.

코드 복사 코드는 다음과 같습니다.
function my_file_get_contents($url, $timeout=30) {
if ( function_exists('curl_init') )
{
$ch = 컬_초기화()
컬_setopt($ch, 컬롭_url, $url)
컬_setopt($ch, 컬롭_리턴트랜스퍼, 1)
컬_setopt($ch, curlopt_connecttimeout, $timeout)
$file_contents = 컬_exec($ch)
컬_닫기($ch)
}
else if ( ini_get('allow_url_fopen') == 1 || strtolower(ini_get('allow_url_fopen')) == 'on' )
{
$file_contents = @file_get_contents($url)
}
그렇지 않으면
{
$file_contents = ''
}
$file_contents 반환
}


코드 복사 코드는 다음과 같습니다.
function 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 = 스트립슬래시(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,60)
$filetime = 시간()
$filename = 날짜("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(" $body 반환;

}

함수 mkdirs($dir)
{
if(!is_dir($dir)){
if(!mkdirs(dirname($dir))){
false를 반환합니다.}
if(!mkdir($dir,0777)){
false를 반환합니다.}
}

사실을 반환합니다. }
//사용법은 다음과 같습니다.

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

이 기사가 모든 사람의 PHP 프로그래밍 설계에 도움이 되기를 바랍니다.
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.