【PHP】操作图片函数实例

本文提供了一个PHP脚本的小Demo,展示了如何实现图片添加文字、缩放、裁剪、添加水印、旋转及翻转等基本操作。

为了试试图片处理函数的效果,就写了这个小Demo,里面的函数都是单独的,可以根据自己的情况提取,和修改参数。

因为偷懒吧,没有给封装成个类。需要的将就着看下吧。


功能包括

1.图片添加文字 2.图片缩放 3.图片裁剪 4.添加图片水印 5.图片旋转 6.水平翻转 7.垂直翻转


这里不再贴样例图片了

样例地址:http://xhua521.cn/DemoFolder/ImageHandle/index.php


1.ImgHandle.php文件

<?php  

$flag = $_GET['flag'];
switch ($flag) {
  case 1:
    image("headImg.jpg","JPG11111");     //给图片加添字符串
    break;
  case 2:
    thumb("headImg.jpg",50,50);         //图片缩放
    break;
  case 3:
    cut("headImg.jpg",50,50,230,130);   //图片剪切
    break;
  case 4:
    wetermark("headImg.jpg","shuiyin.png");   //添加水印文字
    break;
  case 5:
    rotate("headImg.jpg",90);   //旋转一定角度
    break;
  case 6:
    trun_x("headImg.jpg");   //沿X轴水平翻转
    break;
  case 7:
    trun_y("headImg.jpg");   //沿Y轴水平翻转
    break;
  default:
    thumb("headImg.jpg",50,50);   //图片缩放
    break;
}

/**
* 为图片添加文字
* string $filename 图片的路径
* string $string 像图片中添加的字符串
*/
function image($filename,$string){
  list($width,$height,$type) = getimagesize($filename);
  $types = array(1=>"gif",2=>"jpeg",3=>"png");
  $createfrom = "imagecreatefrom".$types[$type];
  $image = $createfrom($filename);
  $x = ($width-imagefontwidth(5)*strlen($string))/2;
  $y = ($height-imagefontheight(5))/2;
  $textColor = imagecolorallocate($image,255,0,0);
  imageString($image,5,$x,$y,$string,$textColor);
  header("Content-type:image/$types[$type]");
  $output = "image".$types[$type];
  $output($image);
  
  imagedestroy($image);  
}
// image("headImg.jpg","JPG11111");     //给图片加添字符串

/**
* 修改图片缩放
* string $filename 图片的路径
* int $width 缩放后的宽度
* int $height 缩放后的高度
*/
function thumb($filename,$width=100,$height=100){
  list($width_orig,$height_orig,$type) = getimagesize($filename);
  $types = array(1=>"gif",2=>"jpeg",3=>"png");
  if($width && ($width_orig < $height_orig)){
    $width = ($height/$height_orig)*$width_orig;
  }else {
    $height = ($width/$width_orig)*$height_orig;
  }
  $image_p = imagecreatetruecolor($width,$height);
  $image = imagecreatefromjpeg($filename);
  
  imagecopyresampled($image_p,$image,0,0,0,0,$width,$height,$width_orig,$height_orig);
  header("Content-type:image/$types[$type]");
  imagejpeg($image_p);
  imagedestroy($image_p);
  imagedestroy($image);
}
// thumb("headImg.jpg",50,50);         //图片缩放

/**
* 裁剪图片
* string $filename 图片的路径
* int $x 起始点的x位置
* int $y 起始点的y位置
* int $width 缩放后的宽度
* int $height 缩放后的高度
*/
function cut($filename,$x,$y,$width,$height){
  list(,,$type) = getimagesize($filename);
  $types = array(1=>"gif",2=>"jpeg",3=>"png");
  $createfrom = "imagecreatefrom".$types[$type];
  $image = $createfrom($filename);
  
  $cutimg = imagecreatetruecolor($width,$height);
  imagecopyresampled($cutimg,$image,0,0,$x,$y,$width,$height,$width,$height);
  
  header("Content-type:image/$types[$type]");
  $output = "image".$types[$type];
  $output($cutimg);
  
  imagedestroy($cutimg);
  imagedestroy($image);
}
// cut("headImg.jpg",50,50,230,130);   //图片剪切

