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

The file extension does not change after changing the file type in minimagick! #2714

Open
gomo opened this issue Dec 12, 2023 · 2 comments
Open

Comments

@gomo
Copy link

gomo commented Dec 12, 2023

I want to convert heic to jpg using Mini magick, but it seems that process convert: :jpg does not allow to specify quality, so I created a method and tried to convert it using minimagick!.

class FileItemUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick

  process :convert_to_jpeg, if: :heic?

  def filename
    @secure_name ||= "#{SecureRandom.uuid}.#{file.extension}"
    @secure_name
  end

  private

    def heic?(new_file)
      new_file.content_type == 'image/heic'
    end

    def convert_to_jpeg
      minimagick! do |image|
        image
          .quality(80)
          .convert('jpg')
      end
    end
end

The file type is converted to jpg, but the filename extension remains heic.

CarrierWave::SanitizedFile(self.file)inconvert_to_jpegchanges both content_type and file extension to jpg, butCarrierWave::Storage:::Fileinfilename Fog::File(self.file)` is heic.

What I found strange is that converting with process convert: :jpg also changes the extension. It's pretty much the same as the convert source, but my method didn't change it. How can I change it?

def convert(format, page=nil, &block)
minimagick!(block) do |builder|
builder = builder.convert(format)
builder = builder.loader(page: page) if page
builder
end
end

@gomo gomo changed the title The file name extension does not change after changing the file type in minimagick! The file extension does not change after changing the file type in minimagick! Dec 12, 2023
@gomo
Copy link
Author

gomo commented Dec 13, 2023

I forgot to write the versions.

  • rails (7.1.1)
  • carrierwave (3.0.5)

thx

@Ahsanfaiyaz123
Copy link

Solution for Handling HEIC Image Uploads with CarrierWave and MiniMagick

To address issues with displaying HEIC images after upload using CarrierWave, follow these steps to convert HEIC files to JPEG format during the upload process:

Update ImageUploader

Ensure HEIC is included in the list of allowed file extensions:

def extension_white_list
  %w(jpg jpeg gif png heic)
end

Implement Conversion Method

Add the following method to your ImageUploader to convert HEIC images to JPEG:

process :convert_heic_to_jpeg , if: :heic?

private

def heic?(file)
   file.content_type == 'image/heic' || file.extension.downcase == 'heic'
end

def convert_heic_to_jpeg
   cache_stored_file! unless cached?

   temp_path = current_path.sub(/\.\w+$/, '.jpg')
   image = MiniMagick::Image.open(current_path)
   image.format('jpg')
   image.write(temp_path)
   File.rename(temp_path, current_path.sub(/\.heic\z/i, '.jpg'))
end

Explanation

  • extension_white_list: Specifies allowed file extensions (jpg, jpeg, gif, png, heic).
  • convert_heic_to_jpeg: Converts HEIC files to JPEG format during upload using MiniMagick.

This solution ensures compatibility of uploaded HEIC images across various browsers and image processing tools by converting them to JPEG format.

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