Skip to content

Commit 47d537e

Browse files
Merge pull request #6 from pressidium/next
1.0.1
2 parents 38531c0 + 56c2abc commit 47d537e

File tree

12 files changed

+352
-37
lines changed

12 files changed

+352
-37
lines changed

composer.lock

Lines changed: 19 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

includes/Optimizations/Image/Converters/Avif_Converter.php

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,56 @@ public function set_quality( int $quality ): Avif_Converter {
115115
return $this; // chainable
116116
}
117117

118+
/**
119+
* Return a unique filename suffix to avoid overwriting existing files.
120+
*
121+
* @throws Image_Conversion_Exception If a unique filename could not be generated after multiple attempts.
122+
*
123+
* @param string $dir_name Directory name.
124+
* @param string $filename Filename without extension.
125+
* @param string $extension File extension.
126+
*
127+
* @return string
128+
*/
129+
private function get_unique_suffix( string $dir_name, string $filename, string $extension ): string {
130+
$base_suffix = 'optimized';
131+
$suffix = $base_suffix;
132+
133+
// Initial destination path
134+
$destination_path = sprintf(
135+
'%s/%s%s.%s',
136+
$dir_name,
137+
$filename,
138+
$suffix,
139+
$extension
140+
);
141+
142+
$max_attempts = 5;
143+
$counter = 1;
144+
145+
while ( file_exists( $destination_path ) ) {
146+
$suffix = sprintf( '%s_%d', $base_suffix, $counter );
147+
148+
$destination_path = sprintf(
149+
'%s/%s%s.%s',
150+
$dir_name,
151+
$filename,
152+
$suffix,
153+
$extension
154+
);
155+
156+
++$counter;
157+
158+
if ( $counter > $max_attempts ) {
159+
throw new Image_Conversion_Exception(
160+
'Could not generate a unique filename for the converted image.'
161+
);
162+
}
163+
}
164+
165+
return $suffix;
166+
}
167+
118168
/**
119169
* Convert the specified image.
120170
*
@@ -128,17 +178,25 @@ public function convert(): Image {
128178
throw new Image_Conversion_Exception( 'Image is not loaded' );
129179
}
130180

131-
$destination_url = sprintf(
132-
'%s.%s',
133-
preg_replace( '/\.[^.]+$/', '', $this->image->get_url() ),
181+
$path_info = pathinfo( $this->image->get_path() );
182+
$suffix = $this->get_unique_suffix(
183+
$path_info['dirname'],
184+
$path_info['filename'],
134185
self::EXTENSION
135186
);
136187

137-
$path_info = pathinfo( $this->image->get_path() );
138188
$destination_path = sprintf(
139-
'%s/%s.%s',
189+
'%s/%s_%s.%s',
140190
$path_info['dirname'],
141191
$path_info['filename'],
192+
$suffix,
193+
self::EXTENSION
194+
);
195+
196+
$destination_url = sprintf(
197+
'%s_%s.%s',
198+
preg_replace( '/\.[^.]+$/', '', $this->image->get_url() ),
199+
$suffix,
142200
self::EXTENSION
143201
);
144202

includes/Optimizations/Image/Converters/Webp_Converter.php

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,56 @@ public function set_quality( int $quality ): Webp_Converter {
115115
return $this; // chainable
116116
}
117117

118+
/**
119+
* Return a unique filename suffix to avoid overwriting existing files.
120+
*
121+
* @throws Image_Conversion_Exception If a unique filename could not be generated after multiple attempts.
122+
*
123+
* @param string $dir_name Directory name.
124+
* @param string $filename Filename without extension.
125+
* @param string $extension File extension.
126+
*
127+
* @return string
128+
*/
129+
private function get_unique_suffix( string $dir_name, string $filename, string $extension ): string {
130+
$base_suffix = 'optimized';
131+
$suffix = $base_suffix;
132+
133+
// Initial destination path
134+
$destination_path = sprintf(
135+
'%s/%s%s.%s',
136+
$dir_name,
137+
$filename,
138+
$suffix,
139+
$extension
140+
);
141+
142+
$max_attempts = 5;
143+
$counter = 1;
144+
145+
while ( file_exists( $destination_path ) ) {
146+
$suffix = sprintf( '%s_%d', $base_suffix, $counter );
147+
148+
$destination_path = sprintf(
149+
'%s/%s%s.%s',
150+
$dir_name,
151+
$filename,
152+
$suffix,
153+
$extension
154+
);
155+
156+
++$counter;
157+
158+
if ( $counter > $max_attempts ) {
159+
throw new Image_Conversion_Exception(
160+
'Could not generate a unique filename for the converted image.'
161+
);
162+
}
163+
}
164+
165+
return $suffix;
166+
}
167+
118168
/**
119169
* Convert the specified image.
120170
*
@@ -128,17 +178,25 @@ public function convert(): Image {
128178
throw new Image_Conversion_Exception( 'Image is not loaded' );
129179
}
130180

131-
$destination_url = sprintf(
132-
'%s.%s',
133-
preg_replace( '/\.[^.]+$/', '', $this->image->get_url() ),
181+
$path_info = pathinfo( $this->image->get_path() );
182+
$suffix = $this->get_unique_suffix(
183+
$path_info['dirname'],
184+
$path_info['filename'],
134185
self::EXTENSION
135186
);
136187

137-
$path_info = pathinfo( $this->image->get_path() );
138188
$destination_path = sprintf(
139-
'%s/%s.%s',
189+
'%s/%s_%s.%s',
140190
$path_info['dirname'],
141191
$path_info['filename'],
192+
$suffix,
193+
self::EXTENSION
194+
);
195+
196+
$destination_url = sprintf(
197+
'%s_%s.%s',
198+
preg_replace( '/\.[^.]+$/', '', $this->image->get_url() ),
199+
$suffix,
142200
self::EXTENSION
143201
);
144202

includes/Optimizations/Image/Image_Metadata_Manager.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
namespace Pressidium\WP\Performance\Optimizations\Image;
1010

11+
use Pressidium\WP\Performance\Utils\String_Utils;
12+
1113
if ( ! defined( 'ABSPATH' ) ) {
1214
die( 'Forbidden' );
1315
}
@@ -64,7 +66,7 @@ public function update( int $attachment_id, string $size_variant_name, Image $op
6466
$relative_path = $this->ensure_path_is_relative( $optimized_image->get_path() );
6567

6668
if ( $size_variant_name === 'full' ) {
67-
$metadata['file'] = $relative_path;
69+
$metadata['file'] = String_Utils::unleading_slash_it( $relative_path );
6870
$metadata['filesize'] = filesize( $optimized_image->get_path() );
6971
$metadata['mime-type'] = $optimized_image->get_mime_type();
7072
$metadata['is_optimized'] = true;

0 commit comments

Comments
 (0)