imagejpeg
(PHP 4, PHP 5, PHP 7)
imagejpeg - 将图像输出到浏览器或文件
描述
bool imagejpeg ( resource $image [, mixed $to [, int $quality ]] )
imagejpeg()从给定的文件中创建一个JPEG文件image
。
参数
`image`
一个图像资源,由图像创建函数之一返回,如imagecreatetruecolor()。
to
路径或打开的流资源(在此函数返回后自动关闭)将文件保存到。如果没有设置或者NULL
,原始图像流将被直接输出。
要跳过此参数以提供quality
参数,请使用NULL
。
quality
quality
是可选的,范围从0(最差质量,较小文件)到100(最好质量,最大文件)。默认值是默认的IJG质量值(约75)。
返回值
返回TRUE
成功或失败时返回FALSE
。
例子
Example #1 Outputting a JPEG image to the browser
<?php
//?Create?a?blank?image?and?add?some?text
$im?=?imagecreatetruecolor(120,?20);
$text_color?=?imagecolorallocate($im,?233,?14,?91);
imagestring($im,?1,?5,?5,??'A?Simple?Text?String',?$text_color);
//?Set?the?content?type?header?-?in?this?case?image/jpeg
header('Content-Type:?image/jpeg');
//?Output?the?image
imagejpeg($im);
//?Free?up?memory
imagedestroy($im);
?>
上面的例子会输出类似于:
Example #2 Saving a JPEG image to a file
<?php
//?Create?a?blank?image?and?add?some?text
$im?=?imagecreatetruecolor(120,?20);
$text_color?=?imagecolorallocate($im,?233,?14,?91);
imagestring($im,?1,?5,?5,??'A?Simple?Text?String',?$text_color);
//?Save?the?image?as?'simpletext.jpg'
imagejpeg($im,?'simpletext.jpg');
//?Free?up?memory
imagedestroy($im);
?>
Example #3 Outputting the image at 75% quality to the browser
<?php
//?Create?a?blank?image?and?add?some?text
$im?=?imagecreatetruecolor(120,?20);
$text_color?=?imagecolorallocate($im,?233,?14,?91);
imagestring($im,?1,?5,?5,??'A?Simple?Text?String',?$text_color);
//?Set?the?content?type?header?-?in?this?case?image/jpeg
header('Content-Type:?image/jpeg');
//?Skip?the?to?parameter?using?NULL,?then?set?the?quality?to?75%
imagejpeg($im,?NULL,?75);
//?Free?up?memory
imagedestroy($im);
?>
注释
注意:如果要输出渐进式JPEG,则需要使用imageinterlace()设置隔行扫描。
更新日志
版 | 描述 |
---|---|
5.4.0 | 增加了对将流资源传递给to的支持。 |
5.4.0 | 不允许将空字符串传递给跳过此参数。 |
← imageistruecolor
imagelayereffect →
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com