Skip to content

fix ImageManager::compressUntilSize() - #2200

Open
blued-gear wants to merge 4 commits into
mainfrom
fix/compress_until_size
Open

fix ImageManager::compressUntilSize()#2200
blued-gear wants to merge 4 commits into
mainfrom
fix/compress_until_size

Conversation

@blued-gear

Copy link
Copy Markdown
Collaborator

The loop checks if the compression had any effect, but it does so with the original file size. If the image got compressed in the first iteration, the condition then would always be false.

Comment thread src/Service/ImageManager.php Outdated
public function compressUntilSize(string $filePath, string $extension, int $maxBytes): bool
{
if (-1 === $this->imageCompressionQuality || filesize($filePath) <= $maxBytes) {
if (-1 === (int)$this->imageCompressionQuality || filesize($filePath) <= $maxBytes) {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IDE showed me that this comparison would always fail as -1 is an int and imageCompressionQuality is a float

$quality = $this->imageCompressionQuality;
}
while ($bytes > $maxBytes && $quality > 0.1) {
while ($bytes > $maxBytes && $quality > 0.3) {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it to 0.3 to shorten the loop. IMO even at this quality level the images would be well fried already.

@BentiGorlich BentiGorlich left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am approving this, but have you though about adding a test? If this code might be flawed from the beginning, maybe that would be a good idea

public function compressUntilSize(string $filePath, string $extension, int $maxBytes): bool
{
if (-1 === $this->imageCompressionQuality || filesize($filePath) <= $maxBytes) {
if (-1 === (int) $this->imageCompressionQuality || filesize($filePath) <= $maxBytes) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you put -1 in the env file, I believe it might be converted to an int? At least that was my idea

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants