Skip to content
Advertisement

mime_content_type(): Invalid path

I am creating a picture with text on it like this:

createImage.php

function randExer() {
    //Creates a black picture
    $img = imagecreatetruecolor(200, 50);

    //uses RGB-values to create a useable color
    $textColor = imagecolorallocate($img, 255, 255, 255);
    $linesColor = imagecolorallocate($img, 192, 192, 192);

    //Adds text
    imagestring($img, 5, 18, 18, "four + five = ?", $textColor);

    $rotate = imagerotate($img, $angle, 0);

    ob_start();
        imagejpeg($rotate);
        $contents = ob_get_contents();
    ob_end_clean();
    $imageData = base64_encode($contents);
    $src = "data:" . mime_content_type($contents) . ";base64," . $imageData;
    return "<img alt='' src='" . $src . "'/>";
}

Result

To actually show the created image in html, I use this:

<label id='exercise'>
    <?php
        echo randExer();
    ?>'
</label><br>

The functionality itself is working fine and the result looks like this – which is how it is supposed to look:

enter image description here

Problem

The problem is that I get the following warning:

Warning: mime_content_type(): Invalid path in createImage.php on line 19

I don’t really know what’s wrong with the path.

What is the problem here?

Advertisement

Answer

mime_content_type requires path to the tested file, not just base64 string. But why are you trying to recognize mime type when you know it is image/jpeg (by imagejpeg function).

$src = "data:image/jpeg;base64," . $imageData;
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement