php笔记----第九章 图像处理

在PHP中可以通过GD库处理图像
1.创建一个图像应该完成如下所示的四个基本步骤:
1)创建图像
2)绘制图像
3)输出图像
header函数注意点
在该函数之前,不能输出任何内容
在我们的PHP代码 的函数里面,我们使用的/开头的路径 这个/不是指 web根目录,而是操作系统的 文件的根目录!
4)释放资源
        测试代码:
        <?php
        header('Content-type:image/jpeg');//默认情况header('Content-type:text/html');
        $img=imagecreatetruecolor(200,200);//新建一个长和高都为200像素的真彩色图像
        $color=imagecolorallocate($img,229,36,36);//分配颜色
        imagefill($img,0,0,$color);
        imagejpeg($img);
        imagedestroy($img);




2.水印
  magecopymerge() 函数用于拷贝并合并图像的一部分,成功返回 TRUE ,否则返回 FALSE 。
  测试代码:
  <?php
header("Content-type: image/jpeg");


//原始图像
$dst = "image/zht.jpg";


//得到原始图片信息
$dst_im = imagecreatefromjpeg($dst);
$dst_info = getimagesize($dst);


//水印图像
$src = "image/logo.gif";
$src_im = imagecreatefromgif($src);
$src_info = getimagesize($src);


//水印透明度
$alpha = 30;


//合并水印图片
imagecopymerge($dst_im,$src_im,$dst_info[0]-$src_info[0],$dst_info[1]-$src_info[1],0,0,$src_info[0],
$src_info[1],$alpha);


//输出合并后水印图片
imagejpeg($dst_im);
imagedestroy($dst_im);
imagedestroy($src_im);


3.缩放
imagecopyresampled — 重采样拷贝部分图像并调整大小(这个函数能实现图片的缩放和裁剪)
//缩放为原来尺寸的一半
<?php
// 这个文件
$filename = 'image/zht.jpg';
$percent = 0.5;


// 内容类型
header('Content-Type: image/jpeg');


// 获取新的尺寸
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;


// 重新取样
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);


// 输出
imagejpeg($image_p, null, 100);


?> 
4.图片的旋转和翻转
imagerotate — 用给定角度旋转图像
图片翻转:内容按特定方向对调(这个需要自己写函数)
把图片旋转180度
<?php
// File and rotation
$filename = 'image/zht.jpg';
$degrees = 180;


// Content type
header('Content-type: image/jpeg');


// Load
$source = imagecreatefromjpeg($filename);


// Rotate
$rotate = imagerotate($source, $degrees, 0);


// Output
imagejpeg($rotate);
?> 
5.设计验证码:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值