我试图了解在浏览器中显示图像的方法。这有效:
echo '<img src="data:image/jpeg;base64,'. base64_encode($photoData) .'" />';
但这不是:
$image = imagecreatefromstring($photoData);
header('Content-Type: image/jpeg');
imagejpeg($image);
它显示数据而不是图像。谁能解释我错过的东西?
答案 0 :(得分:0)
尝试以下代码:
<?php
header('Content-Type: image/jpeg');
$exploded = explode(',', $photoData, 2); // limit to 2 parts, i.e: find the first comma
$encoded = $exploded[1]; // pick up the 2nd part
$decoded = base64_decode($encoded);
$im = imagecreatefromstring($decoded);
$width = "500px";
$height = "400px";
$im2 = imagecrop($im, ['x' => 0, 'y' => 0, 'width' => $width, 'height' => $height]);
imagejpeg($im2);
// Free up memory
imagedestroy($im2);
?>
图片裁剪示例 imagecreatefromstring using image with data:image/png;base64,
确保:
您的页面仅打印图像,因为如果它包含任何字符串或其他错误,则不会显示图像。
我用这个参考来解决你的问题, confluent-kafka-dotnet