How to convert HEIF to JPG in PHP?

I’ve developed a PHP script for uploading images, wherein the uploaded image undergoes a validation check to determine its format (jpeg/png). If it’s not in jpeg format but is a valid image, the script proceeds to convert it into jpeg.

Recently, Apple introduced HEIC file types, and I’m encountering challenges in adapting my script to handle this new format. The issue lies in the absence of a specific mime type for HEIC, complicating the integration into my existing script.

I’m seeking insights from anyone who has successfully addressed the dilemma of uploading HEIC files and subsequently converting them to jpeg using PHP. If you’ve come across a solution or workaround, your guidance would be greatly appreciated.

1 Like

Dealing with HEIC files in PHP can be a bit tricky given the format’s relatively recent introduction and its limited support. A viable solution might be to use the Imagick PHP extension. This extension is capable of processing various image formats, including HEIC.

It can convert HEIC files to JPEG. You will need to ensure that Imagick is installed on your server and that it supports the HEIC format.

Could you elaborate on how to use Imagick for converting HEIC to JPEG? How would I integrate this into my existing PHP script for image processing?

You could write a PHP function utilizing Imagick for this conversion. This function would accept the HEIC file path and the output path for the JPEG file. In this function, create a new Imagick object with the HEIC file, change the image format to ‘jpeg’, and save the image to the specified output path.
This method will convert the HEIC image to JPEG. You can call this function in your script when handling HEIC file uploads. It’s also important to include error handling, especially for scenarios where Imagick might not support the HEIC format.