I am looking to try and create a small image converter that would convert HEIC files that are uploaded to a php web document to .JPG (or any other generic file format).
I am running PHP off a unix server and have ImageMagick installed on the server. The following command line code works from the server:
mogrify -format jpg *.HEIC
I’d like to convert this command line code to PHP.
As mentioned I like to convert the command line formatting code to PHP. I currently have the following code set up in a basic HTML + PHP form. The file being converted is newly uploaded and not located on the server. If necessary I can upload to the server first then read form the server file.
if($_SERVER["REQUEST_METHOD"] == "POST")
{
if(empty($_FILES['image_url']['name']))
{
echo "No File uploaded";
}
else{
$uploadedImage = fopen($_FILES['image_url']['tmp_name'], 'rb');
$image_to_convert = new Imagick();
$image_to_convert->readImageFile($uploadedImage);
$image_to_convert->setFormat("jpg");
$image_to_convert->setFileName('test.jpg');
header('Content-Type: image/jpg');
header('Content-disposition: attachment; filename='.$image_to_convert->getFileName());
header("Content-Description: File Transfer");
readfile($image_to_convert);
}
}
This code downloads a "test.jpg" file, but when I try to open it in Windows image viewer it displays a "It looks like we don’t support this file format" message. I’m relatively new to PHP so I don’t know all the tricks for output/input streams so if my code is horrible let me know.
Any and all help is welcome. Thanks!
2
Answers
Managed to get it working after I changed from image formats like png and jpeg, and instead downloaded the converted HEIC image as a .PDF file.
I also had to upload (write) the PDF file to the server first before I was able to download (read) it through the website.
I think you need to specify ‘jpeg’ rather than ‘jpg’ for your format.
$image_to_convert->setFormat("jpeg");