imagepng() - gd函数(图像处理)
imagepng()
(PHP 4, PHP 5, PHP 7)
以 PNG 格式将图像输出到浏览器或文件
说明
imagepng(resource $image[,string $filename]): boolimagepng()将 GD 图像流($image)以 PNG 格式输出到标准输出(通常为浏览器),或者如果用$filename给出了文件名则将其输出到该文件。
参见imagegif(),imagewbmp(),imagejpeg()和imagetypes()。
The name "quality" for the compression parameter is quite misleading, as png compression is always lossless. The trade off is between speed and filesize, it cannot affect quality. Here's something I found at stackoverflow; I haven't checked it, but if it is correct it should definitely included in the documentation: --- from php source (gd.h): /* 2.0.12: Compression level: 0-9 or -1, where 0 is NO COMPRESSION at all, * 1 is FASTEST but produces larger files, 9 provides the best * compression (smallest files) but takes a long time to compress, and * -1 selects the default compiled into the zlib library. */ Conclusion: Based on the Zlib manual (http://www.zlib.net/manual.html) the default compression level is set to 6. --- Regarding suggestions to rescale the 0-99 quality range of jpeg into the 0-9 range of png, note that for jpeg 99 is minimum compression (maximum quality) while for png 9 is maximum compression (quality doesn't change).
"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...
Be careful when using a variable for the file name. PHP behavior with $filename differs when switching to PHP5.4 : PHP5.3 will use $filename='' the same way as $filename=NULL (e.g. no warning)
I just lost about 4 hours on a really stupid problem. My images on the local server were somehow broken and therefore did not display in the browsers. After much looking around and testing, including re-installing apache on my computer a couple of times, I traced the problem to an included file. No the problem was not a whitespace, but the UTF BOM encoding character at the begining of one of my inluded files... So beware of your included files! Make sure they are not encoded in UTF or otherwise in UTF without BOM. Hope it save someone's time.
If you're generating an image dynamically based on post data and don't want to save it to the server, sending it to be displayed can cause problems as when the person tries to save it, the browser will request it again from the server (causing any post data to be lost and probably a corrupted png). The easiest way to get around this is to force it to download using the content disposition header, for example:
When you allow multiple output formats, (jpg/png) but want to use the 1-100 quality scale (like jpg), you will have to format the number:
If you want to open a png image with alpha blending, you need to do something like this: I spent almost a day to find out why alpha blending doesn't work. I hope this is usefull to others too :)
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! Good luck, Namolovan Nicolae, Moldova
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
I was curious about the relationship between quality, processing time and resulting image size, so I created this little benchmark to test it (The image used was originally RGB_24bits_palette_R85.png, found on wikipedia). Results are at the bottom.
If you are outputting a PNG directly in response to a client request it is important to check your web server configuration. Some clients may request your images with a accept header of image/*. Default configurations of Apache and possibly other servers will by default NOT allow your script to run in response to this request. Apache is specifically discussed at http://stackoverflow.com/q/19169337 but other server have been documented to have issue too. In other words, when testing your application don't just use the web browser, consider a phone's browser and networking libraries which could send different headers.
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.
apparently something changed from php 5.5 to 5.6. I used to call ImagePng($image, ''); apparently this doesn't work anymore in 5.6 as it returns: imagepng(): Filename cannot be empty ImagePng($image, NULL); seems to work fine though.
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: (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
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.
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
My script was unable to complete: Fatal error: Allowed memory size of XX bytes exhausted (tried to allocate XX+n bytes). I found out that PHP handles images in uncompressed format: my input image was 8768x4282@32 bit => ~150 MB per single in-memory copy. As a solution, you can either check the dimensions and reject anything too big or, as I did, use ini_set('memory_limit','1024M'); on the page start (if your server has enough on board memory).
Creating a transparent image filled with tranparent color only I had some hard times putting up this one:
I ran across the following WRT sessions and image creation. In main.php:
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)