/**
* 为图片添加文字
* string $filename 背景图片的路径
* string $water 水印图片路径
*/
function wetermark($filename,$water){
  list($b_w,$b_h,$type) = getimagesize($filename);
  list($w_w,$w_h) = getimagesize($water);
  $posX = mt_rand(0,($b_w - $w_w));
  $posY = mt_rand(0,($b_h - $w_h));
  $types = array(1=>"gif",2=>"jpeg",3=>"png");
  $createfrom = "imagecreatefrom".$types[$type];
  $back = $createfrom($filename);
  $water = imagecreatefrompng($water);
  imagecopy($back,$water,$posX,$posY,0,0,$w_w,$w_h);
  header("Content-type:image/jpeg");
  $output = "image".$types[$type];
  $output($back);
  imagedestroy($image);
  imagedestroy($back);
}
// wetermark("headImg.jpg","shuiyin.png");   //添加水印文字

/**
* 为图片添加文字
* string $filename 背景图片的路径
* int $degrees 旋转角度
*/
function rotate($filename,$degrees){
  list(,,$type) = getimagesize($filename);
  $types = array(1=>"gif",2=>"jpeg",3=>"png");
  $createfrom = "imagecreatefrom".$types[$type];
  $source = $createfrom($filename);
  $rotate = imagerotate($source,$degrees,0);
  header("Content-type:image/jpeg");
  $output = "image".$types[$type];
  $output($rotate);
  imagedestroy($rotate);
}
// rotate("headImg.jpg",90);   //旋转一定角度

/**
* 将图片沿Y轴翻转
* string $filename 背景图片的路径
*/
function trun_y($filename){
  list($width,$height,$type) = getimagesize($filename);
  //获取宽高也可以使用imagesx()、imagesy()
  $types = array(1=>"gif",2=>"jpeg",3=>"png");
  $createfrom = "imagecreatefrom".$types[$type];
  $back = $createfrom($filename);
  $new = imagecreatetruecolor($width,$height);
  for ($i=0; $i < $width; $i++) { 
    imagecopy($new,$back,$width-$i-1,0,$i,0,1,$height);
  }
  header("Content-type:image/jpeg");
  $output = "image".$types[$type];
  $output($new);
  imagedestroy($new);
}
// trun_y("headImg.jpg");   //沿Y轴水平翻转

/**
* 将图片沿X轴翻转
* string $filename 背景图片的路径
*/
function trun_x($filename){
  list($width,$height,$type) = getimagesize($filename);
  //获取宽高也可以使用imagesx()、imagesy()
  $types = array(1=>"gif",2=>"jpeg",3=>"png");
  $createfrom = "imagecreatefrom".$types[$type];
  $back = $createfrom($filename);
  $new = imagecreatetruecolor($width,$height);
  for ($i=0; $i < $height; $i++) { 
    imagecopy($new,$back,0,$height-$i-1,0,$i,$width,1);
  }
  header("Content-type:image/jpeg");
  $output = "image".$types[$type];
  $output($new);
  imagedestroy($new);
}
// trun_x("headImg.jpg");   //沿X轴垂直翻转

?>


2.index.php文件

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>图片处理结果</title>
  </head>
  <style media="screen">
    .div1{
      display: inline-block;
      border: 1px solid #000;
    }
  </style>
  <body>
    <a href="../index.html">返回Demo列表</a> 
    <br/>
    <dl class="dlist">
      <dt><h2>原图:</h2></dt>
      <dd><img src="./headImg.jpg" alt=""></dd>
      <hr/>
      <div class="div1">
        <dt><h2>加添字符串:</h2></dt>
        <dd><img src="./ImgHandle.php?flag=1" alt=""></dd>
      </div>
      <div class="div1">
        <dt><h2>图片缩放:</h2></dt>
        <dd><img src="./ImgHandle.php?flag=2" alt=""></dd>
      </div>
      <div class="div1">
        <dt><h2>图片剪切:</h2></dt>
        <dd><img src="./ImgHandle.php?flag=3" alt=""></dd>
      </div>
      <div class="div1">
        <dt><h2>添加水印图文:</h2></dt>
        <dd><img src="./ImgHandle.php?flag=4" alt=""></dd>
      </div>
      <div class="div1">
        <dt><h2>旋转一定角度:</h2></dt>
        <dd><img src="./ImgHandle.php?flag=5" alt=""></dd>
      </div>
      <div class="div1">
        <dt><h2>沿X轴水平翻转:</h2></dt>
        <dd><img src="./ImgHandle.php?flag=6" alt=""></dd>
      </div>
      <div class="div1">
        <dt><h2>沿Y轴水平翻转:</h2></dt>
        <dd><img src="./ImgHandle.php?flag=7" alt=""></dd>
      </div>
    </dl>
  </body>
</html>

如果代码中有什么不对的地方可以向我提出来~!

PHP函数库,PHP函数大全,PHP函数实例,PHP函数手册,PHP5函数实例 PHP函数库,PHP函数大全,PHP函数实例,PHP函数手册,PHP5函数实例 curl获取远程文件内容 GD显示中文 PHP GIF动画生成类 PHP HTML转UBB函数 PHP XML转数组函数 PHP 缓存函数 PHP 设置COOKIE,并且加密COOKIE函数 PHP不缓存数据头 PHP伪造IP PHP全角半角转换函数 PHP农历函数 PHP分页函数 PHP判断字符串是否UTF8格式 php判断爬虫函数 PHP判断远程文件是否存在 PHP图片处理类:缩略,裁剪,圆角,倾斜 PHP多功能图片处理PHP多重判断删除文件函数 PHP实现英文标题的正确大写 PHP常用图片处理PHP常用测试函数 PHP得到当周每天日期 PHP文件下载类 PHP无限分类[左右值]算法 PHP显示日期、周几、农历初几、什么节日函数 PHP格式化数据,防止注入函数 PHP模拟登陆 PHP生成唯一标识符函数 PHP生成曲线图函数 PHP生成条形码 PHP统计字符串里单词出现次数 PHP缩略图类,可生成BMP格式 PHP自定义大小验证码函数 PHP获取.NET发出的WEBSERVICE数据 PHP获取FLV文件播放时间函数 PHP获取一年内所有周的开始和结束日期 php获取指定日期所在周的开始和结束日期 PHP读取文件前几个字节 判断文件类型函数 PHP连接ACCESS PHP采集程序中常用的函数 PHP随机产生指定长度中文字符串 SMTP类 url地址参数加密 一些常用验证函数 下拉-单选框选择 创建多级目录 删除数组中相同元素,只保留一个 判断路径是绝对目录还是相对目录 利用PHP搜索指定目录下指定的文件 加密解密 去掉指定的html标签 发送 trackback 数据包 图像处理类 图片验证码生成 字符集转换类 对要输入的字符串进行转换 对要输出的字符串进行反转换 对输入JS进行转换 寻找两个函数所有不同的元素 寻找两数组所有不同元素 得到文件类型 截取字符串中两个特定唯一字符之间的内容 截取指定长度字符串 折线图 按照比例改变图片大小(非生成缩略图) 收藏主页 数据验证类 数组转换成XML格式 日期计算 是否为电子邮件格式 柱形统计图 检查是否为一个合法的时间格式 检测URL地址有效性 检测文件是否图片 检测是否可以以网页形式显示 检测是否序列化后的字符串 模仿JAVASCRIPT的ESCAPE和UNESCAPE函数的功能 用curl函数读取远程文件 用file_getcontents提交数据 用php生成扭曲,有角度的验证图片(支持中文) 用正则加亮关键字 程序运行过程中直接输出 缩略图带版权信息函数 缩略图类 获得用户操作系统的换行符 获得用户的真实IP地址 计算字符串的长度(汉字按照两个字符计算) 设为主页 转换附件大小单位 转静态函数 遍历文件夹文件 采集网络数据 随机字符串 验证码 验证码类 验证输入的邮件地址是否合法
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值