Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: getUrl('conversion_name') always returns .jpg when using ->keepOriginalImageFormat() #3786

Open
mauricekindermann opened this issue Feb 13, 2025 · 3 comments

Comments

@mauricekindermann
Copy link

When using keepOriginalImageFormat() on a conversion, while the conversion itself correctly generates files with the original extension (e.g., .png), the URLs generated by getUrl() still use .jpg extension.

The cause seems to be part of the Conversion constructor - it sets a default format('jpg') manipulation which isn't removed when keepOriginalImageFormat() is called.

I cannot PR this, but I suspect the fix is something like this

class Conversion
{ 
  public function keepOriginalImageFormat(): self
  {
      $this->keepOriginalImageFormat = true;
      $this->removeManipulation('format');  // Add this line
      return $this;
  }
}
@mauricekindermann mauricekindermann changed the title Bug:getUrl('conversion_name') always returns .jpg when using ->keepOriginalImageFormat() Bug: getUrl('conversion_name') always returns .jpg when using ->keepOriginalImageFormat() Feb 13, 2025
@mauricekindermann
Copy link
Author

For now I am doing this since I have a mix of imported media with original file extensions, and the default jpg conversions (which is a big "gotcha" in this library). And I have 500k files/conversions so it's not trivial to fix this in production.

class Media extends BaseMedia
{
  public function getCorrectUrl(string $conversionName = ''): string
      {
          if ($conversionName === '') {
              return $this->getUrl();
          }
      
          $url = $this->getUrl($conversionName);
          $path = $this->getPath($conversionName);
      
          // If jpg conversion doesn't exist
          if (!file_exists($path)) {
              $originalExt = pathinfo($this->file_name, PATHINFO_EXTENSION);
              $originalPath = str_replace('.jpg', '.'.$originalExt, $path);
              
              // Check if original extension version exists
              if (file_exists($originalPath)) {
                  return str_replace('.jpg', '.'.$originalExt, $url);
              }
              
              // If neither exists, return original file URL
              return $this->getUrl();
          }
      
          return $url;
      }
}

@freekmurze
Copy link
Member

I'd be happy to merge in a PR that fixes this + added tests 👍

@mauricekindermann
Copy link
Author

I will see what I can do, will take me a few weeks at best.

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

No branches or pull requests

2 participants