Skip to content

Commit 70649be

Browse files
committed
Merge pull request #55 from Soullivaneuh/img-blacklist
Images URLs blacklist
2 parents a06a333 + ed61e55 commit 70649be

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ The available options for the adapters are:
7474

7575
* minImageWidth (int): Minimal image width used to choose the main image
7676
* minImageHeight (int): Minimal image height used to choose the main image
77+
* imagesBlacklist (array): Images that you don't want to be used. Could be plain text or [Url](https://github.com/oscarotero/Embed/blob/master/src/Url.php) match pattern.
7778
* getBiggerImage (bool): Choose the bigger image as the main image (instead the first found, that usually is the most relevant).
7879
* getBiggerIcon (bool): The same than getBiggerImage but used to choose the main icon
7980
* facebookKey (string): Used only in Facebook adapter, to get info from facebook pages when these pages are not public and a access token is required
@@ -87,6 +88,7 @@ $config = [
8788
'config' => [
8889
'minImageWidth' => 16,
8990
'minImageHeight' => 16,
91+
'imagesBlacklist' => null,
9092
'getBiggerImage' => false,
9193
'getBiggerIcon' => false,
9294
'facebookKey' => null,
@@ -191,6 +193,11 @@ $config = [
191193
'config' => [
192194
'minImageWidth' => 16,
193195
'minImageHeight' => 16,
196+
'imagesBlacklist' => [
197+
'http://example.com/full/path/to/image.jpg',
198+
'http?://test.*/*.png/',
199+
'*/bad_image.gif'
200+
]
194201
]
195202
],
196203
'providers' => [

src/Adapters/Adapter.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace Embed\Adapters;
33

4+
use Embed\Url;
45
use Embed\Utils;
56
use Embed\Request;
67
use Embed\Providers\ProviderInterface;
@@ -43,6 +44,7 @@ abstract class Adapter
4344
protected $config = [
4445
'minImageWidth' => 16,
4546
'minImageHeight' => 16,
47+
'imagesBlacklist' => null,
4648
'getBiggerImage' => false,
4749
'getBiggerIcon' => false,
4850
'facebookKey' => null,
@@ -302,7 +304,29 @@ public function getProviderUrl()
302304
*/
303305
public function getImagesUrls()
304306
{
305-
return Utils::getData($this->providers, 'imagesUrls', $this->request->url);
307+
$imagesUrls = Utils::getData($this->providers, 'imagesUrls', $this->request->url);
308+
309+
$blacklist = $this->config['imagesBlacklist'];
310+
$hasBlacklist = is_array($blacklist) && count($blacklist) > 0;
311+
312+
$imagesUrls = array_filter($imagesUrls, function($imageUrl) use ($blacklist, $hasBlacklist) {
313+
// Clean empty urls
314+
if (empty($imageUrl['value'])) {
315+
return false;
316+
}
317+
318+
// Remove image url if on blacklist
319+
if ($hasBlacklist) {
320+
$url = new Url($imageUrl['value']);
321+
322+
return !$url->match($blacklist) && !in_array($imageUrl['value'], $blacklist, true);
323+
}
324+
325+
return true;
326+
});
327+
328+
// Use array_values to reset keys after filter
329+
return array_values($imagesUrls);
306330
}
307331

308332
/**

tests/ImagesBlacklistTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
class ImagesBlacklistTest extends PHPUnit_Framework_TestCase
3+
{
4+
public function testPlainText()
5+
{
6+
$info = Embed\Embed::create('http://www.lopinion.fr/24-mars-2015/cartes-chance-sont-epuisees-22609', [
7+
'adapter' => [
8+
'config' => [
9+
'imagesBlacklist' => [
10+
'http://www.lopinion.fr/sites/nb.com/files/2015/01/logo-o-default_0.png?p',
11+
],
12+
]
13+
],
14+
'providers' => [
15+
'html' => [
16+
'maxImages' => 0
17+
]
18+
]
19+
]);
20+
21+
$this->assertNull($info->image);
22+
}
23+
24+
public function testPlainUrlMatch()
25+
{
26+
$info = Embed\Embed::create('http://www.lopinion.fr/24-mars-2015/cartes-chance-sont-epuisees-22609', [
27+
'adapter' => [
28+
'config' => [
29+
'imagesBlacklist' => [
30+
'*/logo-o-default_0.png*',
31+
],
32+
]
33+
],
34+
'providers' => [
35+
'html' => [
36+
'maxImages' => 0
37+
]
38+
]
39+
]);
40+
41+
$this->assertNull($info->image);
42+
}
43+
44+
public function testAuthorizedImage()
45+
{
46+
$info = Embed\Embed::create('http://www.lopinion.fr/24-mars-2015/jeunes-restent-angle-mort-politiques-publiques-22607', [
47+
'adapter' => [
48+
'config' => [
49+
'imagesBlacklist' => [
50+
'*/logo-o-default_0.png*',
51+
],
52+
]
53+
],
54+
'providers' => [
55+
'html' => [
56+
'maxImages' => 0
57+
]
58+
]
59+
]);
60+
61+
$this->assertNotNull($info->image);
62+
}
63+
}

0 commit comments

Comments
 (0)