PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8
imageinterlace() 函数用于设置图像的交错模式。交错模式使得图像在传输时能逐渐显示出来,即图像在加载时逐步清晰。这通常用于优化图像的网络传输,尤其是对于大图像。
bool imageinterlace(resource $image, bool $interlace = true);
返回布尔值,若设置成功则返回 true,若失败则返回 false。
以下是使用 imageinterlace() 函数的一个示例代码:
<?php // 创建图像资源 $image = imagecreatefromjpeg('example.jpg'); <p>// 设置交错模式<br> $image_interlaced = imageinterlace($image, true);</p> <p>if ($image_interlaced) {<br> echo '图像交错模式已启用';<br> } else {<br> echo '无法启用图像交错模式';<br> }</p> <p>// 输出并保存图像<br> imagejpeg($image, 'output_example.jpg');</p> <p>// 释放内存<br> imagedestroy($image);<br> ?><br>
在此示例中,我们首先使用 imagecreatefromjpeg() 函数加载一张 JPEG 图片。接着,通过 imageinterlace() 启用交错模式,成功后会输出“图像交错模式已启用”。然后,使用 imagejpeg() 函数将修改后的图像保存为新的文件,并且释放图像资源以节省内存。