File tree Expand file tree Collapse file tree 2 files changed +21
-5
lines changed
Expand file tree Collapse file tree 2 files changed +21
-5
lines changed Original file line number Diff line number Diff line change 1212class 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 );
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments