imageinterlace

(PHP 4, PHP 5, PHP 7, PHP 8)

imageinterlace启用或禁用隔行扫描

说明

imageinterlace(GdImage $image, ?bool $enable = null): bool

imageinterlace() 打开或关闭隔行扫描位(bit)。

如果设置了隔行扫描位(interlace bit)并且图像被用作 JPEG,将会创建为渐进式 JPEG 图像。

参数

image

由图象创建函数(例如imagecreatetruecolor())返回的 GdImage 对象。

interlace

如果为 true,则图像开启隔行扫描,如果为 false,否则将关闭隔行扫描。传递 null 不会让隔行扫描行为发生改变。

返回值

如果为图像设置了隔行扫描位,则返回 true,否则返回 false

更新日志

版本 说明
8.0.5 imageinterlace() 现在返回 bool;之前返回 int(非零为隔行扫描图像,否则为 0)
8.0.0 image 现在需要 GdImage 实例;之前需要有效的 gd resource
8.0.0 enable 现在接受 bool;之前接受 int

示例

示例 #1 使用 imageinterlace() 打开隔行扫描

<?php
// 创建图像实例
$im = imagecreatefromgif('php.gif');

// 打开隔行扫描
imageinterlace($im, true);

// 保存隔行扫描图像
imagegif($im, './php_interlaced.gif');
imagedestroy($im);
?>

添加备注

用户贡献的备注 11 notes

up
0
info at dvorakj dot com
4 years ago
This function will be great only after that browsers will stop sending next data if it is already sent enought to display them on users’s screen due to its possibilities and more pixels to send are absolutely useless. This is the current purpose of jpeg progressivity and still not implemented. After that, you doesn’t have to create many versions of pictures for different densities of screens and it will save a lot of things.
up
0
razvan_bc at yahoo dot com
4 years ago
this function really saved my day !!!

well for testing i should upload several jpg some had jfif encoding ,
so some are color losing when i include them in fpdf ,tcpdf (i tryed 2 open sources to minimize errors to know wht's wrong) .
the thumb function i used now (fixed) is

public static function jpgtofilethumb($file,$thumb,$lun,$quality){
//de la php.net
list ($x, $y) = @getimagesize ($file);
$img = @imagecreatefromjpeg ($file);
if ($x > $y) {
$tx = $lun;
$ty = round($lun / $x * $y);
} else {
$tx = round($lun / $y * $x);
$ty = $lun;
}
$thb = imagecreatetruecolor ($tx, $ty);

// Enable interlancing
if(imageistruecolor($thb)) imageinterlace($thb, true);

imagecopyresampled ($thb,$img, 0,0, 0,0, $tx,$ty, $x,$y);
imagejpeg ($thb, $thumb, $quality);
imagedestroy ($thb);
imagedestroy ($img);
}
up
0
Gerry Danen
17 years ago
It was suggested that this function can be used to retrieve the interlace bit of an image that is stored in a file. This is not the case.
While imageinterlace() returns 0 or 1 if a valid Image resource is passed, passing a file name as a string results in a PHP warning and the return value is neither 0 nor 1.
up
0
mangobrain.co.uk
18 years ago
dr_snapid's comment that "the server sends every Nth line" is not entirely true. A web server need not know anything about the contents of the file it is sending; its job is simply to send the data. Rather, the image is created in such a way that the data corresponding to "every Nth line" appears at the beginning of the file, with the details becoming able to be filled in as more of the file is received by the browser. In PHP's case, the data may have been generated dynamically instead of pulled from a file, but this does not change the fact that it is the data itself that is different, not the manner in which it is sent.*

In fact, with JPEG, it is less "every Nth line", and more "every Nth pixel", where N is gradually decreased, resulting in a grid that gets progressively more fine-grained as the data is received (hence the appearance of a low res image becoming more detailed). The browser basically estimates what goes in the gaps between pixels, probably by simply blending between the colours, whilst the "real" data continues arriving. This is a fundamentally different method for encoding the data when compared to non-progressive JPEGs, and coupled with the format's other compression techniques, may indeed result in a different file size.

*Can you imagine how much more buggy the Web would be if servers were expected to send different file types using different algorithms, and browsers were expected to be able to receive every one of them?
up
0
thomas dot brandl at barff dot de
19 years ago
Set imageinterlace() to 0 if you need to load the generated images in Flash. Flash does not support progressive JPEGs
up
-1
julien / at / theoconcept.com
18 years ago
This function is useful when working with Ming, as SWFBitmap constructor will use a NON INTERLACED Jpeg file, so you have to use imageinterlace(0);
up
-1
mironto at mironto dot sk
20 years ago
just to add my 5 cents on the progressive principle of jpeg: there are no several lowres images stored in jpeg along with original picture, the only thing altered is the order of "pixels". in jpeg the image is divided in areas 8x8 pixels, so instead of linear order of pixels (row-by-row), first there is one pixel form each 8x8 area included in the begining of the image data stream, so when the browser recieves all 8x8 area pixels, it can display "pixelate" image and as soon as it recieves more data, the browser can add more pixels and "sharpen" the image.
up
-1
manuel.warum at edu.uni-klu.ac.at
20 years ago
About MichaelSoft's note "Imageinterlace($im, 1) creates a JPG which is first loaded completely before showing anything":

Actually, that's not completely true.
This only happens with Internet Explorer (any version, for the time being) as it doesn't seem to support progressive displaying and rather shows the image, when it's 100% done with loading. Other browsers (Mozilla, Mozilla Firefox, Opera, Konqueror, etc.) do their job as they're supposed to do: Displaying a very low-res image, then overlaying a midlow-res image (while loading), and then displaying more and more details.
up
-2
ben dash xo at dubplates dot org
20 years ago
There is a bug in Microsoft Internet Explorer (at least at present) that means that often, a progressive/interlaced JPEG will actually NOT show at all whilst loading, suddenly appearing only when the entire picture has loaded. A regular NON-interlaced/NON-progressive JPEG will display line by line as it loads, which paradoxically gives the illusion that it's loading faster. MSIE definitely has this one backwards!!

This behaviour is not apparent in other browsers such as Mozilla/FireFox - in these browsers, the image loads progressively, as it should.
up
-3
dr_snapid at mxm dot com dot au
18 years ago
Interlacing doesnt store another image, it simple changes the order in which the images lines are sent and rendered. The server sends every Nth line, reaches the end, then goes back to the start, reading the lines in between.

After each pass the browser displays the downloaded lines, plus filles the lines not yet received the same, but with each pass the gaps being filled get smaller and the image sharpens. After several passes every line has been read, and the browser has rendered the image in full detail.

Hope that makes sense, it does explain why there should not really be any difference in filesize, so I cant explain why some people have observed a file size difference.

As I understand it, there is only 1 bit in the file which says if its interlaced or not, and the server and client (browser) just handle it differently if it is set to 1.
up
-5
drake127
19 years ago
Interlancing works also with PNG files but it increase filesize (from 14.4M to 17.7M).
To Top