你混淆了两种不同的东西。
带有图像的网页不contain那个形象一般。相反,它通常指的是外部来源。我说一般来说,因为,是的,一个图像can嵌入到 HTML 页面中,请参见下文。
您有两个选择:
-
您可以创建一个单独的 PHP 文件,在其中创建图像并输出其字节。在您的 HTML 代码中,您引用该图像:
页面.html:
<html>
<body>
<img src="myimage.php" alt="" />
</body>
</html>
myimage.php:
<?php
header("Content-Type: image/png");
createimageandso_on();
// Do the drawing.
?>
-
或者您可以使用 base64 编码将图像嵌入到 HTML 文件中:
<?php
$contents = all_bytes_from_created_image();
// Get the bytes from the created image.
$base64 = base64_encode($contents);
?>
<html>
<body>
<img src="data:image/png;base64,<?php echo $base64; ?>" alt="" />
</body>
</html>
第二个选项适用于较小的图像,因为 Base64 编码的字符串将生成大部分文本。
Edit
如果我理解正确的话,您想从目录中读取图像并将它们调整为相同大小,并将它们用作缩略图?
您可能只想创建一个 PHP 文件,在其中读取源图像并赋予它们相同的大小。
就像“普通”PHP 文件一样,PHP 可以使用您提供的请求参数执行某些操作。也许你曾经见过这样的:
问号后面的字符串(key=value&anotherkey=anothervalue
) 是查询字符串。 PHP 可以对这些值做一些事情:
<?php
echo $_GET['key']; // returns "value"
echo $_GET['anotherkey']; // returns "anothervalue"
?>
现在我们可以在创建图像时做同样的事情。您不必使用几乎相同的代码创建 20 个 PHP 文件,而只需使用一个文件来读取文件(您指定的)并将其大小调整为指定的宽度(您指定的)和高度(您指定的)。
缩略图.php
<?php
// Get some request parameters we're going to use.
// We're expecting the parameters below to exist.
$file = $_GET['filepath'];
$width = $_GET['width'];
$height = $_GET['height'];
// Now we're gonna create the image from the given file.
$src = imagecreatefromjpeg($file);
imagecreatetruecolor($width, $height);
// And the rest of the file reading and image creation.
header("Content-Type: image/jpeg");
imagejpeg($image);
?>
网页.html
<html>
<body>
<?php
$width = 100;
$height = 100;
$files = read_some_directory_and_return_a_list_of_filenames();
foreach ($files as $file) {
// Echo an image tag in the HTML document;
// use as image our thumbnail.php file and give it a query string:
echo "<img src=\"thumbnail.php?width=".$width."&height=".$height."&filepath=".$file."\" alt=\"\" />";
}
?>
</body>
</html>