Got a project in mind? Our team is here to support you with tailored Laravel development services.
6 min read
Got a project in mind? Our team is here to support you with tailored Laravel development services.
One of our teammates, Revaz, recently encountered a challenge in his project that he answered with an open-source package that converts HEIC and HEIF images to JPEG format in PHP. Here's a little backstory from Revaz and a bit more info about the package that could also help solve your problems.
I had the opportunity to work on a project called Timewell, a product designed to help users capture and save their memories as pictures and audio files. Users can create unique, personalized memories of their most precious life moments and even create their hardcover albums with unique QR codes. Given the importance of visual content in Timewell, the process of uploading pictures is among the most critical aspects of the project.
As I began the process of uploading pictures, I quickly discovered a bug that was hindering my progress. After some investigation, I realized that the issue stemmed from the formatting of the images. More specifically, the PHP backend could not convert HEIC/HEIF to JPEG, making uploading pictures in these formats impossible.
For those unfamiliar, HEIC and HEIF are modern image file formats that offer superior compression efficiency and image quality compared to older formats like JPEG. They support advanced features such as transparency and depth maps and can store multiple images within a single file. However, since PHP currently does not support these formats, it presented a great challenge for our project.
Identifying the problem was not too hard; however, finding a good solution to the problem - obviously was. As I delved into the research, it became evident that uploading HEIC/HEIF formatted images was a common issue in the Laravel community. Unfortunately, with PHP not supporting the mentioned formatting, there was no clear solution in sight.
However, I recalled how I had previously developed a Composer package called "html-strings-affixer" using the Go language and decided to try the same approach to solve this issue.
At first, I kept my experiment under wraps as I thought it was a risky proposition and wasn't sure whether it would work. So I was burning the midnight oil while working on it. The good thing is that I already had a test version the next day. And to my surprise, it worked just fine when I tested it in the project.
The solution to the problem was technically simple. It involved creating an executable file that worked by taking two arguments: the source image in HEIC format and the desired output file name in JPG format. The file then converted the source image to JPEG and returned the path of the converted image on success. A class named "Maestroerror\HeicToJpg" was implemented to use this functionality in PHP. The "convert" method executed the CLI command on the executable, read the converted image, and deleted it from the file system. To obtain the converted JPG image content, the "get" method could be used, or the "saveAs" method could be called to save the file using PHP's built-in "file_put_contents" function.
Once everything was in order, I uploaded the solution on GitHub as an open-source package. It has gained over 550 downloads, making it apparent that uploading HEIC/HEIF images is a common problem among PHP developers. The package has clearly saved many of them a major headache.
The client's feedback was positive and humbling, which I greatly appreciate. Here is a quote that was especially pleasant to hear:
“I’m really honored to have you on this project with me. You’ve been awesome, and this is a perfect example of how you went above and beyond to find a better solution.”
The client's positive feedback not only boosted my confidence but also validated the effort and time I put into creating the package. Seeing that my solution had significantly impacted and solved a crucial problem in the project was so nice and gratifying.
In short, I am pleased to offer this package to anyone facing similar challenges with uploading HEIC/HEIF images in PHP, and I hope it will help streamline their workflow. To learn more about this package and my experience developing it, please check out my blog post on Medium.
We are a 200+ people agency and provide product design, software development, and creative growth marketing services to companies ranging from fresh startups to established enterprises. Our work has earned us 100+ international awards, partnerships with Laravel, Vue, Meta, and Google, and the title of Georgia’s agency of the year in 2019 and 2021.