Skip to content

Commit 7c8d189

Browse files
committed
fixed accept header for images #275
1 parent c71e62f commit 7c8d189

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

src/Http/CurlDispatcher.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
class CurlDispatcher implements DispatcherInterface
1313
{
1414
private $responses = [];
15+
private static $acceptHeaders = [
16+
'jpg' => 'image/jpeg',
17+
'jpeg' => 'image/jpeg',
18+
'gif' => 'image/gif',
19+
'png' => 'image/png',
20+
];
1521

1622
private $config = [
1723
CURLOPT_MAXREDIRS => 10,
@@ -79,7 +85,14 @@ public function __destruct()
7985
public function dispatch(Url $url)
8086
{
8187
$options = $this->config;
82-
$options[CURLOPT_HTTPHEADER] = ['Accept: text/html'];
88+
89+
$extension = $url->getExtension();
90+
91+
if (!empty($extension) && isset(self::$acceptHeaders[$extension])) {
92+
$options[CURLOPT_HTTPHEADER] = ['Accept: '.self::$acceptHeaders[$extension]];
93+
} else {
94+
$options[CURLOPT_HTTPHEADER] = ['Accept: text/html'];
95+
}
8396

8497
$response = $this->exec($url, $options);
8598

@@ -176,7 +189,10 @@ public function dispatchImages(array $urls)
176189

177190
$connection = curl_init((string) $url);
178191

179-
curl_setopt_array($connection, $this->config);
192+
$options = $this->config;
193+
$options[CURLOPT_HTTPHEADER] = ['Accept: image/*'];
194+
195+
curl_setopt_array($connection, $options);
180196
curl_multi_add_handle($curl_multi, $connection);
181197

182198
$curl = new CurlResult($connection);

tests/ImageInfoTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ public function testCurl()
1313
$dispatcher = new CurlDispatcher();
1414

1515
$result = $dispatcher->dispatchImages([
16-
Url::create('http://www.mixdecultura.ro/wp-content/uploads/2013/03/galicia-locuinte-celtice.jpg'),
16+
Url::create('https://lightwitch.org/Media/Default/Pictures/server_station.jpg'),
1717
]);
1818

1919
$this->assertCount(1, $result);
2020

2121
$img = $result[0];
2222

23-
$this->assertEquals(600, $img->getWidth());
24-
$this->assertEquals(408, $img->getHeight());
23+
$this->assertEquals(5504, $img->getWidth());
24+
$this->assertEquals(3096, $img->getHeight());
2525
$this->assertEquals('image/jpeg', $img->getContentType());
2626
}
2727

0 commit comments

Comments
 (0)