以下是一个使用PHP和GD库来加载和展示图片的实例。我们将创建一个简单的PHP脚本,该脚本将加载一个图片文件,并在网页上显示它。

```php

// 设置图片的路径

$imagePath = 'path/to/your/image.jpg';

// 检查图片文件是否存在

if (file_exists($imagePath)) {

// 获取图片信息

$imageInfo = getimagesize($imagePath);

// 根据图片类型创建相应的图像资源

switch ($imageInfo[2]) {

case IMAGETYPE_JPEG:

$imageResource = imagecreatefromjpeg($imagePath);

break;

case IMAGETYPE_PNG:

$imageResource = imagecreatefrompng($imagePath);

break;

case IMAGETYPE_GIF:

$imageResource = imagecreatefromgif($imagePath);

break;

default:

die('不支持的图片格式');

}

// 设置输出图片的类型

header('Content-Type: ' . $imageInfo['mime']);

// 输出图片

switch ($imageInfo[2]) {

case IMAGETYPE_JPEG:

imagejpeg($imageResource);

break;

case IMAGETYPE_PNG:

imagepng($imageResource);

break;

case IMAGETYPE_GIF:

imagegif($imageResource);

break;

}

// 释放图像资源

imagedestroy($imageResource);

} else {

die('图片文件不存在');

}

>

```

以下是一个表格,展示了上述PHP脚本中使用的函数和它们的作用:

函数名称参数作用
`file_exists()``$filePath`:文件路径检查文件是否存在
`getimagesize()``$filePath`:文件路径获取图像的尺寸和类型信息
`imagecreatefromjpeg()``$filePath`:文件路径从JPEG文件创建图像资源
`imagecreatefrompng()``$filePath`:文件路径从PNG文件创建图像资源
`imagecreatefromgif()``$filePath`:文件路径从GIF文件创建图像资源
`header()``$string`:要发送的HTTP头部信息发送原始的HTTP头部信息
`imagejpeg()``$imageResource`:图像资源将图像输出为JPEG文件
`imagepng()``$imageResource`:图像资源将图像输出为PNG文件
`imagegif()``$imageResource`:图像资源将图像输出为GIF文件
`imagedestroy()``$imageResource`:图像资源释放图像资源,防止内存泄漏

确保将`$imagePath`变量设置为实际图片文件的路径。这个脚本将会在用户访问该PHP文件时显示指定的图片。