PHP imagejpeg Function: Output JPEG Image to Browser or File

The article explains PHP's imagejpeg() function, detailing its parameters, return values, and usage example for creating and outputting a JPEG image directly to the browser or saving it to a file, including optional quality settings.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP imagejpeg Function: Output JPEG Image to Browser or File

imagejpeg() outputs an image resource as a JPEG file, either sending it directly to the browser or saving it to a specified filename.

Parameters

image – The image resource returned by functions such as imagecreatetruecolor().

filename – Path to save the file; if omitted or NULL, the JPEG data is sent to the output buffer.

quality – Optional integer from 0 (worst) to 100 (best); default is the IJG library default (~75).

Return value

Returns TRUE on success, FALSE on failure.

Example

<?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 to image/jpeg
header('Content-Type: image/jpeg');
// Output the image
imagejpeg($im);
// Free memory
imagedestroy($im);
?>
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendImage ProcessingPHPJPEGimagejpeg
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.