Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Image Functions : imagepng

imagepng

Output a PNG image to either the browser or a file (PHP 4, PHP 5)
bool imagepng ( resource image [, string filename [, int quality [, int filters]]] )


Related Examples ( Source code ) » imagepng
















Code Examples / Notes » imagepng

php

You could use the function imagecreatefrompng
(I assume that you already know how to get the text from the url.)
The only thing left to do is put that text on the image using the correct colors for you.
<?php
$im = @imagecreatefrompng($imgname);
$text_color = imagecolorallocate ($im, $Red,$Green,$Blue);
imagestring ($im, 3,  5, 15,  $SomeTextFromURL, $text_color);
?>
Regards,
Peter Berkhout.


phpman

When changeing the PHP version from 4 to 5 I found out, that PHP5 handles imagepng() more restrictive than in PHP4. I'd used
imagepng($image,'',90);
to reduce the image quality whithout saving the image as a file. The quality parameter is not supported at all, I used imagejpg before and simply changed the function to imagepng whithout taking care of the existing parameters. It did not matter and there was no error in PHP4. But in PHP5, the image will not be shown anymore. So you have to remove it to have the standard:
imagepng($image);


adrenalin

Trying to resize a png 256 colors image and save it in 256 colors with a correct color palette ? (if you'll save a 256 color image in truecolor palette the result image will have a big size).
I spent some hours trying various function to get a good quality 256 color png image, but because of color palette the result image quality was awful.
But thank to the comment of zmorris at zsculpt dot com from imagetruecolortopalette function page, i figured out how to get a properly image!
<?php
function resize_png($src,$dst,$dstw,$dsth) {
list($width, $height, $type, $attr) = getimagesize($src);
$im = imagecreatefrompng($src);
$tim = imagecreatetruecolor($dstw,$dsth);
imagecopyresampled($tim,$im,0,0,0,0,$dstw,$dsth,$width,$height);
$tim = ImageTrueColorToPalette2($tim,false,255);
imagepng($tim,$dst);
}
//zmorris at zsculpt dot com function, a bit completed
function ImageTrueColorToPalette2($image, $dither, $ncolors) {
$width = imagesx( $image );
$height = imagesy( $image );
$colors_handle = ImageCreateTrueColor( $width, $height );
ImageCopyMerge( $colors_handle, $image, 0, 0, 0, 0, $width, $height, 100 );
ImageTrueColorToPalette( $image, $dither, $ncolors );
ImageColorMatch( $colors_handle, $image );
ImageDestroy($colors_handle);
return $image;
}
?>
Good luck,
Namolovan Nicolae, Moldova


vladson

To send 'Content-Length' header (like in static pictures case) i use "Output handler" like this...
<?php
// Output handler
function output_handler($img) {
header('Content-type: image/png');
header('Content-Length: ' . strlen($img));
return $img;
}
// Image output
ob_start("output_handler");
imagepng($im);
ob_end_flush();
?>


14-apr-2005 08:11

To cbrasho at yahoo dot com
I have read your note and the note that was made in reply. My SIMPLE solution is to add a key to each image record in the database so that the ID+Key pair will be used very similarly to a Username+Password pair =) all you have to do is create a unique random number or something to put in the "Key" field of the database when the entry is made.
i've modified the code and pasted it here...
<?php
//get file ID+Key
$id = $_REQUEST['id'];
$key = $_REQUEST['key']; // i added this!
//get data from blob
$sql = "SELECT Data, Type, Extension FROM Images WHERE ID=$id AND Key=$key"; // this is the clever bit =D
$records=mysql_query($sql);
$record=mysql_fetch_row($records);
$data=$record['Data'];
$type=$record['Type'];
$ext=$record['Extension'];
//output data
header("Content-Type: $type");
header("Content-Disposition: inline; filename=img$id.$ext");
echo $data;
?>
all i did was add 1 line and change another =D
now if "image.php?id=1&key=A29F1X" gives a result, "image.php?id=2&key=A29F1X" will output an image of zero bytes in size because the keys in the database are unique =)


10-nov-2004 10:08

To cbrasho at yahoo dot com
I don't get the problem. If you want to have images stored as blobs, you can do a show.php like this:
<?php
//get file ID
$id = $_REQUEST['id'];
//get data from blob
$sql = "SELECT Data, Type, Extension FROM Images WHERE ID=$id";
$records=mysql_query($sql);
$record=mysql_fetch_row($records);
$data=$record['Data'];
$type=$record['Type'];
$ext=$record['Extension'];
//output data
header("Content-Type: $type");
header("Content-Disposition: inline; filename=img$id.$ext");
echo $data;
?>
I've done this many times (conceptually), and it works like a charm, although I would recommend a little more security on that indata. If you have a unified place images go in (like an upload form), you can even drop the type and extension part by saving every image as the same filetype.


mail

to all the ones, who like having their users fill their profil with an image without destroying a fixed design the following should be a great way to handle this problem.
this file opens a picture from $imagepath and returns it as a valid picture to embed in: <img src="file.php?image=123.jpg[?maxX=200&maxY=150]"> (in [] = optional)
but this file does more than this. it also adds black borders to files that are smaller than the max. size, so adding borders to the left and right where a image is too high :-)
if there is a need for a copyright note this script will also help you. you can put in a various text to $copyright. the text length should be in relationship to $maxX and $maxY.
Well there are other features of the script, just try'em out and have fun with it :-)
bye
<?php
# standard height & weight if not given
if(!isset($maxX)) $maxX = 100;
if(!isset($maxY)) $maxY = 75;
# colour- & textvalues
$picBG = "0,0,0"; # RGB-value !
$picFG = "104,104,104"; # RGB-value !
$copyright = "stefan bechtold";
$font = 1;
# minimal & maximum zoom
$minZoom = 1; # per cent related on orginal (!=0)
$maxZoom = 200; # per cent related on orginal (!=0)
# paths
$imgpath = "userimages/"; # ending with "/" !
$nopicurl = "../images/nopic.jpg"; # starting in $imagepath!!!
$nofileurl = "../images/nofile.jpg"; # starting in $imagepath!!!
if(!isset($image) || empty($image))
$imageurl = $imgpath . $nopicurl;
elseif(! file_exists($imgpath . trim($image)))
$imageurl = $imgpath . $nofileurl;
else
$imageurl = $imgpath . trim($image);
# reading image
$image = getImageSize($imageurl, $info); # $info, only to handle problems with earlier php versions...
switch($image[2]) {
  case 1:
  # GIF image
$timg = imageCreateFromGIF($imageurl);
break;
case 2:
  # JPEG image
$timg = imageCreateFromJPEG($imageurl);
break;
case 3:
  # PNG image
$timg = imageCreateFromPNG($imageurl);
break;
}
# reading image sizes
$imgX = $image[0];
$imgY = $image[1];
# calculation zoom factor
$_X = $imgX/$maxX * 100;
$_Y = $imgY/$maxY * 100;
# selecting correct zoom factor, so that the image always keeps in the given format
# no matter if it is more higher than wider or the other way around
if((100-$_X) < (100-$_Y)) $_K = $_X;
else $_K = $_Y;
# zoom check to the original
if($_K > 10000/$minZoom) $_K = 10000/$minZoom;
if($_K < 10000/$maxZoom) $_K = 10000/$maxZoom;
# calculate new image sizes
$newX = $imgX/$_K * 100;
$newY = $imgY/$_K * 100;
# set start positoin of the image
# always centered
$posX = ($maxX-$newX) / 2;
$posY = ($maxY-$newY) / 2;
# creating new image with given sizes
$imgh = imageCreateTrueColor($maxX, $maxY);
# setting colours
$cols = explode(",", $picBG);
$bgcol = imageColorallocate($imgh, trim($cols[0]), trim($cols[1]), trim($cols[2]));
$cols = explode(",", $picFG);
$fgcol = imageColorallocate($imgh, trim($cols[0]), trim($cols[1]), trim($cols[2]));
# fill background
imageFill($imgh, 0, 0, $bgcol);
# create small copy of the image
imageCopyResampled($imgh, $timg, $posX, $posY, 0, 0, $newX, $newY, $image[0], $image[1]);
# writing copyright note
imageStringUp($imgh, $font, $maxX-9, $maxY-3, $copyright, $fgcol);
# output
switch($image[2]) {
  case 1:
# GIF image
header("Content-type: image/gif");
imageGIF($imgh);
case 2:
# JPEG image
header("Content-type: image/jpeg");
imageJPEG($imgh);
case 3:
# PNG image
header("Content-type: image/png");
imagePNG($imgh);
}
# cleaning cache
imageDestroy($timg);
imageDestroy($imgh);
?>


neburk0rk

This is my way to store PNG-images in a MySQL database... You cannot directly store the PNG-image in a variable, and then parse it in the database, cos if you try to define it to a variable, it'll still just output it...
In my method i use three functions
to "capture" the output and store it in a variable; ob_start (to start the output buffering),    ob_get_contents  (to capture the output),    and  ob_end_clean (to erase the cache, and end the output buffering)
<?php
$imagefile = "changethistogourimage.gif";
$image = imagecreatefromgif($imagefile);
ob_start();
imagepng($image);
$imagevariable = ob_get_contents();
ob_end_clean();
/*
HERE YOU CAN MESS WITH THE $imagevariable AS YOU LIKE
*/
?>


dws

Presumably it returns true on success and false on failure, although the documentation doesn't actually say so.

awalton

PNG images (as any image) can be stored in a MySQL blob field, but if you want to do this, you'll want to serialize the image stream into a better form. I would recommend base64_encode() and base64_decode(). (Just fopen() the file, fread() the contents in, base64_encode() the string, and fire off your SQL query (use addslashes()/stripslashes() to be more secure)).
This has been posted an innumerable amount of times throughout the site, but it's still terrible that a lot of users simply don't understand this and use it to its full potential.
I would also recommend that if you are doing images this way, to keep an image cache folder somewhere that PHP can access (possibly even somewhere off your webroot?). That way if your website is swamped with traffic it won't kill the SQL server.


johnbeech

PNG files are already compressed. They use a lossless compression algorithm. If you are using HighColour images, the compression only does so much. For low colour images (16 or 256) the compression is much better.
It is pointless trying to compress the images further before sending to a browser.


pm

