PHP GD库

来源:互联网 发布:画板软件在线 编辑:程序博客网 时间:2024/05/22 03:16
<?php
echo '---------------------- 生成缩略图-----------------------------------------<br/>';


$pic = './images.jpg';
list($bw,$bh) = getimagesize($pic);
$big = imagecreatefromjpeg($pic);// 原图
$small = imagecreatetruecolor($bw/2, $bh/2);// 小图
imagecopyresampled ( $small , $big , 0 , 0 , 0 , 0 , $bw/2 , $bh/2 , $bw , $bh );
imagepng($small,'./xiaotu.png');
imagedestroy($small);
imagedestroy($big);





exit;
echo '----------------------生成水印-----------------------------------------<br/>';


$big = imagecreatefromjpeg('./images.jpg');
$small = imagecreatefrompng('./t1.png');


// 将小画布粘贴到大画布上
     /*
     将 src_im  图像中坐标从 src_x , src_y  开始,宽度为 src_w ,高度为 src_h 的一部分拷贝到 dst_im  图像中坐标为 dst_x  和 dst_y 的位置上。两图像将根据 pct 来决定合并程度,其值范围从 0  到 100
     */
    list($w,$h)=getimagesize('./t1.png');
    list($bw,$bh)=getimagesize('./images.jpg');
    //imagecopymerge(dst_im, src_im, dst_x, dst_y, src_x, src_y, src_w, src_h, pct);


imagecopymerge ( $big , $small , $bw-$w , $bh-$h , 0 , 0 , $w , $h , 40 );
imagepng($big , './shuiyin.png');
// 销毁画布
imagedestroy($big);
imagedestroy($small);




exit;
echo '----------------------生成验证码-----------------------------------------<br/>';

/**
*  生成随机字符串
* @param int $length  产生几位的随机字符
*/
function randStr($length=6) {
$str = str_shuffle('ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjklmnpqrstuvwxyz23456789');
$str = substr($str, 0 , $length);
return $str;
}
//1  创建画布
$img = imagecreatetruecolor(60, 20);
//2  创建颜色
$red = imagecolorallocate($img, 255, 0, 0);
$gray = imagecolorallocate($img, 200, 200, 200);
//3  填充颜色
imagefill($img, 1, 1, $gray);
//4  水平的画一行字符串
//参数 :  画布 , 字体 (1-5),str 的 x 轴开始处 ,str 的 y 轴开始处 ,str, 字符串颜色
imagestring ($img , 5 , 10 , 5 , randStr(4) , $red );
//5  保存图片
// 通知浏览器 接下来输出的是 png 图片
header('Content-type:image/png');
// 不加第二个参数 浏览器会将图片的二进制信息输出在浏览器上 , 它会按照文字来理解这个图片
imagepng($img);
// 6  销毁画布
imagedestroy($img);


exit;


echo '----------------------生成图片-----------------------------------------<br/>';


//1、创建画布
$pic=imagecreatetruecolor(201, 300);
//2、创建颜料
$red=imagecolorallocate($pic, 255, 0, 0);
$blue=imagecolorallocate($pic, 0, 0, 255);

//3、画图形
imageellipse($pic, 100, 150, 200, 300, $red);


//填充背景颜色
imagefill($pic, 100, 10, $blue);
//4、输出和保存图片
imagepng($pic,'./t1.png');
//5、销毁画布资源
imagedestroy($pic);

原创粉丝点击