<?php
class ImageDownload
{
/**
* file directory path
*
* @return void
*/
public $folder = 'images';
/**
* website link
*
* @return void
*/
public $websitelink;
/**
* Create a new class instance.
*
* @return void
*/
function __construct($websitelink)
{
if (!file_exists($this->folder)) {
mkdir($this->folder, 0777, true);
}
$this->websitelink = $websitelink;
}
/**
* save file.
*
* @return void
*/
public function getLinks()
{
$html = file_get_contents($this->websitelink);
preg_match_all("{<imgs*(.*?)src=('.*?'|".*?"|[^s]+)(.*?)s*/?>}ims", $html, $image_urls, PREG_SET_ORDER);
return $image_urls;
}
/**
* save file.
*
* @return void
*/
public function saveImage($images)
{
foreach ($images as $val) {
$pos = strpos($val[2],"/");
$link = substr($val[2],1,-1);
if($pos == 1) {
$site = parse_url($this->$websitelink);
$image_url = $site['scheme'].'://'.$site['host'].$link;
} else {
$image_url = $link;
}
$image_name = pathinfo($image_url)['basename'];
copy($image_url, $this->folder.'/'.$image_name);
}
}
}