in reply to: cbrasho at yahoo dot com
if you use Apache as a webserver, you could do the following:
You could set up a 'img' directory in your webspace.
In that directory there will be two files: a .htaccess file and a img.php file
the .htaccess file contains the following code:
ErrorDocument 404 /img/img.php
the img.php file looks something like this:
<?php
 $file = $_SERVER['REDIRECT_URL'];
 $result = mysql_query('select img_blob from images where filename=\\'' . $file . '\\');
 list($blob) = mysql_fetch_row($result);
 header('HTTP/1.0 200 Ok');
 header("Content-type: image/jpeg");
 
 print $blob; # or whatever works, I don't use this
?>
if you use a url for your image like http://test.com/img/image1.jpeg, which doesn't exist, normally you would get a 404-page. in this case, the 404 is being handled by img.php, which brings up the required image...


bart

If you want to resize a png-24 image and preserve the alpha channel you need to set imagealphablending($im_dest, false) on the destination image just after creating it with imagecreatetruecolor() and do a imagesavealpha($im_dest, true) on it before saving it:
<?php
$im = ImageCreateFromPNG('redfade.png');
$im_dest = imagecreatetruecolor (500, 300);
imagealphablending($im_dest, false);
imagecopyresampled($im_dest, $im, 0, 0, 0, 0, 300, 300, 500, 300);
imagesavealpha($im_dest, true);
imagepng($im_re, 'small_redfade.png');
?>


bgd1977

I have experienced segfaults and bus errors with the following configuration: FreeBSD4.4, Apache 1.3.26, PHP 4.2.2, GD-1.8.4, PDFlib 4.0.1. The apache process crashed when calling the imagepng function, but it didn't crash when calling the imagejpg function, or imagecreatefrompng...
Some wasted hours (lots) later, in which I have tried to recompile gd, libpng, php, libjpeg, what-not, I have found the following advices:
http://bugs.php.net/bug.php?id=16841
So the problem was not with the png library, but rather with the PDFlib. Even though all the threads led to a png-problem... so I have simply upgraded to PDFlib 4.0.3 (w/o any special configure arguments; --with-libpng didn't work anyways), recompiled PHP, and now everything works (imagepng, pdf creation, etc.).
Hope this helps,
bogdan


cbrasho

Having your pictures stored in a database sounds great but brings you a lot of trouble.
Storing images in a DB you will have a script show.php that will appear in <img> tags: <img src='show.php?img_id=$some_id'>
But if you want to have REGISTER GLOBALS = OFF, you are in trouble and there is no way (at leas as far as i know) to solve the problem but to put te img from the DB in a file and put the coresponding file name in the <img> tag. But this brings another problem: simultaneous accesses to the page. So you will have to find a way to give unique names to the picture files for each simultaneous access to the page. The solution might be using sessions. This is how you end up having a very compleh PHP script for a very simple problem. So, the basic ideea is " do not store your pictures in a blob unless you know exactly what you are doing".


teemu

Filter constants are listed on libpng site http://www.libpng.org/pub/png/libpng-1.2.5-manual.html#section-5.9

ruelle

Better than a chmod 777 to any '/dir/pic.png' you should :
- test if dir is writable (is_writable func.)
- use chmod 700 (more secure because let only the webserver ID have access)
In any case you should program a (crontab) script to change the owner ID of any images created.


alex

barts code below does not work at least with gd 2
Only returns a blank image with alpha not the source resized
$im = ImageCreateFromPNG($sourcefile);
$im_dest = imagecreatetruecolor ($forcedwidth, $forcedheight);
imagealphablending($im_dest, false);
imagecopyresampled($im_dest, $im, 0, 0, 0, 0, $wm_width, $wm_height, $forcedwidth, $forcedheight);
imagesavealpha($im_dest, true);
imagepng($im_dest, $destfile);
imagedestroy($img_dest);
ps you also forgot image destroy and you had a random var in imagepng undefined in your post


23-jan-2003 06:20

"Tip: As with anything that outputs its result directly to the browser, you can use the output-control functions (http://www.php.net/manual/en/ref.outcontrol.php) to capture the output of this function, and save it in a string (for example)."
ob_start();
imagepng($image);
$image_data = ob_get_contents();
ob_end_clean();
And now you can save $image_data to a database, for example, instead of first writing it to file and then reading the data from it. Just don't forget to use mysql_escape_string...


Change Language


Follow Navioo On Twitter
gd_info
getimagesize
image_type_to_extension
image_type_to_mime_type
image2wbmp
imagealphablending
imageantialias
imagearc
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
imagecreatefromgd2
imagecreatefromgd2part
imagecreatefromgd
imagecreatefromgif
imagecreatefromjpeg
imagecreatefrompng
imagecreatefromstring
imagecreatefromwbmp
imagecreatefromxbm
imagecreatefromxpm
imagecreatetruecolor
imagedashedline
imagedestroy
imageellipse
imagefill
imagefilledarc
imagefilledellipse
imagefilledpolygon
imagefilledrectangle
imagefilltoborder
imagefilter
imagefontheight
imagefontwidth
imageftbbox
imagefttext
imagegammacorrect
imagegd2
imagegd
imagegif
imagegrabscreen
imagegrabwindow
imageinterlace
imageistruecolor
imagejpeg
imagelayereffect
imageline
imageloadfont
imagepalettecopy
imagepng
imagepolygon
imagepsbbox
imagepsencodefont
imagepsextendfont
imagepsfreefont
imagepsloadfont
imagepsslantfont
imagepstext
imagerectangle
imagerotate
imagesavealpha
imagesetbrush
imagesetpixel
imagesetstyle
imagesetthickness
imagesettile
imagestring
imagestringup
imagesx
imagesy
imagetruecolortopalette
imagettfbbox
imagettftext
imagetypes
imagewbmp
imagexbm
iptcembed
iptcparse
jpeg2wbmp
png2wbmp
eXTReMe Tracker