使用img函数库必须打开gd2扩展:
extension=php_gd2.dll
gd2函数库介绍:
实例应用:
文本图像:
<?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); //输出png图像
imagedestroy($im); //销毁资源
?>
img解决中文
<?php
header('content-type: image/jpeg');
$img=imagecreatetruecolor(100,80);#设置图片大小
$color=imagecolorallocate($img,192,192,192);#设置图片的背景颜色
imagefill($img,0,0,$color);#填充图片的背景颜色
$font_color=imagecolorallocate($img,0,0,255);#设置字体颜色
$str='汉字';
imagettftext($img, 10, 0, 20, 30, $font_color, "simfang.ttf",$str); #中文是需要使用字体
imagejpeg($img1);#输出图片
imagedestroy($img1);#销毁资源
?>
添加水印:
<?php
header ('content-type: image/jpeg');
$waterstring="http://www.xrss.cn/";//水印字符串
$watertype=1;//加水印字符串 $watertype=2;//加水印图片
$filename='http://127.0.0.1/2013-2-22/2.jpg'; //源文件图片
$simage =imagecreatefromjpeg($filename);//读取源图像文件
$image_size = getimagesize($filename);//获取源图像宽度和高度 宽度:$image_size[0]; 长度: $image_size[1];
$nimage=imagecreatetruecolor($image_size[0],$image_size[1]);//创建画布
$white=imagecolorallocate($nimage,255,255,255); //设置画布背景颜色
imagefill($nimage,0,0,$white); //颜色填充
imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]); //图像合并
$black=imagecolorallocate($nimage,0,0,0); //设置填充字体颜色
switch($watertype){
case 1: //加水印字符串
imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black);break;
case 2: //加水印图片
$simage1 =imagecreatefromgif('mzone.gif');
imagecopy($nimage,$simage1,0,0,0,0,85,15);
imagedestroy($simage1);
break;
}
imagejpeg($nimage);//输出图像
//销毁资源
imagedestroy($nimage);
imagedestroy($simage);
?>
缩略图:
<?php
//调整php分配内存大小,避免出现处理图像的内存不足
if(intval(ini_get('memory_limit')) <= 20){
ini_set('memory_limit','20M');
}
$imgfile='http://127.0.0.1/2013-2-22/2.jpg';
//获取图片信息
list($imgw, $imgh, $imgt, $attr) = getimagesize($imgfile);
//计算缩小的比例,目标最长边缩至150
$percent = $imgw>$imgh?(150/$imgw):(150/$imgh); //以最长边作为缩放参考
if($percent<1){ //计算缩略图的新尺寸
$new_width = floor($imgw*$percent);
$new_height = floor($imgh*$percent);
}else{ //如果原图尺寸小于 150x150 直接输出原图尺寸
$new_width = $imgw;
$new_height = $imgh;
}
$thumb=imagecreatetruecolor($new_width,$new_height);
//读取图片
switch($imgt){ //判断格式,图像类型,但缩略图输出的都是jpg..参考下文
case 1:
$orgimg=imagecreatefromgif($imgfile);
break;
case 2:
$orgimg=imagecreatefromjpeg($imgfile);
break;
case 3:
$orgimg=imagecreatefrompng($imgfile);
break;
}
header('Content-type: image/jpeg'); //指定header ,告诉浏览器输出的文件为jpg
//imagecopyresampled(缩略图片资源, 源图片资源, dx, dy, sx,sy, 新图像宽度, 新图像高度, 源图像宽度, 源图像高度);
imagecopyresampled($thumb, $orgimg, 0, 0, 0, 0, $new_width, $new_height, $imgw, $imgh); //缩放核心函数
imagejpeg($thumb,null,85); //输出图像
//销毁资源
imagedestroy($thumb);
imagedestroy($orgimg);
?>
图像剪切:图片剪切的原理同压缩原理相同
<?php
$filename='http://127.0.0.1/2013-2-22/2.jpg';
$wh=getimagesize($filename);//获取源图片的信息
$w=$wh[0]; //源图片宽度
$h=$wh[1];//源图片高度
$img_s = imagecreatefromjpeg($filename); //读取图片资源
$fenbianlv=$_REQUEST['tfbl'];
$img_d = ImageCreateTrueColor(60,$h); //创建新图片
header('Content-type: image/jpeg');
imagecopy($img_d,$img_s,0,0,20,0,$w,$h); //将截取后的图片合并到新资源中
imagejpeg($img_d,null,100);
imagedestroy($img_s);
imagedestroy($img_d);
?>
extension=php_gd2.dll
gd2函数库介绍:
实例应用:
文本图像:
<?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); //输出png图像
imagedestroy($im); //销毁资源
?>
img解决中文
<?php
header('content-type: image/jpeg');
$img=imagecreatetruecolor(100,80);#设置图片大小
$color=imagecolorallocate($img,192,192,192);#设置图片的背景颜色
imagefill($img,0,0,$color);#填充图片的背景颜色
$font_color=imagecolorallocate($img,0,0,255);#设置字体颜色
$str='汉字';
imagettftext($img, 10, 0, 20, 30, $font_color, "simfang.ttf",$str); #中文是需要使用字体
imagejpeg($img1);#输出图片
imagedestroy($img1);#销毁资源
?>
添加水印:
<?php
header ('content-type: image/jpeg');
$waterstring="http://www.xrss.cn/";//水印字符串
$watertype=1;//加水印字符串 $watertype=2;//加水印图片
$filename='http://127.0.0.1/2013-2-22/2.jpg'; //源文件图片
$simage =imagecreatefromjpeg($filename);//读取源图像文件
$image_size = getimagesize($filename);//获取源图像宽度和高度 宽度:$image_size[0]; 长度: $image_size[1];
$nimage=imagecreatetruecolor($image_size[0],$image_size[1]);//创建画布
$white=imagecolorallocate($nimage,255,255,255); //设置画布背景颜色
imagefill($nimage,0,0,$white); //颜色填充
imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]); //图像合并
$black=imagecolorallocate($nimage,0,0,0); //设置填充字体颜色
switch($watertype){
case 1: //加水印字符串
imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black);break;
case 2: //加水印图片
$simage1 =imagecreatefromgif('mzone.gif');
imagecopy($nimage,$simage1,0,0,0,0,85,15);
imagedestroy($simage1);
break;
}
imagejpeg($nimage);//输出图像
//销毁资源
imagedestroy($nimage);
imagedestroy($simage);
?>
缩略图:
<?php
//调整php分配内存大小,避免出现处理图像的内存不足
if(intval(ini_get('memory_limit')) <= 20){
ini_set('memory_limit','20M');
}
$imgfile='http://127.0.0.1/2013-2-22/2.jpg';
//获取图片信息
list($imgw, $imgh, $imgt, $attr) = getimagesize($imgfile);
//计算缩小的比例,目标最长边缩至150
$percent = $imgw>$imgh?(150/$imgw):(150/$imgh); //以最长边作为缩放参考
if($percent<1){ //计算缩略图的新尺寸
$new_width = floor($imgw*$percent);
$new_height = floor($imgh*$percent);
}else{ //如果原图尺寸小于 150x150 直接输出原图尺寸
$new_width = $imgw;
$new_height = $imgh;
}
$thumb=imagecreatetruecolor($new_width,$new_height);
//读取图片
switch($imgt){ //判断格式,图像类型,但缩略图输出的都是jpg..参考下文
case 1:
$orgimg=imagecreatefromgif($imgfile);
break;
case 2:
$orgimg=imagecreatefromjpeg($imgfile);
break;
case 3:
$orgimg=imagecreatefrompng($imgfile);
break;
}
header('Content-type: image/jpeg'); //指定header ,告诉浏览器输出的文件为jpg
//imagecopyresampled(缩略图片资源, 源图片资源, dx, dy, sx,sy, 新图像宽度, 新图像高度, 源图像宽度, 源图像高度);
imagecopyresampled($thumb, $orgimg, 0, 0, 0, 0, $new_width, $new_height, $imgw, $imgh); //缩放核心函数
imagejpeg($thumb,null,85); //输出图像
//销毁资源
imagedestroy($thumb);
imagedestroy($orgimg);
?>
图像剪切:图片剪切的原理同压缩原理相同
<?php
$filename='http://127.0.0.1/2013-2-22/2.jpg';
$wh=getimagesize($filename);//获取源图片的信息
$w=$wh[0]; //源图片宽度
$h=$wh[1];//源图片高度
$img_s = imagecreatefromjpeg($filename); //读取图片资源
$fenbianlv=$_REQUEST['tfbl'];
$img_d = ImageCreateTrueColor(60,$h); //创建新图片
header('Content-type: image/jpeg');
imagecopy($img_d,$img_s,0,0,20,0,$w,$h); //将截取后的图片合并到新资源中
imagejpeg($img_d,null,100);
imagedestroy($img_s);
imagedestroy($img_d);
?>