Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to enable or disable interlace using imageinterlace() function in PHP?
The imageinterlace() function is a built-in PHP function used to enable or disable interlacing in images. Interlacing encodes bitmap images so that partially loaded images display as degraded copies of the entire image, allowing users to see portions as the image loads progressively.
Non-interlaced JPEGs appear line-by-line during loading, while interlaced images show a blurred version that becomes clearer. This function controls this behavior by setting the interlace parameter to 1 (enable) or 0 (disable).
Syntax
int imageinterlace(resource $image, int $interlace)
Parameters
The imageinterlace() function accepts two parameters:
$image − The image resource to be modified
$interlace − Integer value (1 to enable, 0 to disable interlacing)
Return Value
Returns 1 if the interlace bit is set for the image, otherwise returns 0.
Example 1: Enabling Interlace
This example demonstrates enabling interlacing on a JPEG image −
<?php
// Create image from file
$img = imagecreatefromjpeg('/path/to/image.jpg');
// Enable interlacing
imageinterlace($img, 1);
// Output image with interlacing enabled
header('Content-type: image/jpeg');
imagejpeg($img);
imagedestroy($img);
?>
Example 2: Disabling Interlace
This example shows how to disable interlacing −
<?php
// Create image from file
$img = imagecreatefromjpeg('/path/to/image.jpg');
// Disable interlacing
imageinterlace($img, 0);
// Output image without interlacing
header('Content-type: image/jpeg');
imagejpeg($img);
imagedestroy($img);
?>
Checking Current Interlace Status
You can check if an image has interlacing enabled by calling the function with only the image parameter −
<?php
// Create a sample image
$img = imagecreate(100, 100);
// Check current interlace status (default is 0)
$status = imageinterlace($img);
echo "Current interlace status: " . $status . "
";
// Enable interlacing
imageinterlace($img, 1);
$newStatus = imageinterlace($img);
echo "New interlace status: " . $newStatus;
imagedestroy($img);
?>
Current interlace status: 0 New interlace status: 1
Conclusion
The imageinterlace() function provides control over image progressive loading behavior. Enable it for web images to improve user experience during slow loading, or disable it for faster processing when progressive display isn't needed.
