imagejpeg

Output image to browser or file

Syntax

imagejpeg(
    GdImage $image,
    resource|string|null $file = null,
    int $quality = -1
): bool

Parameters

image

A GdImage object, returned by one of the image creation functions, such as imagecreatetruecolor().

file

The path or an open stream resource (which is automatically closed after this function returns) to save the file to. If not set or null, the raw image stream will be output directly.

quality

quality is optional, and ranges from 0 (worst quality, smaller file) to 100 (best quality, biggest file). The default (-1) uses the default IJG quality value (about 75).

Return

Returns true on success or false on failure. CautionHowever, if libgd fails to output the image, this function returns true.

Examples

1 · image

<?

$width = 100;
$height = 100;
$image = imagecreatetruecolor($width, $height);

$return = imagejpeg($image);

var_export($return);

?>
���JFIF``��>CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), default quality
��C        

 $.' ",#(7),01444'9=82<.342��C            

2!!22222222222222222222222222222222222222222222222222��dd"��    
���}!1AQa"q2���#B��R��$3br�    
%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz������������������������������������������������������������������������    
���w!1AQaq"2�B����    #3R�br�
$4�%�&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz�����������������������������������������������������������������������?���(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(���true

2 · file

<?

$width = 100;
$height = 100;
$image = imagecreatetruecolor($width, $height);

$file = $_SERVER['DOCUMENT_ROOT'] . '/assets/jpg/1.jpg';

$return = imagejpeg($image, $file);

var_export($return);

?>
true

3 · quality

<?

$width = 100;
$height = 100;
$image = imagecreatetruecolor($width, $height);

$file = $_SERVER['DOCUMENT_ROOT'] . '/assets/jpg/1.jpg';
$quality = 100;

$return = imagejpeg($image, $file, $quality);

var_export($return);

?>
true

4 · header

<?

$width = 100;
$height = 100;
$image = imagecreatetruecolor($width, $height);

header("content-type: image/jpeg");

imagejpeg($image);

?>
���JFIF``��>CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), default quality
��C        

 $.' ",#(7),01444'9=82<.342��C            

2!!22222222222222222222222222222222222222222222222222��dd"��    
���}!1AQa"q2���#B��R��$3br�    
%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz������������������������������������������������������������������������    
���w!1AQaq"2�B����    #3R�br�
$4�%�&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz�����������������������������������������������������������������������?���(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(���

5 · path

<?

$width = 100;
$height = 100;
$image = imagecreatetruecolor($width, $height);

$path = "/assets/jpg/1.jpg";
$file = $_SERVER['DOCUMENT_ROOT'] . $path;
imagejpeg($image, $file);

echo '<!doctype html>
<html>
<body>
    <img src="' . $path . '">
</body>
</html>';

?>
<!doctype html>
<html>
<body>
    <img src="/assets/jpg/1.jpg">
</body>
</html>

6 · base64

<?

$width = 100;
$height = 100;
$image = imagecreatetruecolor($width, $height);

ob_start();

    imagejpeg($image);

$output = ob_get_clean();

echo '<!doctype html>
<html>
<body>
    <img src="data:image/jpeg;base64,' . base64_encode($output) . '">
</body>
</html>';

?>
<!doctype html>
<html>
<body>
    <img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD//gA+Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcgSlBFRyB2NjIpLCBkZWZhdWx0IHF1YWxpdHkK/9sAQwAIBgYHBgUIBwcHCQkICgwUDQwLCwwZEhMPFB0aHx4dGhwcICQuJyAiLCMcHCg3KSwwMTQ0NB8nOT04MjwuMzQy/9sAQwEJCQkMCwwYDQ0YMiEcITIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIy/8AAEQgAZABkAwEiAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6g4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2drh4uPk5ebn6Onq8fLz9PX29/j5+v/EAB8BAAMBAQEBAQEBAQEAAAAAAAABAgMEBQYHCAkKC//EALURAAIBAgQEAwQHBQQEAAECdwABAgMRBAUhMQYSQVEHYXETIjKBCBRCkaGxwQkjM1LwFWJy0QoWJDThJfEXGBkaJicoKSo1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoKDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uLj5OXm5+jp6vLz9PX29/j5+v/aAAwDAQACEQMRAD8A+f6KKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKAP/9k=">
</body>
</html>

gd_info

getimagesize

getimagesizefromstring

image_type_to_extension

image_type_to_mime_type

imageaffine

imageaffinematrixconcat

imageaffinematrixget

imagealphablending

imageantialias

imagearc

imageavif

imagebmp

imagechar

imagecharup

imagecolorallocate

imagecolorallocatealpha

imagecolorat

imagecolorclosest

imagecolorclosestalpha

imagecolorclosesthwb

imagecolordeallocate

imagecolorexact

imagecolorexactalpha

imagecolormatch

imagecolorresolve

imagecolorresolvealpha

imagecolorset

imagecolorsforindex

imagecolorstotal

imagecolortransparent

imageconvolution

imagecopy

imagecopymerge

imagecopymergegray

imagecopyresampled

imagecopyresized

imagecreate

imagecreatefromavif

imagecreatefrombmp

imagecreatefromgif

imagecreatefromjpeg

imagecreatefrompng

imagecreatefromstring

imagecreatefromtga

imagecreatefromwbmp

imagecreatefromwebp

imagecreatefromxbm

imagecreatefromxpm

imagecreatetruecolor

imagecrop

imagecropauto

imagedashedline

imagedestroy

imageellipse

imagefill

imagefilledarc

imagefilledellipse

imagefilledpolygon

imagefilledrectangle

imagefilltoborder

imagefilter

imageflip

imagefontheight

imagefontwidth

imageftbbox

imagefttext

imagegammacorrect

imagegetclip

imagegetinterpolation

imagegif

imageinterlace

imageistruecolor

imagelayereffect

imageline

imageloadfont

imageopenpolygon

imagepalettecopy

imagepalettetotruecolor

imagepng

imagepolygon

imagerectangle

imageresolution

imagerotate

imagesavealpha

imagescale

imagesetbrush

imagesetclip

imagesetinterpolation

imagesetpixel

imagesetstyle

imagesetthickness

imagesettile

imagestring

imagestringup

imagesx

imagesy

imagetruecolortopalette

imagettfbbox

imagettftext

imagetypes

imagewbmp

imagewebp

imagexbm

iptcembed

iptcparse

HomeMenu