Last active
December 12, 2016 14:05
-
-
Save vozhukh/776b89c40131e9dc0b1f78f860455f02 to your computer and use it in GitHub Desktop.
клас для Бітрикс + phpQuery. Копіювання зображень із стороннього сайту із збереженням структури каталогів.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require ('lib/phpQuery.php'); // include phpQuery library | |
class ParseImg { | |
private $domen ='http://www.domen.com'; | |
// get img by html | |
public function parseImg ($html){ | |
$document = phpQuery::newDocument($html); | |
$ImgData=array(); | |
$hentry = $document->find('img'); // get all img | |
foreach ($hentry as $el) { | |
$pq = pq($el); # Это аналог $ в jQuery | |
$ImgData[] =array( | |
'src'=> $pq->attr('src'),// get img path | |
); | |
} | |
return $ImgData; | |
} | |
// load img to local tmp dir site | |
public function loadImg($src){ | |
$src = $this->domen.$src; | |
$arRes= false; | |
if(file_exists($_SERVER["DOCUMENT_ROOT"].$src)) return -1; // file exist in local site - return -1 | |
if ( preg_match( "/^(http|https):\\/\\//", $src ) ) | |
{ | |
$arRes =CIBlock::makeFileArray( | |
$src, | |
false,//delete | |
'' | |
); | |
} | |
return $arRes; | |
} | |
//copy img with save | |
public function copyToServer ($From, $To){ | |
if(file_exists($To)) return -1; | |
return CopyDirFiles($From, $To); | |
} | |
} | |
/*used:*/ | |
/* | |
load by step. | |
$LastID= ($_SESSION['LastID_img'])? $_SESSION['LastID_img'] : 0; | |
$arFilter = array( | |
"IBLOCK_ID" => 4, | |
'>ID'=>$LastID, | |
); | |
$pImg = new ParseImg(); | |
$obRes = CIBlockElement::GetList( array('id'=>'asc'), $arFilter, false, array('nTopCount'=>1), array('ID', 'DETAIL_TEXT' ) ); | |
$ptime = getmicrotime(); | |
while ( $arRes = $obRes->GetNext() ) | |
{ | |
foreach ($pImg->parseImg($arRes['DETAIL_TEXT']) as $img){ | |
if(trim($img['src'])!=''){ | |
$res= $pImg->loadImg(trim($img['src'])); | |
if($res['tmp_name']!=''){ | |
$pImg->copyToServer($res['tmp_name'], $_SERVER["DOCUMENT_ROOT"].trim($img['src'])); | |
} | |
} | |
} | |
$LastID=$arRes['ID']; | |
} | |
$_SESSION['LastID_img']= $LastID; | |
echo "Loop time ".round(getmicrotime()-$ptime, 3)." seconds"; | |
echo '<pre>sastID: '; | |
print_r($LastID); | |
echo '</pre>'; | |
// simple reload page by js | |
if($LastID>0){ | |
?> | |
<script> | |
setTimeout(function(){ | |
window.location.reload(1); | |
}, 1500); // 1500 = 1.5 second | |
</script> | |
<? | |
} | |
*/ | |
/*end;*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment