imagepng CREATE a particular PNG image to be shown in a browser or stored in a file.
This function creates a PNG 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 fail to output the image, this function returns TRUE.
This function returns TRUE on success os FALSE on failure.
Portable Network Graphics (PNG)
is a raster-graphics file-format that supports lossless data compression.
PNG was developed as an improved, non-patented replacement for Graphics Interchange Format, (GIF).
PNG supports palette-based images with palettes of 24-bit RGB or 32-bit RGBA colors, grayscale images with or without alpha channel for transparency, and full-color non-palette-based RGB/RGBA images with or without alpha channe).
The PNG working group designed the format for transferring images on the Internet, not for professional-quality print graphics, and therefore it does not support non-RGB color spaces such as CMYK.
A PNG file contains a single image in an extensible structure of "chunks", encoding the basic pixels and other information such as textual comments and integrity checks documented in RFC 2083.
PNG files nearly always use the file extension PNG or png and are assigned MIME media type image/png.
PNG was published as informational RFC 2083 in March 1997 and as an ISO/IEC standard in 2004.
. . . . . .
From Wikipedia, the free encyclopedia.
<?php
bool imagepng ( GdImage $image,
resource|string|null $file = null,
int $quality = -1,
int $filters = -1 )
where,
$image = The image identifier
$file = The path or stream resource
( SEE the below TABLE )
$quality = The compression level
$filters = To control the reducing the file size
?>
$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 |
$file = null is invalid if
$quality and
$filters are not used.
$quality
The compression level to be used.
QUALITY |
VALUE |
MEANING |
-1 |
USES ZLIB COMPRESSION |
0 |
NO COPRESSION |
: : : : : : : : |
9 |
MAXIMUM COMPRESSION |
ed48 |
$filters
To be used to reduce the PNG file size.
PNG FILTERS |
CONSTANT |
VALUE |
MEANING |
DEFAULT |
-1 |
DISABLE FILTERING |
PNG_NO_FILTER |
0 |
DISABLE ALL FILTERS |
PNG_FILTER_NONE |
8 |
The line scan is transmitted unchanged. |
PNG_FILTER_SUB |
16 |
Transmits the difference between each byte and the value of the corresponding byte before the pixel. |
PNG_FILTER_UP |
32 |
As a sub-filter, except that the pixel immediately above the current pixel is used for forecasting, rather than the leftmost pixel. |
PNG_FILTER_AVG |
64 |
Uses the average of the pixels above and below neighboring the current to predict the value of your pixel. |
PNG_FILTER_PAETH |
128 |
It calculates a simple linear function of the three neighboring pixels, (left, top, top left) and then chooses the nearest neighbor of the calculated value as a predictor. Technique developed by Alan W. Paeth. |
PNG_ALL_FILTERS |
248 |
ACTIVATE ALL FILTERS |
ed48 |
EXERCISE
<?php
$img01 = imagecreatetruecolor(200, 200);
// truecolor image
$blue = imagecolorallocate($img01, 0, 0, 255);
// it doesn't matter if the first color is provided
$red = imagecolorallocate($img01, 255, 0, 0);
$img_name = "png/PNG 001 01.png";
// normal link
$bool01 = imagepng($img01, $img_name);
if ($bool01 == true)
{
echo '$bool01 = ' . $bool01 .
'<br><br>The image has been created!<br><br>';
echo '<img src="' . $img_name .
'" alt="' . $img_name . '" title="' . $img_name . '">';
}
else
{
echo '$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 PNG 001 01.pngThe image has been created!Note that the background for images created by imagecreatetruecolor will always be black , regardless of the first color allocated by imagecolorallocate.
EXERCISE
<?php
$img02 = imagecreatetruecolor(200, 200);
// truecoor image
$blue = imagecolorallocate($img02, 0x0, 0x0, 0xff);
// it doesn't matter if the first color is provided
$red = imagecolorallocate($img02, 0xFF, 0x0, 0x0);
$quality02 = 6;
$filtr02 = PNG_FILTER_AVG;
$img_name = "png/PNG 002 02.png";
// normal link
$bool02 = imagepng($img02,
$img_name, $quality02, $filtr02);
if ($bool02 == true)
{
echo '$bool02 = ' . $bool02 .
'<br><br>The image has been created!<br><br>';
echo '<img src="' . $img_name .
'" alt="' . $img_name . '" title="' . $img_name . '">';
}
else
{
echo '$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
NORMAL LINK
- - - - - - - - - - - - - - - - - - - - - - - - - - - */
?>
RESULT PNG 002 02.pngThe image has been created!Note that the background for images created by imagecreatetruecolor will always be black , regardless of the color allocated by imagecolorallocate.
EXERCISE
<?php
define ('P2PNGNW', 'file://' . __DIR__ . '/png/');
echo __DIR__ . '<br><br>';
// stream wrapper link
$img03 = imagecreate(200, 200);
// palette image
$green = imagecolorallocate($img03, 0, 255, 0);
// background color
$blue = imagecolorallocate($img03, 0, 0, 255);
$red = imagecolorallocate($img03, 255, 0, 0);
$quality03 = 9;
$filtr03 = PNG_FILTER_PAETH;
$img_name = "PNG 003 03.png";
var_dump($img03);
$bool03 = imagepng($img03, P2PNGNW .
$img_name, $quality03, $filtr03);
if ($bool03 == true)
{
echo '<br><br>$bool03 = ' . $bool03 .
'<br><br>The image has been created!<br><br>';
echo '<img src="png/' . $img_name .
'" alt="' . $img_name . '" title="' . $img_name . '">';
}
else
{
echo '<br><br>$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
P2PNGNW => PATH to the image folder
- - - - - - - - - - - - - - - - - - - - - - - - - - - */
?>
RESULT PNG 003 03.pngThe image has been created!Note that the background for images created by imagecreate will always be the same as the first color defined by imagecolorallocate, in this case green .
EXERCISE
<?php
$img04 = imagecreate(200, 200);
// palette image
$red = imagecolorallocate($img04, 255, 0, 0);
// background color
$green = imagecolorallocate($img04, 0, 255, 0);
$blue = imagecolorallocate($img04, 0, 0, 255);
$quality04 = 9;
$filtr04 = 248;
// PNG_ALL_FILTERS
$img_name = "png/PNG 004 04.png";
// normal link
var_dump($img04);
$bool04 = imagepng($img04, $img_name, $quality04, $filtr04);
if ($bool04 == true)
{
echo '<br><br>$bool04 = ' . $bool04 .
'<br><br>The image has been created!<br><br>';
echo '<img src="' . $img_name .
'" alt="' . $img_name . '" title="' . $img_name . '">';
}
else
{
echo '<br><br>$bool04 = ' . $bool04 .
'<br><br>The image was NOT created!';
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - -
$bool04 return:
1 (TRUE) if the image WAS created
or 0 (FALSE) if the image WAS NOT created
- - - - - - - - - - - - - - - - - - - - - - - - - - - */
?>
RESULT PNG 004 04.pngThe image has been created!Note that the background for images created by imagecreate will always be the same as the first color defined by imagecolorallocate, in this case red .
EXERCISE
<?php
header('Content-type: image/png');
$img05 = imagecreatetruecolor(200, 200);
$blue = imagecolorallocate($img05, 0, 0, 255);
$red = imagecolorallocate($img05, 255, 0, 0);
$quality05 = 6;
$filtr05 = PNG_FILTER_AVG;
imagepng($img05, NULL, $quality05, $filtr05);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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
$img06 = imagecreatetruecolor(200, 200);
$blue = imagecolorallocate($img06, 0, 0, 255);
$red = imagecolorallocate($img06, 255, 0, 0);
$quality06 = 6;
$img_name06 = null;
$filtr06 = PNG_FILTER_AVG;
imagepng($img06, $img_name06, $quality06, $filtr06);
?>