@@ -225,6 +225,7 @@ protected function _resize( $max_w, $max_h, $crop = false ) {
225
225
* each new image is created.
226
226
*
227
227
* @since 3.5.0
228
+ * @since 6.1.0 Added the `$unique` parameter.
228
229
*
229
230
* @param array $sizes {
230
231
* An array of image size data arrays.
@@ -241,13 +242,14 @@ protected function _resize( $max_w, $max_h, $crop = false ) {
241
242
* @type bool $crop Optional. Whether to crop the image. Default false.
242
243
* }
243
244
* }
245
+ * @param bool $unique Whether to generate unique file names for the sub-sizes.
244
246
* @return array An array of resized images' metadata by size.
245
247
*/
246
- public function multi_resize ( $ sizes ) {
248
+ public function multi_resize ( $ sizes, $ unique = false ) {
247
249
$ metadata = array ();
248
250
249
251
foreach ( $ sizes as $ size => $ size_data ) {
250
- $ meta = $ this ->make_subsize ( $ size_data );
252
+ $ meta = $ this ->make_subsize ( $ size_data, $ unique );
251
253
252
254
if ( ! is_wp_error ( $ meta ) ) {
253
255
$ metadata [ $ size ] = $ meta ;
@@ -261,6 +263,7 @@ public function multi_resize( $sizes ) {
261
263
* Create an image sub-size and return the image meta data value for it.
262
264
*
263
265
* @since 5.3.0
266
+ * @since 6.1.0 Added the `$unique` parameter.
264
267
*
265
268
* @param array $size_data {
266
269
* Array of size data.
@@ -269,10 +272,11 @@ public function multi_resize( $sizes ) {
269
272
* @type int $height The maximum height in pixels.
270
273
* @type bool $crop Whether to crop the image to exact dimensions.
271
274
* }
275
+ * @param bool $unique Whether to generate unique file names. Default false.
272
276
* @return array|WP_Error The image data array for inclusion in the `sizes` array in the image meta,
273
277
* WP_Error object on error.
274
278
*/
275
- public function make_subsize ( $ size_data ) {
279
+ public function make_subsize ( $ size_data, $ unique = false ) {
276
280
if ( ! isset ( $ size_data ['width ' ] ) && ! isset ( $ size_data ['height ' ] ) ) {
277
281
return new WP_Error ( 'image_subsize_create_error ' , __ ( 'Cannot resize the image. Both width and height are not set. ' ) );
278
282
}
@@ -296,7 +300,7 @@ public function make_subsize( $size_data ) {
296
300
if ( is_wp_error ( $ resized ) ) {
297
301
$ saved = $ resized ;
298
302
} else {
299
- $ saved = $ this ->_save ( $ resized );
303
+ $ saved = $ this ->_save ( $ resized, null , null , $ unique );
300
304
imagedestroy ( $ resized );
301
305
}
302
306
@@ -426,9 +430,11 @@ public function flip( $horz, $vert ) {
426
430
* @since 5.9.0 Renamed `$filename` to `$destfilename` to match parent class
427
431
* for PHP 8 named parameter support.
428
432
* @since 6.0.0 The `$filesize` value was added to the returned array.
433
+ * @since 6.1.0 The `$unique` parameter was added.
429
434
*
430
435
* @param string|null $destfilename Optional. Destination filename. Default null.
431
436
* @param string|null $mime_type Optional. The mime-type. Default null.
437
+ * @param bool $unique Whether the filename should be unique. Default false.
432
438
* @return array|WP_Error {
433
439
* Array on success or WP_Error if the file failed to save.
434
440
*
@@ -440,8 +446,8 @@ public function flip( $horz, $vert ) {
440
446
* @type int $filesize File size of the image.
441
447
* }
442
448
*/
443
- public function save ( $ destfilename = null , $ mime_type = null ) {
444
- $ saved = $ this ->_save ( $ this ->image , $ destfilename , $ mime_type );
449
+ public function save ( $ destfilename = null , $ mime_type = null , $ unique = false ) {
450
+ $ saved = $ this ->_save ( $ this ->image , $ destfilename , $ mime_type, $ unique );
445
451
446
452
if ( ! is_wp_error ( $ saved ) ) {
447
453
$ this ->file = $ saved ['path ' ];
@@ -454,10 +460,12 @@ public function save( $destfilename = null, $mime_type = null ) {
454
460
/**
455
461
* @since 3.5.0
456
462
* @since 6.0.0 The `$filesize` value was added to the returned array.
463
+ * @since 6.1.0 The `$unique` parameter was added.
457
464
*
458
465
* @param resource|GdImage $image
459
466
* @param string|null $filename
460
467
* @param string|null $mime_type
468
+ * @param bool $unique Whether the filename should be unique. Default false.
461
469
* @return array|WP_Error {
462
470
* Array on success or WP_Error if the file failed to save.
463
471
*
@@ -469,11 +477,15 @@ public function save( $destfilename = null, $mime_type = null ) {
469
477
* @type int $filesize File size of the image.
470
478
* }
471
479
*/
472
- protected function _save ( $ image , $ filename = null , $ mime_type = null ) {
480
+ protected function _save ( $ image , $ filename = null , $ mime_type = null , $ unique = false ) {
473
481
list ( $ filename , $ extension , $ mime_type ) = $ this ->get_output_format ( $ filename , $ mime_type );
474
482
475
483
if ( ! $ filename ) {
476
484
$ filename = $ this ->generate_filename ( null , null , $ extension );
485
+ // Only generate a unique name when needed. Without this check, a '-#' suffix is added to all sub sizes.
486
+ if ( $ unique && file_exists ( $ filename ) ) {
487
+ $ filename = $ this ->generate_filename ( null , null , $ extension , true );
488
+ }
477
489
}
478
490
479
491
if ( 'image/gif ' === $ mime_type ) {
@@ -505,6 +517,7 @@ protected function _save( $image, $filename = null, $mime_type = null ) {
505
517
$ stat = stat ( dirname ( $ filename ) );
506
518
$ perms = $ stat ['mode ' ] & 0000666 ; // Same permissions as parent folder, strip off the executable bits.
507
519
chmod ( $ filename , $ perms );
520
+ error_log ( 'filename 2 --- ' . json_encode ( $ filename , JSON_PRETTY_PRINT ));
508
521
509
522
return array (
510
523
'path ' => $ filename ,
0 commit comments