@@ -33,6 +33,15 @@ public static function getFullSizeWidth() {
3333 return 2560 ;
3434 }
3535
36+ /**
37+ * Returns all sizes from getSizes(), plus the 'full' option (2560px)
38+ */
39+ public static function getSizesWithFull () {
40+ $ sizes = self ::getSizes ();
41+ $ sizes ['full ' ] = self ::getFullSizeWidth ();
42+ return $ sizes ;
43+ }
44+
3645 public static function getPNGMaxSize () {
3746 return 3 * 1024 * 1024 ; // 3MB in bytes
3847 }
@@ -201,8 +210,8 @@ function (Environment $env, $image, $size = 'medium') use ($image_filters) {
201210 * Migration status: Mixed (legacy files still exist, new uploads use WordPress format)
202211 *
203212 * Example of legacy Timmy file names:
204- * custom_palette -1-150x0 -c-default.png rgb_palette-1-1200x0 -c-default.png rgb_palette-1-20x0 -c-default.png rgb_palette-1-790x0 -c-default.png
205- * rgb_palette-1-10x0-c-default.png rgb_palette-1-150x0-c-default.png rgb_palette-1-2560x0-c-default.png
213+ * rgb_palette -1-1200x0 -c-default.png rgb_palette-1-20x0-c-default.png rgb_palette-1-790x0 -c-default.png rgb_palette-1-10x0-c-default.png rgb_palette-1-150x0 -c-default.png rgb_palette-1-2560x0 -c-default.png
214+ * rgb_palette-1-1200x1412.jpg rgb_palette-1-1200x630.jpg
206215 *
207216 * Example of new (wordpress default) file names:
208217 * europawahlen-1-1200x630.jpg europawahlen-1-150x100.jpg europawahlen-1-2560x1713.jpg europawahlen-1-768x514.jpg
@@ -223,6 +232,10 @@ function image_downsize_fallback($out, $attachment_id, $size) {
223232 return false ;
224233 }
225234
235+ if ($ size === 'full-width ' ) {
236+ $ size = 'full ' ;
237+ }
238+
226239 if (!is_string ($ size ) || $ size === 'regular ' ) {
227240 $ size = 'medium ' ;
228241 }
@@ -240,10 +253,17 @@ function image_downsize_fallback($out, $attachment_id, $size) {
240253 return $ legacy_image ;
241254 }
242255
243- return getScaledImage ( $ metadata , $ pathinfo , $ upload_dir ) ;
256+ return false ;
244257}
245258
246259function hasRequestedImageSize ($ metadata , $ size , $ upload_dir , $ pathinfo ) {
260+ if ($ size === 'full ' && str_contains ($ metadata ['file ' ], '-scaled ' )) {
261+ $ full_file = trailingslashit ($ upload_dir ['basedir ' ]) . $ metadata ['file ' ];
262+ if (file_exists ($ full_file )) {
263+ return true ;
264+ }
265+ }
266+
247267 if (isset ($ metadata ['sizes ' ][$ size ])) {
248268
249269 $ size_data = $ metadata ['sizes ' ][$ size ];
@@ -295,25 +315,19 @@ function find_legacy_image($attachment_id, $size) {
295315 }
296316
297317 if ($ width === false ) {
298- if ($ size === 'full ' ) {
299- return false ;
300- }
301- else {
302- $ width = isset (LesVertsImages::getSizes ()[$ size ]) ?
303- LesVertsImages::getSizes ()[$ size ] :
304- LesVertsImages::getSizes ()['regular ' ];
305- }
318+ $ width = isset (LesVertsImages::getSizesWithFull ()[$ size ]) ?
319+ LesVertsImages::getSizesWithFull ()[$ size ] :
320+ LesVertsImages::getSizes ()['medium ' ];
306321 }
307322
308323 // Remove any existing WordPress dimensions from filename
309324 $ clean_name = str_replace ('-scaled ' , '' , preg_replace ('/-\d+x\d+$/ ' , '' , $ base_name ));
310- $ search_pattern = $ base_path . '/ ' . $ clean_name . '- ' . $ width . '*-c-default. ' . $ extension ;
311- $ legacy_files = glob ($ search_pattern );
312-
313- if (empty ($ legacy_files )) {
314- $ search_pattern = $ base_path . '/ ' . $ clean_name . '- ' . $ width . '*-c-center.jpg ' ;
315- $ legacy_files = glob ($ search_pattern );
316- }
325+ $ pattern_dim = $ base_path . '/ ' . $ clean_name . '-*[0-9]x[0-9]*. ' . $ extension ;
326+ $ pattern_scaled = $ base_path . '/ ' . $ clean_name . '-scaled. ' . $ extension ;
327+ // Find matching files for both patterns
328+ $ files_dim = glob ($ pattern_dim );
329+ $ files_scaled = glob ($ pattern_scaled );
330+ $ legacy_files = array_merge ($ files_dim , $ files_scaled );
317331
318332 if (!empty ($ legacy_files )) {
319333 // Find the best matching file based on extracted width
@@ -355,12 +369,12 @@ function select_legacy_file_by_width($legacy_files, $target_width, $size) {
355369 }
356370
357371 if (!$ target_width ) {
358- $ sizes = LesVertsImages::getSizes ();
372+ $ sizes = LesVertsImages::getSizesWithFull ();
359373 if (array_key_exists ($ size , $ sizes )) {
360374 $ target_width = $ sizes [$ size ];
361375 }
362376 else {
363- $ target_width = LesVertsImages::getSizes ()['regular ' ];
377+ $ target_width = LesVertsImages::getSizes ()['medium ' ];
364378 }
365379 }
366380
@@ -399,52 +413,25 @@ function select_legacy_file_by_width($legacy_files, $target_width, $size) {
399413 return $ best_match ?: $ legacy_files [$ biggest_image_index ];
400414}
401415
402- /**
403- * Find image file with "-scaled" in filename
404- * These are generated by Wordpress if the image is bigger than the "full" size we defined
405- */
406- function getScaledImage ($ metadata , $ pathinfo , $ upload_dir ) {
407- if (str_contains ($ pathinfo ['filename ' ], '-scaled ' )) {
408- $ scaled_file = trailingslashit ($ upload_dir ['basedir ' ]) . $ pathinfo ['dirname ' ] . '/ ' . $ pathinfo ['basename ' ];
409- if (file_exists ($ scaled_file )) {
410- return false ;
411- }
412-
413- $ original_url = trailingslashit ($ upload_dir ['baseurl ' ]) . $ pathinfo ['dirname ' ] . '/ ' . $ metadata ['original_image ' ];
414- $ original_file = trailingslashit ($ upload_dir ['basedir ' ]) . $ pathinfo ['dirname ' ] . '/ ' . $ metadata ['original_image ' ];
415-
416- if (file_exists ($ original_file )) {
417- return [
418- $ original_url ,
419- $ metadata ['width ' ], // width
420- $ metadata ['height ' ], // height
421- false // original image
422- ];
423- }
424- }
425-
426- return false ;
427- }
428-
429416/**
430417 * Build srcset for legacy images
431418 * If the names are not in wordpress default format, we need to build the srcset manually
432419 */
433420function buildLegacySrcset ($ image_id ) {
434- $ srcset_parts = [];
435-
436- foreach (LesVertsImages::getSizes () as $ size_name => $ width ) {
421+ $ srcset_map = [];
422+ foreach (LesVertsImages::getSizesWithFull () as $ size_name => $ width ) {
437423 $ legacy_result = find_legacy_image ($ image_id , $ size_name );
438424
439425 if ($ legacy_result && is_array ($ legacy_result )) {
440- $ url = $ legacy_result [0 ];
426+ $ url = esc_url ( $ legacy_result [0 ]) ;
441427 $ actual_width = $ legacy_result [1 ];
442428
443429 if ($ url && $ actual_width ) {
444- $ srcset_parts [ ] = esc_url ( $ url) . ' ' . $ actual_width . ' w ' ;
430+ $ srcset_map [ $ url ] = " $ url { $ actual_width} w " ;
445431 }
446432 }
447433 }
434+ $ srcset_parts = array_values ($ srcset_map );
448435
449436 return !empty ($ srcset_parts ) ? implode (', ' , $ srcset_parts ) : '' ;
450437}
0 commit comments