11<?php namespace ProcessWire ;
22
3+ use Daun \Image ;
4+ use Daun \Placeholders \PlaceholderAverageColor ;
35use Daun \Placeholders \PlaceholderBlurHash ;
46use Daun \Placeholders \PlaceholderThumbHash ;
57
@@ -33,30 +35,27 @@ static public function getModuleInfo()
3335 public function init ()
3436 {
3537 $ this ->generators = [
36- // PlaceholderNone::class => $this->_('None'),
3738 PlaceholderThumbHash::class => $ this ->_ ('ThumbHash ' ),
3839 PlaceholderBlurHash::class => $ this ->_ ('BlurHash ' ),
39- // PlaceholderAverageColor::class => $this->_('Average Color '),
40+ PlaceholderAverageColor::class => $ this ->_ ('Average color ' ),
4041 // PlaceholderDominantColor::class => $this->_('Dominant Color'),
41- // PlaceholderProcessWire::class => $this->_('Image variant'),
42- // PlaceholderSVG::class => $this->_('SVG'),
4342 ];
4443
45- // On image upload, generate placeholder
46- $ this ->addHookAfter ('FieldtypeImage::savePageField ' , $ this , 'handleImageUpload ' );
47-
4844 // Add settings to image field config screen
4945 $ this ->addHookAfter ('FieldtypeImage::getConfigInputfields ' , $ this , 'addImageFieldSettings ' );
5046
51- // Generate palceholders for existing images on field save
47+ // Generate placeholders for existing images on field save
5248 $ this ->addHookAfter ('FieldtypeImage::savedField ' , $ this , 'handleImageFieldtypeSave ' );
5349
54- // Add `Pageimage.lqip` property that returns the placeholder data uri
50+ // Generate placeholder on image upload
51+ $ this ->addHookAfter ('FieldtypeImage::savePageField ' , $ this , 'handleImageUpload ' );
52+
53+ // Add `$image->lqip` property that returns the placeholder data uri
5554 $ this ->addHookProperty ('Pageimage::lqip ' , function (HookEvent $ event ) {
5655 $ event ->return = $ this ->getPlaceholderDataUri ($ event ->object );
5756 });
5857
59- // Add `Pageimage. lqip($width, $height)` method that returns the placeholder in a given size
58+ // Add `$image-> lqip($width, $height)` method that returns the placeholder in a given size
6059 $ this ->addHookMethod ('Pageimage::lqip ' , function (HookEvent $ event ) {
6160 $ width = (int ) $ event ->arguments (0 ) ?: 0 ;
6261 $ height = (int ) $ event ->arguments (1 ) ?: 0 ;
@@ -72,7 +71,9 @@ public function handleImageUpload(HookEvent $event): void
7271 $ type = $ this ->getPlaceholderType ($ field );
7372 if ($ type && $ images ->count () && !$ page ->hasStatus (Page::statusDeleted)) {
7473 $ image = $ images ->last (); // get the last added images (should be the last uploaded image)
75- $ this ->generateAndSavePlaceholder ($ image );
74+ if ($ this ->isSupportedImageFormat ($ image )) {
75+ $ this ->generateAndSavePlaceholder ($ image );
76+ }
7677 }
7778 }
7879
@@ -91,16 +92,18 @@ public function generateAndSavePlaceholder(Pageimage $image, bool $force = false
9192
9293 protected function getPlaceholderType (Field $ field ): string
9394 {
94- return $ field ->generateLqip ?? '' ;
95+ return $ field ->imagePlaceholderType ?: '' ;
9596 }
9697
9798 protected function getPlaceholder (Pageimage $ image , bool $ checks = false ): array
9899 {
99100 $ type = $ image ->filedata ("image-placeholder-type " );
100101 $ data = $ image ->filedata ("image-placeholder-data " );
101102 if ($ checks ) {
102- $ expectedType = $ this ->getPlaceholderType ($ image ->field );
103- if ($ type !== $ expectedType ) {
103+ $ created = $ image ->filedata ("image-placeholder-created " );
104+ $ current = $ created && $ image ->modified <= $ created ;
105+ $ expected = $ this ->getPlaceholderType ($ image ->field );
106+ if (!$ current || $ type !== $ expected ) {
104107 $ data = null ;
105108 }
106109 }
@@ -112,33 +115,31 @@ protected function setPlaceholder(Pageimage $image, array $placeholder): void
112115 [$ type , $ data ] = $ placeholder ;
113116 $ image ->filedata ("image-placeholder-type " , $ type );
114117 $ image ->filedata ("image-placeholder-data " , $ data );
118+ $ image ->filedata ("image-placeholder-created " , time ());
115119 $ image ->page ->save ($ image ->field ->name , ["quiet " => true , "noHooks " => true ]);
116120 }
117121
118122 protected function generatePlaceholder (Pageimage $ image ): array
119123 {
120- $ type = $ this ->getPlaceholderType ($ image ->field );
121- $ handler = $ this ->getPlaceholderGenerator ($ type );
122- $ placeholder = '' ;
123-
124124 try {
125+ $ type = $ this ->getPlaceholderType ($ image ->field );
126+ $ handler = $ this ->getPlaceholderGenerator ($ type );
125127 $ placeholder = $ handler ::generatePlaceholder ($ image );
128+ return [$ type , $ placeholder ];
126129 } catch (\Throwable $ e ) {
127130 if ($ this ->wire ()->user ->isSuperuser ()) {
128131 $ this ->wire ()->error ("Error generating image placeholder: {$ e ->getMessage ()}" );
129132 }
130133 $ this ->wire ()->log ("Error generating image placeholder: {$ e ->getMessage ()}: {$ e ->getTraceAsString ()}" );
131134 }
132135
133- return [$ type , $ placeholder ];
136+ return [null , null ];
134137 }
135138
136139 protected function getPlaceholderDataUri (Pageimage $ image , int $ width = 0 , int $ height = 0 ): string
137140 {
138141 [$ type , $ placeholder ] = $ this ->getPlaceholder ($ image , false );
139- if (!$ placeholder ) {
140- return '' ;
141- }
142+ if (!$ placeholder ) return '' ;
142143
143144 $ handler = $ this ->getPlaceholderGenerator ($ type );
144145 $ width = $ width ?: $ this ->defaultLqipWidth ;
@@ -172,20 +173,20 @@ protected function createPlaceholdersForField(Field $field, bool $force = false)
172173 $ this ->message (sprintf ($ this ->_ ('Generating missing image placeholders in field `%s` ' ), $ field ->name ));
173174 }
174175
175- $ count = 0 ;
176176 $ total = 0 ;
177+ $ generated = 0 ;
177178 $ pages = $ this ->wire ()->pages ->findMany ("{$ field ->name }.count>0, check_access=0 " );
178179 foreach ($ pages as $ page ) {
179180 $ images = $ page ->getUnformatted ($ field ->name );
180- $ total += $ images ->count ();
181181 foreach ($ images as $ image ) {
182+ $ total ++;
182183 if ($ this ->generateAndSavePlaceholder ($ image , $ force )) {
183- $ count ++;
184+ $ generated ++;
184185 }
185186 }
186187 }
187188
188- $ this ->message (sprintf ($ this ->_ ('Generated %d placeholders of %d images in field `%s` ' ), $ count , $ total , $ field ->name ));
189+ $ this ->message (sprintf ($ this ->_ ('Generated %d placeholders of %d images in field `%s` ' ), $ generated , $ total , $ field ->name ));
189190 }
190191
191192 protected function addImageFieldSettings (HookEvent $ event )
@@ -201,13 +202,14 @@ protected function addImageFieldSettings(HookEvent $event)
201202 $ fs ->name = '_files_fieldset_placeholders ' ;
202203 $ fs ->label = $ this ->_ ('Image placeholders ' );
203204 $ fs ->icon = 'picture-o ' ;
205+ $ fs ->addClass ('InputfieldIsOffset ' );
204206 // $inputfields->insertAfter($fs, $children->first());
205207 $ inputfields ->add ($ fs );
206208
207209 // Placeholder type
208210 /** @var InputfieldRadios $f */
209211 $ f = $ modules ->get ('InputfieldRadios ' );
210- $ f ->name = 'generateLqip ' ;
212+ $ f ->name = 'imagePlaceholderType ' ;
211213 $ f ->label = $ this ->_ ('Placeholder type ' );
212214 $ f ->description = $ this ->_ ('Choose whether this field should generate low-quality image placeholders (LQIP) on upload. ' );
213215 $ f ->icon = 'toggle-on ' ;
@@ -216,19 +218,19 @@ protected function addImageFieldSettings(HookEvent $event)
216218 foreach ($ this ->generators as $ class => $ label ) {
217219 $ f ->addOption ($ class ::$ name , $ label );
218220 }
219- $ f ->value = $ field ->generateLqip ;
221+ $ f ->value = $ field ->imagePlaceholderType ;
220222 $ fs ->add ($ f );
221223
222224 // Generate missing placeholders for existing images
223225 /** @var InputfieldCheckbox $f */
224226 $ f = $ modules ->get ('InputfieldCheckbox ' );
225- $ f ->name = 'generateLqipForExisting ' ;
227+ $ f ->name = 'imagePlaceholdersGenerateMissing ' ;
226228 $ f ->label = $ this ->_ ('Generate missing placeholders ' );
227229 $ f ->description = $ this ->_ ('Placeholders are only generated when uploading new images. ' ) . ' '
228230 . $ this ->_ ('Check the box below and submit the form to batch-generate image placeholders for any existing images in this field. ' );
229231 $ f ->label2 = $ this ->_ ('Generate missing placeholders for existing images ' );
230232 $ f ->collapsed = true ;
231- $ f ->showIf = 'generateLqip !="" ' ;
233+ $ f ->showIf = 'imagePlaceholderType !="" ' ;
232234 $ f ->icon = 'question-circle-o ' ;
233235 $ f ->value = 1 ;
234236 $ f ->checked = false ;
@@ -237,30 +239,40 @@ protected function addImageFieldSettings(HookEvent $event)
237239 // Re-generate all placeholders for existing images
238240 /** @var InputfieldCheckbox $f */
239241 $ f = $ modules ->get ('InputfieldCheckbox ' );
240- $ f ->name = 'generateLqipForAll ' ;
242+ $ f ->name = 'imagePlaceholdersRegenerateAll ' ;
241243 $ f ->label = $ this ->_ ('Re-generate all placeholders ' );
242244 $ f ->description = $ this ->_ ('Check the box below and submit the form to re-generate all placeholders for any existing images in this field. Useful after changing the placeholder type. ' );
243245 $ f ->label2 = $ this ->_ ('Re-generate all placeholders for existing images ' );
244246 $ f ->collapsed = true ;
245- // $f->showIf = 'generateLqipForExisting =1';
246- $ f ->showIf = 'generateLqip !="" ' ;
247+ // $f->showIf = 'imagePlaceholdersGenerateMissing =1';
248+ $ f ->showIf = 'imagePlaceholderType !="" ' ;
247249 $ f ->icon = 'refresh ' ;
248250 $ f ->value = 1 ;
249251 $ f ->checked = false ;
250252 $ fs ->add ($ f );
251253
252- // generateLqipForExisting
254+ // imagePlaceholdersGenerateMissing
253255 }
254256
255257 protected function handleImageFieldtypeSave (HookEvent $ event )
256258 {
257259 /** @var FieldtypeImage $fieldtype */
258260 $ field = $ event ->arguments (0 );
259261
260- if ($ field ->generateLqipForAll ) {
262+ if ($ field ->imagePlaceholdersRegenerateAll ) {
261263 $ this ->createPlaceholdersForField ($ field , true );
262- } else if ($ field ->generateLqipForExisting ) {
264+ } else if ($ field ->imagePlaceholdersGenerateMissing ) {
263265 $ this ->createPlaceholdersForField ($ field , false );
264266 }
265267 }
268+
269+ protected function isSupportedImageFormat (Pageimage $ image ): bool
270+ {
271+ $ format = Image::getImageType ($ image ->filename );
272+ return in_array ($ format , $ this ->supportedImageFormats ());
273+ }
274+
275+ protected function supportedImageFormats () {
276+ return [\IMAGETYPE_GIF , \IMAGETYPE_JPEG , \IMAGETYPE_PNG ];
277+ }
266278}
0 commit comments