php imagecreatetruecolor 创建高清和透明图片代码小结


Posted in PHP onMay 15, 2010

(PHP 4 >= 4.0.6, PHP 5)
imagecreatetruecolor — 新建一个真彩色图像

说明
resource imagecreatetruecolor ( int $x_size , int $y_size )
imagecreatetruecolor() 返回一个图像标识符,代表了一幅大小为 x_size 和 y_size 的黑色图像。

是否定义了本函数取决于 PHP 和 GD 的版本。从 PHP 4.0.6 到 4.1.x 只要加载了 GD 模块本函数一直存在,但是在没有安装 GD2 的时候调用,PHP 将发出致命错误并退出。在 PHP 4.2.x 中此行为改为发出警告而不是错误。其它版本只在安装了正确的 GD 版本时定义了本函数。

新建一个新的 GD 图像流并输出图像

<?php 
header("Content-type: image/png"); 
$im = @imagecreatetruecolor(50, 100) 
or die("Cannot Initialize new GD image stream"); 
$text_color = imagecolorallocate($im, 233, 14, 91); 
imagestring($im, 1, 5, 5, "A Simple Text String", $text_color); 
imagepng($im); 
imagedestroy($im); 
?>

Note: 本函数需要 GD 2.0.1 或更高版本(推荐 2.0.28 及更高版本)。

php imagecolorallocatealpha 创建透明图片实例
imagecolorallocatealpha(resource $image , int $red , int $green , int $blue, int $alpha )
imagecolorallocatealpha()的行为相同imagecolorallocate()同阿尔法增加透明度参数。

$image
图像资源,通过创造的图像功能,如,一返回imagecreatetruecolor()。

$red
红色分量的价值。

$green
价值的绿色成分。

$blue
蓝色成分的价值。

$alpha
一个介于0和127的价值。 0表示完全不透明,而127表示完全透明。
来看个imagecolorallocatealpha实例教程

<?php 
$size = 300; 
$image=imagecreatetruecolor($size, $size); // something to get a white background with black border 
$back = imagecolorallocate($image, 255, 255, 255); 
$border = imagecolorallocate($image, 0, 0, 0); 
imagefilledrectangle($image, 0, 0, $size - 1, $size - 1, $back); 
imagerectangle($image, 0, 0, $size - 1, $size - 1, $border); 
$yellow_x = 100; 
$yellow_y = 75; 
$red_x = 120; 
$red_y = 165; 
$blue_x = 187; 
$blue_y = 125; 
$radius = 150; 
// allocate colors with alpha values 
$yellow = imagecolorallocatealpha($image, 255, 255, 0, 75); 
$red = imagecolorallocatealpha($image, 255, 0, 0, 75); 
$blue = imagecolorallocatealpha($image, 0, 0, 255, 75); 
// drawing 3 overlapped circle 
imagefilledellipse($image, $yellow_x, $yellow_y, $radius, $radius, $yellow); 
imagefilledellipse($image, $red_x, $red_y, $radius, $radius, $red); 
imagefilledellipse($image, $blue_x, $blue_y, $radius, $radius, $blue); 
// don't forget to output a correct header! 
header('Content-type: image/png'); 
// and finally, output the result 
imagepng($image); 
imagedestroy($image); 
?>

php imagecreatetruecolor创建高清图片函数
imagecreatetruecolor()返回一个图像标识符代表指定大小的黑色形象。

根据你的PHP和GD版本中函数定义与否。对于PHP 4.0.6通过4.1.x这个函数总是存在的

,如果广东模块加载,但它要求GD2的情况下被安装了PHP将发出一个致命错误并退出。

用PHP 4.2.x版这种行为是不同的人发出警告,而不是一个错误。其他版本只定义此功

能,

看看实例

<?php 
header ('Content-type: image/png'); 
$im = @imagecreatetruecolor(120, 20) 
or die('Cannot Initialize new GD image stream'); 
$text_color = imagecolorallocate($im, 233, 14, 91); 
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color); 
imagepng($im); 
imagedestroy($im); 
?>

我提出这方面合作 - 结合一些例子,然后动态生成的文本。但是,与此设置,我能得

到透明背景的工作也。

<?php 
// Set the content-type header('Content-type: image/png'); 
// Create the image 
$im = imagecreatetruecolor(175, 15); 
imagesavealpha($im, true); 
// Create some colors 
$white = imagecolorallocate($im, 255, 255, 255); 
$grey = imagecolorallocate($im, 128, 128, 128); 
$black = imagecolorallocate($im, 0, 0, 0); 
imagefilledrectangle($im, 0, 0, 150, 25, $black); 
$trans_colour = imagecolorallocatealpha($im, 0, 0, 0, 127); 
imagefill($im, 0, 0, $trans_colour); 
// The text to draw 
$text = $_GET['text']; 
// Replace path by your own font path 
$font = 'catriel regular.ttf'; 
// Add some shadow to the text 
imagettftext($im, 9, 0, 13, 16, $black, $font, $text); 
// Add the text 
imagettftext($im, 9, 0, 12, 15, $white, $font, $text); 
// Using imagepng() results in clearer text compared with imagejpeg() 
imagepng($im); 
imagedestroy($im); 
?>

ph利用imagecreatetruecolor动态生成高清图片代码
//实例用我们用imagecreatetruecolor 
header ('Content-type: image/png'); 
$im = @imagecreatetruecolor(120, 20) 
or die('Cannot Initialize new GD image stream'); 
$text_color = imagecolorallocate($im, 233, 14, 91); 
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color); 
imagepng($im); 
imagedestroy($im); //我把这个一起 - 结合较好的例子,然后动态生成的文本。但是,与此成立,我能得到透明背景以及工作。 
//实例二imagecreatetruecolor 
header('Content-type: image/png'); 
// Create the image 
$im = imagecreatetruecolor(175, 15); 
imagesavealpha($im, true); 
// Create some colors 
$white = imagecolorallocate($im, 255, 255, 255); 
$grey = imagecolorallocate($im, 128, 128, 128); 
$black = imagecolorallocate($im, 0, 0, 0); 
imagefilledrectangle($im, 0, 0, 150, 25, $black); 
$trans_colour = imagecolorallocatealpha($im, 0, 0, 0, 127); 
imagefill($im, 0, 0, $trans_colour); 
// The text to draw 
$text = $_GET['text']; 
// Replace path by your own font path 
$font = 'catriel regular.ttf'; 
// Add some shadow to the text 
imagettftext($im, 9, 0, 13, 16, $black, $font, $text); 
// Add the text 
imagettftext($im, 9, 0, 12, 15, $white, $font, $text); 
// Using imagepng() results in clearer text compared with imagejpeg() 
imagepng($im); 
imagedestroy($im); 
/* 
实例三创建透明图片 
如果你想创建一个PNG图像*透明*,其中的背景是完全透明的,所有行动发生在借鉴,除此之外,然后执行下列操作: 
*/ 
$png = imagecreatetruecolor(800, 600); 
imagesavealpha($png, true); 
$trans_colour = imagecolorallocatealpha($png, 0, 0, 0, 127); 
imagefill($png, 0, 0, $trans_colour); 
$red = imagecolorallocate($png, 255, 0, 0); 
imagefilledellips教程e($png, 400, 300, 400, 300, $red); 
header("Content-type: image/png"); 
imagepng($png);

你要做的就是创建一个真正的彩色图像,确保阿尔法保存状态是,然后填写一个颜色,也经历了阿尔法级别设置为完全透明(127)的图像。

从上面的代码产生的巴新将有一个完全透明的背景(一红色圆圈拖到Photoshop中的图像,以了解自己)
The resulting PNG from the code above will have a red circle on a fully transparent background (drag the image into Photoshop to see for yourself)

PHP 相关文章推荐
关于时间计算的结总
Dec 06 PHP
Win2003服务器安全加固设置--进一步提高服务器安全性
May 23 PHP
PHP学习 运算符与运算符优先级
Jun 15 PHP
php异常:Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE  eval()'d code error
May 19 PHP
php购物车实现代码
Oct 10 PHP
关于PHP递归算法和应用方法介绍
Apr 15 PHP
WordPress导航菜单的滚动和淡入淡出效果的实现要点
Dec 14 PHP
PHP访问数据库集群的方法小结
Mar 14 PHP
php获取给定日期相差天数的方法分析
Feb 20 PHP
laravel + vue实现的数据统计绘图(今天、7天、30天数据)
Jul 31 PHP
PHP面向对象程序设计(OOP)之方法重写(override)操作示例
Dec 21 PHP
浅谈PHP各环境下的伪静态配置
Mar 13 PHP
php gd2 上传图片/文字水印/图片水印/等比例缩略图/实现代码
May 15 #PHP
php getimagesize 上传图片的长度和宽度检测代码
May 15 #PHP
PHP 获取客户端真实IP地址多种方法小结
May 15 #PHP
PHP生成UTF8文件的方法
May 15 #PHP
PHP 魔术函数使用说明
May 14 #PHP
PHP 事务处理数据实现代码
May 13 #PHP
php 常用类汇总 推荐收藏
May 13 #PHP
You might like
很温暖很温暖的Lester Young
2021/03/03 冲泡冲煮
从康盛产品(discuz)提取出来的模板类
2011/06/28 PHP
yii,CI,yaf框架+smarty模板使用方法
2015/12/29 PHP
PHP fopen函数用法实例讲解
2019/02/15 PHP
PHP使用PDO创建MySQL数据库、表及插入多条数据操作示例
2019/05/30 PHP
PHP下用Swoole实现Actor并发模型的方法
2019/06/12 PHP
innerhtml用法 innertext用法 以及innerHTML与innertext的区别
2009/10/26 Javascript
js 调用父窗口的具体实现代码
2013/07/15 Javascript
JS限制Textarea文本域字符个数的具体实现
2013/08/02 Javascript
jquery 新建的元素事件绑定问题解决方案
2014/06/12 Javascript
jQuery实现按钮的点击 全选/反选 单选框/复选框 文本框 表单验证
2015/06/25 Javascript
ionic在开发ios系统微信时键盘挡住输入框的解决方法(键盘弹出问题)
2016/09/06 Javascript
微信小程序开发之实现选项卡(窗口顶部TabBar)页面切换
2016/11/25 Javascript
js导出Excel表格超出26位英文字符的解决方法ES6
2017/11/15 Javascript
详解使用create-react-app添加css modules、sasss和antd
2018/07/31 Javascript
详解使用jest对vue项目进行单元测试
2018/09/07 Javascript
[40:13]Ti4 冒泡赛第二天 iG vs NEWBEE 2
2014/07/15 DOTA
Numpy中转置transpose、T和swapaxes的实例讲解
2018/04/17 Python
Sanic框架请求与响应实例分析
2018/07/16 Python
Python判断两个文件是否相同与两个文本进行相同项筛选的方法
2019/03/01 Python
Python读取xlsx文件的实现方法
2019/07/04 Python
Python list和str互转的实现示例
2020/11/16 Python
Python+unittest+requests+excel实现接口自动化测试框架
2020/12/23 Python
HTML5实现视频弹幕功能
2019/08/09 HTML / CSS
比利时家具购买网站:Home24
2019/01/03 全球购物
Pandora德国官网:购买潘多拉手链、戒指、项链和耳环
2020/02/20 全球购物
社区十八大感言
2014/01/19 职场文书
文明学生标兵事迹
2014/01/21 职场文书
幼儿园中班下学期评语
2014/04/18 职场文书
疾病捐款倡议书
2014/05/13 职场文书
综治目标管理责任书
2015/05/11 职场文书
超强台风观后感
2015/06/09 职场文书
mysql批量新增和存储的方法实例
2021/04/07 MySQL
nginx配置虚拟主机的详细步骤
2021/07/21 Servers
工厂无线对讲系统解决方案
2022/02/18 无线电
20180830晚上第一届KSL半决赛 雨神vs解冻(二龙 三炮解说)
2022/04/01 星际争霸