When I have a form that uses a file upload field that allows for HEIC file uploads, is there a way to automatically convert this file into JPG?
Since WordPress 6.7 the media library allows for HEIC images to be uploaded and automatically converts these into a JPG. https://make.wordpress.org/core/2024/08/15/automatic-conversion-of-heic-images-to-jpeg-in-wordpress-6-7/ This is basically what I would like to happen. In its simplest form I would just like to convert the HEIC file into a JPG right after upload or form submit.
Formidable's file upload field allows you to define the file types for upload. Have you tried HEIC? https://formidableforms.com/knowledgebase/file-upload/#kb-allowed-file-types
Yes, I have. It is configured so that it allows HEIC file types to be uploaded.
The problem is not that it does not allow HEIC file types to be uploaded, but that I would like to have this HEIC file to be converted into JPG automatically. This is - how I understand and experience it - not automatically the case whenever you allow for HEIC files to be uploaded.
Right now, you upload a HEIC file and this file is saved without any conversion. This means that the uses for this file are limited.
Example 1: Whenever you automatically send a confirmation e-mail with the contents of the submitted form, we get a link to the uploaded file. This file is (the original) HEIC format, which means you can't easily view the file.
Example 2: Whenever you would like to use this uploaded image to be viewed inside your website, you would first have to manually convert it.
HEIC conversion in WordPress 6.7 only works if you have the correct PHP library installed on your server. Is ImageMagick installed? It's not installed by default in PHP.
Yes, this is installed correctly. I know, because uploading a HEIC file directly in the media library automatically converts into JPG with HEIC as the original.
The only thing I can suggest is to create a custom PHP function that converts the file after Formidable uploads. You would call the custom routine from the frm_after_create_entry hook. Here’s and example you may be able to use as a starting point. This will not work as is
function custom_heic_to_jpg_conversion( $file ) {
if ( 'image/heic' === $file['type'] || 'image/heif' === $file['type'] ) {
// Check if Imagick extension is available and supports HEIC
if ( ! class_exists( 'Imagick' ) || ! Imagick::queryFormats( 'HEIC' ) ) {
$file['error'] = 'Server does not support HEIC conversion with Imagick.';
return $file;
}
$upload_dir = wp_upload_dir();
$file_path = $upload_dir['path'] . '/' . basename( $file['tmp_name'] );
$new_file_path = str_replace( '.heic', '.jpg', $file_path );
$new_file_path = str_replace( '.heif', '.jpg', $new_file_path );
try {
$imagick = new Imagick();
$imagick->readImage( $file['tmp_name'] );
$imagick->setImageFormat( 'jpeg' ); // Use 'jpeg' instead of 'jpg' for Imagick
$imagick->writeImage( $new_file_path );
$imagick->clear();
$imagick->destroy();
// Update file details for WordPress
$file['tmp_name'] = $new_file_path;
$file['type'] = 'image/jpeg';
$file['name'] = pathinfo( $file['name'], PATHINFO_FILENAME ) . '.jpg';
} catch ( Exception $e ) {
$file['error'] = 'HEIC to JPG conversion failed: ' . $e->getMessage();
}
}
return $file;
}
add_filter( 'wp_handle_upload_prefilter', 'custom_heic_to_jpg_conversion' );
Please login or Register to submit your answer