Skip to content

Commit 7df244a

Browse files
committed
Merge pull request #5 from zanardigit/master
Build DataUri from remote URL
2 parents 6b84ac5 + c3562fa commit 7df244a

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
],
1313
"require": {
1414
"php" : ">=5.3.2",
15-
"symfony/http-foundation" : "~2.0"
15+
"symfony/http-foundation" : "~2.0",
16+
"ext-curl" : "*"
1617
},
1718
"require-dev": {
1819
"sami/sami" : "dev-master"

docs/source/index.rst

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ Dumper
8989
// Add some parameters
9090
$dataObject->addParameters('charset' => 'utf-8');
9191
92-
echo DaraURI\Dumper::dump($dataObject);
92+
echo DataURI\Dumper::dump($dataObject);
9393
// Output data:text/plain;charset=utf-8,%23%24%25
9494
9595
Dump URI from file
@@ -101,7 +101,19 @@ Dump URI from file
101101
use DataURI;
102102
103103
$dataObject = DataURI\Data::buildFromFile("/path/to/my/image.png");
104-
echo DaraURI\Dumper::dump($dataObject);
104+
echo DataURI\Dumper::dump($dataObject);
105+
// Output data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA...+S/EAAAAASUVORK5CYII=
106+
107+
Dump URI from url
108+
^^^^^^^^^^^^^^^^^
109+
110+
.. code-block:: php
111+
112+
<?php
113+
use DataURI;
114+
115+
$dataObject = DataURI\Data::buildFromUrl("http://www.example.org/path/to/my/image.png");
116+
echo DataURI\Dumper::dump($dataObject);
105117
// Output data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA...+S/EAAAAASUVORK5CYII=
106118
107119
Handling Exceptions
@@ -110,7 +122,7 @@ Handling Exceptions
110122
PHP-dataURI throws 4 different types of exception :
111123

112124
- ``\DataURI\Exception\FileNotFoundException`` is thrown when an invalid
113-
pathfile is supplied
125+
pathfile is supplied or when we don't get a valid response from URL
114126
- ``\DataURI\Exception\InvalidDataException`` is thrown when raw data could not
115127
be decoded
116128
- ``\DataURI\Exception\TooLongDataException`` is thrown when provided data is too

src/DataURI/Data.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,32 @@ public static function buildFromFile($file, $strict = false, $lengthMode = Data:
225225
return $dataURI;
226226
}
227227

228+
/**
229+
* Get a new instance of DataUri\Data from a remote file
230+
*
231+
* @param string $url Path to the remote file
232+
* @param boolean $strict Use strict mode
233+
* @param int $lengthMode The length mode
234+
* @return \DataURI\Data
235+
*/
236+
public static function buildFromUrl($url, $strict = false, $lengthMode = Data::TAGLEN)
237+
{
238+
$ch = curl_init($url);
239+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
240+
$data = curl_exec($ch);
241+
242+
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200) {
243+
throw new FileNotFoundException(sprintf('%s file does not exist or the remote server does not respond', $url));
244+
}
245+
246+
$mimeType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
247+
curl_close($ch);
248+
249+
$dataURI = new static($data, $mimeType, array(), $strict, $lengthMode);
250+
251+
return $dataURI;
252+
}
253+
228254
/**
229255
* Contructor initialization
230256

tests/DataTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,15 @@ public function testBuildFromFile()
119119
$this->assertEquals(file_get_contents($file), $dataURI->getData());
120120
}
121121

122+
public function testBuildFromUrl()
123+
{
124+
$url = 'http://www.alchemy.fr/images/header_03.png';
125+
$dataURI = DataURI\Data::buildFromUrl($url);
126+
$this->assertInstanceOf('DataURI\Data', $dataURI);
127+
$this->assertEquals('image/png', $dataURI->getMimeType());
128+
$this->assertEquals(file_get_contents($url), $dataURI->getData());
129+
}
130+
122131
/**
123132
* @expectedException \DataURI\Exception\FileNotFoundException
124133
*/

0 commit comments

Comments
 (0)