如何在PHP中创建和显示图像
我想用PHP显示一个图像。这是我尝试过的,但不起作用如何在PHP中创建和显示图像,php,html,image,function,Php,Html,Image,Function,我想用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(
<!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'];
echo img_block($img_name);
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;
}
您正在调用函数,但没有使用返回值执行任何操作,您需要回显它:
img_block($img_name);
应该是:
$img_name = $row['img_name'];
echo img_block($img_name);
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;
}
最后,您没有将所需的$
放在函数中的img\u src
变量上;它的定义应该是:
$img_name = $row['img_name'];
echo img_block($img_name);
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;
}
尝试将
$img\u input
更改为$img\u input=“./images/…”