Convert .HEIC to .JPG using ImageMagick in PHP
P粉322918729
P粉322918729 2023-11-02 17:38:57
0
1
828

I wish to try creating a small image converter that converts HEIC files uploaded to php web documents to .JPG (or any other common file format).

I'm running PHP on a unix server and have ImageMagick installed on the server. The following command line code is run on the server:

mogrify -format jpg *.HEIC

I want to convert this command line code to PHP.

As mentioned before, I like to convert 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 is not on the server. If necessary I can upload to the server first and then read from 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 the "test.jpg" file, but when I try to open it in Windows Image Viewer, it displays the "We don't seem to support this file format" message. I'm relatively new to PHP, so I don't know all the tricks of output/input streams, so if my code is bad, please let me know.

Any and all help welcome. Thanks!

P粉322918729
P粉322918729

reply all(1)
P粉667649253

I think you need to specify "jpeg" instead of "jpg" for the format.

$image_to_convert->setFormat("jpeg");

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template