php - 如何在 PHP 中创建和显示图像
问题描述
我想使用 PHP 显示图像。这是我尝试过的,但它不起作用。
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
</head>
<body>
<div class="container">
<?php
$img_name = echo $row['img_name']; //I get it from the database
img_block($img_name); //Display the image here.
//Function to display image
function img_block(img_src) {
// e.g. img_src = cat.jpg;
$img_input = "images/" . img_src;
$set_img = '<img class="media-object-ph" src="'.$img_input.'" width="380" height="290" alt="...">';
return $set_img;
}
?>
</div>
</body>
</html>
先感谢您。
解决方案
评论太长了...您有许多错误:
$img_name = echo $row['img_name'];
应该:
$img_name = $row['img_name'];
您正在调用您的函数,但没有对返回值做任何事情,您需要回显它:
img_block($img_name);
应该:
echo img_block($img_name);
最后,您还没有将 required$
放在img_src
函数中的变量上;它的定义应该是:
function img_block($img_src) {
// e.g. img_src = cat.jpg;
$img_input = "images/" . $img_src;
$set_img = '<img class="media-object-ph" src="'.$img_input.'" width="380" height="290" alt="...">';
return $set_img;
}
如果您进行所有这些更改,并且 (eg) $row['img_name'] = 'image1.jpg'
,您的代码将输出:
<img class="media-object-ph" src="images/image1.jpg" width="380" height="290" alt="...">