简体   繁体   English

mime_content_type(): 无效路径

[英]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:要在 html 中实际显示创建的图像,我使用:

<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:功能本身运行良好,结果如下所示 - 它应该是这样的:

在此处输入图片说明

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?这里有什么问题?

mime_content_type requires path to the tested file, not just base64 string. mime_content_type 需要测试文件的路径,而不仅仅是 base64 字符串。 But why are you trying to recognize mime type when you know it is image/jpeg (by imagejpeg function).但是,当您知道它是 image/jpeg(通过 imagejpeg 函数)时,为什么还要尝试识别 mime 类型。

$src = "data:image/jpeg;base64," . $imageData;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM