Skip to content

Commit 97d8542

Browse files
author
Bradie Tilley
committed
Add better support for Arrayable options when resizing, add html find/replace twig filter
1 parent 32e3299 commit 97d8542

File tree

3 files changed

+85
-3
lines changed

3 files changed

+85
-3
lines changed

Plugin.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Artisan;
1212
use DB;
1313
use Event;
14+
use Illuminate\Contracts\Support\Arrayable;
1415
use Illuminate\Database\QueryException;
1516
use Symfony\Component\Console\Output\ConsoleOutput;
1617
use System\Classes\PluginBase;
@@ -41,12 +42,36 @@ public function registerMarkupTags()
4142
'resize' => function ($image, $width, $height = null, $options = []) {
4243
$resizer = new Resizer((string) $image);
4344

44-
return $resizer->resize((int) $width, (int) $height, (array) $options);
45+
$width = ($width !== null) ? (int) $width : null;
46+
$height = ($height !== null) ? (int) $height : null;
47+
$options = ($options instanceof Arrayable) ? $options->toArray() : (array) $options;
48+
49+
return $resizer->resize($width, $height, $options);
4550
},
4651
'modify' => function ($image, $options = []) {
4752
$resizer = new Resizer((string) $image);
4853

49-
return $resizer->resize(null, null, (array) $options);
54+
$width = null;
55+
$height = null;
56+
$options = ($options instanceof Arrayable) ? $options->toArray() : (array) $options;
57+
58+
return $resizer->resize($width, $height, $options);
59+
},
60+
'filterHtmlImageResize' => function ($html, $width, $height = null, $options = []) {
61+
$html = (string) $html;
62+
$width = ($width !== null) ? (int) $width : null;
63+
$height = ($height !== null) ? (int) $height : null;
64+
$options = ($options instanceof Arrayable) ? $options->toArray() : (array) $options;
65+
66+
return Resizer::parseFindReplaceImages($html, $width, $height, $options);
67+
},
68+
'filterHtmlImageModify' => function ($html, $options = []) {
69+
$html = (string) $html;
70+
$width = null;
71+
$height = null;
72+
$options = ($options instanceof Arrayable) ? $options->toArray() : (array) $options;
73+
74+
return Resizer::parseFindReplaceImages($html, $width, $height, $options);
5075
}
5176
]
5277
];

classes/Resizer.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,4 +933,59 @@ public static function clearFiles(Carbon $minAge = null, string $directory = nul
933933

934934
return count($files);
935935
}
936+
937+
/**
938+
* Parse a given HTML string for images and replace them with the resized copies as per the given modifications.
939+
*
940+
* CAUTION: Experimental
941+
*
942+
* This uses regex to find and replace HTML content which is often frowned upon. You may supply your own custom
943+
* regexes, or you may rely on the defaults (which may change in future versions of this plugin soo beware).
944+
*
945+
* By default this will search img elements with a src, data-src or lazy-src attribute, as well as any "style"
946+
* attribute with a background or background-image CSS rule (of which contains a "url()" to an image)
947+
*
948+
* Example Usage (a richeditor field that contains custom embedded images that require OTF optimisation or resizing):
949+
* {{ service.description | filterHtmlImageResize(600, 600, { mode: 'contain' }) }}
950+
* {{ service.description | filterHtmlImageModifiy({ quality: 60 }) }}
951+
*
952+
* @param string $html The HTML to find/replace images
953+
* @param int|null $width
954+
* @param int|null $height
955+
* @param array $options
956+
* @param array $regexes List of regexes (keys) and callbacks (values) to use in the preg_replace_callback.
957+
* @param int $limit See preg_replace_callback $limit docs
958+
* @return string The same HTML but with images replaced with their resized URL equivalents
959+
*/
960+
public static function parseFindReplaceImages(
961+
string $html,
962+
int $width = null,
963+
int $height = null,
964+
array $options = [],
965+
array $regexes = null,
966+
int $limit = 255
967+
): string {
968+
$regexes = ($regexes !== null) ? $regexes : [
969+
'/(<img [^>]*(?:src|data-src|lazy-src)=)"([^"]+)"/' => function ($match) use ($width, $height, $options) {
970+
$resizer = new Resizer((string) $match[2]);
971+
972+
$url = $resizer->resize($width, $height, $options);
973+
974+
return $match[1] . '"' . $url . '"';
975+
},
976+
'/(style="([^"]*)background(-image)?:(\s*[^"]+)?url\(\'?)(.+?)(\'?\))"/' => function ($match) use ($width, $height, $options) {
977+
$resizer = new Resizer((string) $match[2]);
978+
979+
$url = $resizer->resize($width, $height, $options);
980+
981+
return $match[1] . $url . $match[6];
982+
},
983+
];
984+
985+
foreach ($regexes as $regex => $callback) {
986+
$html = preg_replace_callback($regex, $callback, $html, $limit);
987+
}
988+
989+
return $html;
990+
}
936991
}

updates/version.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,6 @@
3838
2.1.6:
3939
- Fix migration (2.1.2 migration) that causes installations to fail (due to plugin settings not existing for new projects at time of execution)
4040
2.1.7:
41-
- Fix - Update plugin's boot method to not directly reference Settings until DB connection is established
41+
- Fix - Update plugin's boot method to not directly reference Settings until DB connection is established
42+
2.1.8:
43+
- Added twig filter to parse and resize/modify images (via regex) using twig filter(s)

0 commit comments

Comments
 (0)