MaestroError / php-heic-to-jpg

The easiest way to convert HEIC images to JPEG with PHP and Laravel framework
MIT License
148 stars 20 forks source link

How to display in html #36

Closed Tsjippy closed 7 months ago

Tsjippy commented 8 months ago

Converting yo a jp file works fine, but below code snippet does not:

`$jpg = \Maestroerror\HeicToJpg::convert($path)->get();

    $base64=base64_encode($jpg);
    echo "<img src='data:image/jpeg;base64, $base64' alt='An elephant' />";`

It leads to a complete white page.

what to do?

Tsjippy commented 8 months ago

I did some more testing if I export a heic as a jpeg file, my computer can see it just fine. But if not wit file_get_contents. If I open the jpeg image and save it again in a picture editor it displays fine

MaestroError commented 8 months ago

Hello, @Tsjippy! Thanks for reaching out and going through it while testing 👍

I couldn't find any solution from the given information. Have you tried setting a header like: header("content-type: image/jpeg");?

I didn't understand what you said in "But if not wit file_get_contents.", please tell me more about it.

Can you provide an image you were using in testing, please? Also, if there are any error messages in any process you use, please post them here too 👍

Tsjippy commented 8 months ago

Thanks for your reply

After some more testing i found a picture that actually works. This is the code I use:

    $path   = "C:/xampp/htdocs/simnigeria/wp-content/uploads/test2.heic";
    $dest   = "C:/xampp/htdocs/simnigeria/wp-content/uploads/test2.jpg";

    if(\Maestroerror\HeicToJpg::isHeic($path)){

        // 1. save as file
        try{
            $result = \Maestroerror\HeicToJpg::convert($path)->saveAs($dest);
        }catch (\Exception $e) {
            return explode(':', $e->getMessage())[0];
        }
        // 2. get content (binary) of converted JPG
        $jpg = \Maestroerror\HeicToJpg::convert($path)->get();

        $base64=base64_encode($jpg);
        echo "<img src='data:image/jpeg;base64, $base64'/>";
    }

For the picture test1 the code works, but for the picture test2 it does not, somehow it leads to a white page, but the code is executed fine.

I cannot upload heic files so this is the onedrive link to get them: link

I can view the resulting test2.jpg in the browser if I supply it as a link: echo "<img src='$url' />";

I cannot view the image if I display the image using file_get_contents:

file_get_contents($dest);
$base64=base64_encode($jpg);
echo "<img src='data:image/jpeg;base64, $base64'/>";
MaestroError commented 8 months ago

Hi! I downloaded your images and copied your code. I tried both your images, in both cases and it works just fine. Here is my code for your "test.heic":

<?php

include "src/HeicToJpg.php";

$path   = "test-binary-bad.heic";
$dest   = "test-binary-bad.jpg";

if(Maestroerror\HeicToJpg::isHeic($path)) {
    // 1. save as file
    try{
        $result = \Maestroerror\HeicToJpg::convert($path)->saveAs($dest);
    }catch (\Exception $e) {
        return explode(':', $e->getMessage())[0];
    }

    // Works
    $jpg = file_get_contents($dest);
    $base64=base64_encode($jpg);
    echo "<img src='data:image/jpeg;base64, $base64'/>";

    // Works
    $jpg = \Maestroerror\HeicToJpg::convert($path)->get();
    $base64=base64_encode($jpg);
    echo "<img src='data:image/jpeg;base64, $base64'/>";
}

So, I am not sure how to help you.

P.s. I was testing on windows platform + PHP 8.2.5

Tsjippy commented 7 months ago

Found the issue: it was Wordpress. If the base64 string is to big it fails. Reducing the image with

$jpg    = \Maestroerror\HeicToJpg::convert($path)->get();

$size   = getimagesizefromstring($jpg);

// reduce size, as we do not need super big images
if($size[0] > 1024 || $size[1] > 1024){
    $img        = imagecreatefromstring($jpg);
    $imgResized = imagescale($img , 1024);

    ob_start (); 

    imagejpeg ($imgResized);
    $jpg = ob_get_contents (); 

    ob_end_clean (); 
}
$base64 = base64_encode($jpg);

return "data:image/jpeg;base64, $base64";

Resolved the issue

MaestroError commented 7 months ago

Happy to hear that! Thanks for reporting it here @Tsjippy ❤️