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.
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);
?>Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
