imagejpeg CREATE a particular JPEG image to be shown in a browser or stored in a file.
This function creates a JPEG file in $file from the $image which is created by imagecreate or imagecreatetruecolor, for example.
PALETTE IMAGE
is the designation term for images with a small number of colors.
For this type of image, a maximum of 256 colors are accepted.
TRUECOLOR IMAGE
is the designation term for images with a great number of colors.
For this type of image, a maximum of 16,777,216 colors are accepted.
However, if libgd fails to output the image, this function returns TRUE.
This function returns TRUE on success os FALSE on failure.
JPEG (Joint Photographic Experts Group)
a commonly used method of lossy compression for digital images, particularly for those images produced by digital photography.
The degree of compression can be adjusted, allowing a selectable tradeoff between storage size and image quality.
JPEG typically achieves 10:1 compression with little perceptible loss in image quality.
JPEG compression is used in a number of image file formats.
JPEG/Exif is the most common image format used by digital cameras and other photographic image capture devices; along with JPEG/JFIF, it is the most common format for storing and transmitting photographic images on the World Wide Web.
These format variations are often not distinguished, and are simply called JPEG.
The term "JPEG" is an initialism/acronym for the Joint Photographic Experts Group, which created the standard.
The MIME media type for JPEG is image/jpeg, except in older Internet Explorer versions, which provides a MIME type of image/pjpeg when uploading JPEG images.
JPEG files usually have a filename extension of .jpg or .jpeg.
JPEG/JFIF supports a maximum image size of 65,535×65,535=4,294,836,225 pixels, hence up to 4 gigapixels for an aspect ratio of 1:1.
. . . . . .
From Wikipedia, the free encyclopedia.
<?php
bool imagejpeg(GdImage $image,
resource|string|null $file = null,
int $quality = -1)
where,
$image = The image identifier
$file = The path or stream resource
( SEE the below TABLE )
$quality = To control the quality
?>
$image
The image identifier.
$file
The path or stream resource.
$file can reference a local file or, (configuration permitting), a remote file using one of the supported stream wrappers:
STREAM WRAPPERS |
MEANING |
file:// |
LOCAL File System |
http:// |
URL HTTP |
ftp:// |
URL FTP |
php:// |
I/O streams |
zlib:// |
Data Compression stream |
data:// |
DATA (RFC-2397) |
glob:// |
Find Filenames |
phar:// |
PHP Archive |
ssh2:// |
Secure Shell 2 |
rar:// |
RAR |
ogg:// |
Audio |
expect:// |
Interaction of stream processes |
ed48 |
$quality
The quality control.
QUALITY |
VALUE |
MEANING |
-1 |
QUALITY ABOUT 75% |
0 |
SMALLER QUALITY |
: : : : : : : : |
100 |
MAXIMUM QUALITY |
ed48 |
EXERCISE
<?php
$img01 = imagecreate(300, 300);
// palette image
$blue = imagecolorallocate($img01, 0, 0, 255);
// color background
$red = imagecolorallocate($img01, 255, 0, 0);
$img_name = "jpeg/JPEG 001 01.jpg";
// normal link
$bool01 = imagejpeg($img01, $img_name);
var_dump($img01);
if ($bool01 == true)
{
echo '<br><br>$bool01 = ' . $bool01 .
'<br><br>The image has been created!<br><br>';
echo '<img src="' . $img_name .
'" alt="' . $img_name . '" title="' . $img_name . '">';
}
else
{
echo '<br><br>$bool01 = ' . $bool01 .
'<br><br>The image was NOT created!';
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
$bool01 return:
1 (TRUE) if the image WAS created
or 0 (FALSE) if the image WAS NOT created
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
?>
RESULT JPEG 001 01.jpgThe image has been created!Whenever imagecreate is used, and some colors are provided, the background of the created image will have the color of the first color.
EXERCISE
<?php
$img02 = imagecreatetruecolor(300, 300);
// truecolor image
$quality02 = 80;
$img_name = "jpeg/JPEG 002 02.jpg";
// normal link
$bool02 = imagejpeg($img02, $img_name, $quality02);
var_dump($img02);
if ($bool02 == true)
{
echo '<br><br>$bool02 = ' . $bool02 .
'<br><br>The image has been created!<br><br>';
echo '<img src="' . $img_name .
'" alt="' . $img_name . '" title="' . $img_name . '">';
}
else
{
echo '<br><br>$bool02 = ' . $bool02 .
'<br><br>The image was NOT created!';
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
$bool02 return:
1 (TRUE) if the image WAS created
or 0 (FALSE) if the image WAS NOT created
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
?>
RESULT JPEG 002 02.jpgThe image has been created!Note that no color was reported in this exercise, so a BLACK background image was created.
EXERCISE
<?php
define ('P2JPEGWW', 'file://' . getcwd() . '/jpeg/');
echo getcwd() . '<br><br>';
// stream wrapper link
$img03 = imagecreatetruecolor(300, 300);
// truecolor image
$red = imagecolorallocate($img03, 255, 0, 0);
$green = imagecolorallocate($img03, 0, 255, 0);
$blue = imagecolorallocate($img03, 0, 0, 255);
$quality03 = 60;
$img_name = "JPEG 003 03.jpg";
$bool03 = imagejpeg($img03, P2JPEGWW . $img_name, $quality03);
if ($bool03 == true)
{
echo '$bool03 = ' . $bool03 .
'<br><br>The image has been created!<br><br>';
echo '<img src="jpeg/' . $img_name .
'" alt="' . $img_name . '" title="' . $img_name . '">';
}
else
{
echo '$bool03 = ' . $bool03 .
'<br><br>The image was NOT created!';
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - -
$bool03 return:
1 (TRUE) if the image WAS created
or 0 (FALSE) if the image WAS NOT created
P2JPEGWW => PATH to the image folder
- - - - - - - - - - - - - - - - - - - - - - - - - - - */
?>
RESULT JPEG 003 03.jpgThe image has been created!No matter how many colors are provided, imagecreatetruecolor will always generate a BLACK background image.
EXERCISE
<?php
header('Content-type: image/jpeg');
$img04 = imagecreate(50, 50);
$color03 = imagecolorallocate($img04, 36, 255, 24);
imagejpeg($img04);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Image created on-the-fly and not saved
* SENDING TO BROWSER
TO BE USED IF THERE ARE NO PROBLEMS WITH headers
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
?>
EXERCISE
<?php
// Try this code
$img05 = imagecreatetruecolor(300, 300);
// you can test too ...
// $img05 = imagecreate(300, 300);
$color05 = imagecolorallocate($img05, 36, 255, 24);
$quality05 = 70;
$img_name05 = null;
imagejpeg($img05, $img_name05, $quality05);
?>