Created
August 17, 2012 13:57
-
-
Save piotrbelina/3378890 to your computer and use it in GitHub Desktop.
Image Fetcher
This file contains hidden or 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 | |
class ImageFetcher | |
{ | |
protected $url; | |
public function __construct() | |
{ | |
libxml_use_internal_errors(true); | |
} | |
public function getUrl() { | |
return $this->url; | |
} | |
public function setUrl($url) { | |
$this->url = $url; | |
} | |
public function fetch() | |
{ | |
if (!$this->url) { | |
throw new InvalidArgumentException('Not set url'); | |
} | |
return $this->extractImages(file_get_contents($this->url)); | |
} | |
/** | |
* | |
* @param string $html | |
* @return array | |
*/ | |
public function extractImages($html) | |
{ | |
$images = array(); | |
$dom = new DOMDocument(); | |
$dom->loadHTML($html); | |
$imgs = $dom->getElementsByTagName('img'); | |
foreach ($imgs as $img) { | |
$images[] = $img->getAttribute('src'); | |
} | |
return $images; | |
} | |
public function getImageName($url) | |
{ | |
$urlParts = explode('/', $url); | |
return array_pop($urlParts); | |
} | |
public function downloadImages($path) | |
{ | |
$images = $this->fetch(); | |
foreach ($images as $image) { | |
file_put_contents($path . '/'. $this->getImageName($image), file_get_contents($image)); | |
} | |
} | |
} |
This file contains hidden or 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_once ('../ImageFetcher.php'); | |
/** | |
* Generated by PHPUnit_SkeletonGenerator on 2012-08-17 at 14:53:21. | |
*/ | |
class ImageFetcherTest extends PHPUnit_Framework_TestCase { | |
/** | |
* @var ImageFetcher | |
*/ | |
protected $object; | |
/** | |
* Sets up the fixture, for example, opens a network connection. | |
* This method is called before a test is executed. | |
*/ | |
protected function setUp() { | |
$this->object = new ImageFetcher; | |
} | |
/** | |
* Tears down the fixture, for example, closes a network connection. | |
* This method is called after a test is executed. | |
*/ | |
protected function tearDown() { | |
} | |
public function testExceptionWithoutUrl() { | |
$if = new ImageFetcher; | |
$this->setExpectedException('InvalidArgumentException'); | |
$if->fetch(); | |
} | |
public function testFetching() { | |
$this->object->setUrl('http://php.net/'); | |
$results = $this->object->fetch(); | |
$this->assertInternalType('array', $results); | |
} | |
public function testExtractImages() | |
{ | |
$html = '<html><body><img src="test1" alt="" /><p><img src="test2" alt="" />' | |
. '</p></body></html>'; | |
$images = $this->object->extractImages($html); | |
$this->assertEquals('test1', $images[0]); | |
$this->assertEquals('test2', $images[1]); | |
} | |
public function testGettingName() | |
{ | |
$url = 'http://static.php.net/www.php.net/images/php.gif'; | |
$this->assertEquals('php.gif', $this->object->getImageName($url)); | |
} | |
public function testDownloadImage() | |
{ | |
$path = 'tmp'; | |
if (!file_exists($path)) { | |
mkdir($path); | |
} | |
$this->object->setUrl('http://php.net/'); | |
$this->object->downloadImages($path); | |
$this->assertTrue(file_exists('tmp/php.gif')); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment