码迷,mamicode.com
首页 > Web开发 > 详细

[php] 处理图像

时间:2014-12-11 10:01:02      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:des   blog   io   ar   color   使用   sp   for   on   

<?php
/* 处理图像 */
/* 
 {php5} 动态图像的处理更容易.
 在 php.ini中就包含了GD扩展包, 去掉 其中的注释即可.
 	 extension=php_gd2.dll
 其中 包含了 支持真彩图像处理的一些有用的JPG功能.
 
  一般生成的图形, 通过PHP的文档格式存放;
 但可以通过HTML的图片插入方式SRC 来直接获取动态图形. 比如: 验证码 / 水印 / 缩略图 ...
    
 *  */

/* {创建图像}的一般流程: 
		1. 设定标头.(告诉浏览器你要身材的MIME的类型)
		2. 创建 {图像区域}, 后面的操作要基于此图像区域.
			* imagecreatetruecolor 
				一般要加上@符号,避免出错.
				* 返回值是一个资源句柄
				没有填充的时候,背景是黑色的(默认).
				
			* imagecreate
			 
			 
		3. 在{图像区域}上,绘制填充背景.
			首先, 要有个 {颜色填充器}
				imagecolorallocate -- 为一幅图像分配颜色
				imagecolorallocatealpha -- 为一幅图像分配颜色 + alpha

			然后, 填充整个图像背景
				imagefill -- 区域图像填充
				imagefilledarc -- 画一椭圆弧且填充
				imagefilledellipse -- 画一椭圆并填充
				imagefilledpolygon -- 画一多边形并填充
				imagefilledrectangle -- 画一矩形并填充

		
		4. 在{背景}上绘制{图形轮廓输入文本}
		5. 输出最终 图像.
		6. 清除所有资源.
		7. 其他页面调用图像.
		

一般 生成的图像 可以是 png, jpg, gif, bmp,
jpeg, wbmp

header("Content-Type:text/html");// 一般网页类型是text/html, 默认不用写

  
  
  
 * */






?>


<img src="demo4.php" alt="PHP创建的图像" />

  

<?php 
header("Content-Type:image/png");
// 设定  标头 指定MIME类型
$im=imagecreatetruecolor(200, 200);
// 创建 一个 空白的  图像区域

/* 设置一个颜色, 用它填充图像区域的背景*/
$blue=imagecolorallocate($im, 0, 102, 255);
imagefill($im, 0, 0, $blue);

/* 在图像轮廓上绘制文本 */
// 白色文字
// 	白色
$white=imagecolorallocate($im, 255, 255, 255);
// 	画两条对角线
imageline($im, 0, 0, 200, 200, $white);
imageline($im, 200, 0, 0, 200, $white);
imagestring($im, 5, 80, 20, "Mr.Lee", $white);

/* 输出  最终的图像 */
imagepng($im);

/* 清除所有的占用 资源 */
imagedestroy($im);

// 其他页面就可以调用此页面创建的图像了
?>

  

<?php
header("Content-Type:image/png");
/*产生随机数字符串*/
$nmsg="";
for ($i=0; $i < 4; $i++) { // 4位的随机数
	$nmsg.=dechex(rand(0,15));
}
// echo $nmsg;
// 准备图像
$im=imagecreatetruecolor(75,25);// 一般的,一个数字高25
$blue=imagecolorallocate($im, 0, 102, 255);
$white=imagecolorallocate($im, 255, 255, 255);

// 填充图像内容
imagefill($im, 0, 0, $white);
imagestring($im, 5, 20, 4, $nmsg, $blue);


/*输出图像,并销毁被使用的资源*/
imagepng($im);
imagedestroy($im);


?>

  

<?php
header("Content-Type:image/jpg");
/*给图像添加一个水印*/


$im=imagecreatefromjpeg("2.jpg");

$white=imagecolorallocate($im, 255, 255, 255);


imagestring($im, 15, 10, 10, "This is a picture!", $white);

// imagettftext($im, 5, 0, 10, 100, $white, "simsun.ttf", "阳光都是中文");


imagejpeg($im);
imagedestroy($im);

?>

  

<?php
ob_clean();//如果输出的图像不显示, 那就在这里清除输出缓冲区
header("Content-type:image/png");

$im=imagecreatetruecolor(200, 200);

$white=imagecolorallocate($im, 255, 255, 255);
$blue=imagecolorallocate($im, 0, 102, 255);

imagefill($im, 0, 0, $blue);


// $text=iconv("gbk", "utf-8", "鎷夋柉鐨勭鏋楄垂鏂?");
$font="C:\\Windows\\Fonts\\consola.ttf";

imagettftext($im, 20, 45, 20, 100, $white, $font, "hello");/*这里的第四第五个参数,用来显示位置,注意不要用0,否则找不到输出的文本内容*/


imagestring($im, 10, 0, 5, "This is a boy.", $white);

imagepng($im);
imagedestroy($im);

?>

  

<?php
ob_clean();//如果输出的图像不显示, 那就在这里清除输出缓冲区
header("Content-type:image/png");

$im=imagecreatetruecolor(200, 200);

$white=imagecolorallocate($im, 255, 255, 255);
$blue=imagecolorallocate($im, 0, 102, 255);

imagefill($im, 0, 0, $blue);


// $text=iconv("gbk", "utf-8", "鎷夋柉鐨勭鏋楄垂鏂?");
$font="C:\\Windows\\Fonts\\simsun.ttc";

imagettftext($im, 20, 45, 20, 100, $white, $font, "hello");
/* imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text) *//*这里的第四$x第五$y个参数,用来显示位置,注意不要用0,否则找不到输出的文本内容*/

imagestring($im, 10, 0, 5, "This is a boy.", $white);

$text="这是中文的";
// $text=iconv("utf-8", "utf-8", "这是中午的");
imagettftext($im, 25, 0, 20, 150, $white, $font, $text);
/* 这个中文的输出是正确的.{现在文件编码是utf8,html嵌入文件输出的文字编码是utf8} */


imagepng($im);
imagedestroy($im);

?>

  

<?php 
/* 说说怎么用eclipse的问题 */
/* 
 * 首先是界面: 有用的就是编辑器部分和项目管理器(其他的都关闭吧)
 * 
 * 其次: 新建项目的问题.
 * 		首先了最好把工作区放到你的文件夹根目录;然后,每个新建的项目只要设置好项目名,让epp自动生成文件夹就行了.
 * 
 *  
 *  */

// 下面将会练习微缩图

// echo __DIR__;

list($w,$h)=getimagesize("2.jpg");
$nw=$w*0.7;
$nh=$h*0.7;
$im_src=imagecreatefromjpeg("2.jpg");
$im=imagecreatetruecolor($nw, $nh);
imagecopyresampled($im, $im_src, 0, 0, 0, 0, $nw, $nh, $w, $h);

ob_clean();
header("Content-type:image/png");
imagepng($im);
// imagepng($im_src);
imagedestroy($im);
imagedestroy($im_src);

?>

  

 

[php] 处理图像

标签:des   blog   io   ar   color   使用   sp   for   on   

原文地址:http://www.cnblogs.com/lizunicon/p/4156751.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!