PHP 图片处理


Posted in PHP onSeptember 16, 2020

图片处理函数功能:缩放、剪切、相框、水印、锐化、旋转、翻转、透明度、反色处理并保存历史记录的思路:当有图片有改动时自动生成一张新图片

1、转Base64编码

/**
 * 获取图片的Base64编码(不支持url)
 * @param $img_file 传入本地图片地址
 * @return string
 */
function imgToBase64($img_file) {
 $img_base64 = '';
 if (file_exists($img_file)) {
  $app_img_file = $img_file; // 图片路径
  $img_info = getimagesize($app_img_file); // 取得图片的大小,类型等
  //echo '<pre>' . print_r($img_info, true) . '</pre><br>';
  list($width, $height, $type, $attr) = getimagesize($app_img_file);
  $fp = fopen($app_img_file, "r"); // 图片是否可读权限
  if ($fp) {
   $filesize = filesize($app_img_file);
   $content = fread($fp, $filesize);
   $file_content = chunk_split(base64_encode($content)); // base64编码
   switch ($type) {   //判读图片类型
    case 1: $img_type = "gif";
     break;
    case 2: $img_type = "jpg";
     break;
    case 3: $img_type = "png";
     break;
   }
   $img_base64 = 'data:image/png;base64,' . $file_content;//合成图片的base64编码
  }
  fclose($fp);
 }else{
  return $img_file;
 }
 return $img_base64; //返回图片的base64
}

2、图片旋转

/**
 * 图片旋转
 * @param $src 图片地址
 * @param $direction 1顺时针90 2 逆时针90
 * @return string
 */
function imgturn($src, $direction = 1){
 $ext = pathinfo($src)['extension'];
 switch ($ext) {
  case 'gif':
   $img = imagecreatefromgif($src);
   break;
  case 'jpg':
  case 'jpeg':
   $img = imagecreatefromjpeg($src);
   break;
  case 'png':
   $img = imagecreatefrompng($src);
   break;
  default:
   die('图片格式错误!');
   break;
 }
 $width = imagesx($img);
 $height = imagesy($img);
 $img2 = imagecreatetruecolor($height, $width);
 //顺时针旋转90度
 if($direction == 1){
  for ($x = 0; $x < $width; $x++) {
   for($y=0; $y<$height; $y++) {
    imagecopy($img2, $img, $height - 1 - $y, $x, $x, $y, 1, 1);
   }
  }
 }else if($direction == 2) {
  //逆时针旋转90度
  for ($x = 0; $x < $height; $x++) {
   for($y = 0; $y < $width; $y++) {
    imagecopy($img2, $img, $x, $y, $width - 1 - $y, $x, 1, 1);
   }
  }
 }
 switch ($ext) {
  case 'jpg':
  case "jpeg":
   imagejpeg($img2, $src, 100);
   break;
  case "gif":
   imagegif($img2, $src, 100);
   break;
  case "png":
   imagepng($img2, $src, 100);
   break;
  default:
   die('图片格式错误!');
   break;
 }
 imagedestroy($img);
 imagedestroy($img2);
}

3、图片压缩

/**
* 图片压缩处理
* @param string $sFile 源图片路径
* @param int $iWidth 自定义图片宽度
* @param int $iHeight 自定义图片高度
* @return string 压缩后的图片路径
*/
function getThumb($sFile, $iWidth, $iHeight){
 //图片公共路径
 $public_path = '';
 //判断该图片是否存在
 if(!file_exists($public_path . $sFile)) return $sFile;
 list($width, $height, $type, $attr) = getimagesize($sFile);
 if($width < $height){
  imgturn($sFile, 2);
 }
 //判断图片格式(图片文件后缀)
 $extend = explode("." , $sFile);
 $attach_fileext = strtolower($extend[count($extend) - 1]);
 if (!in_array($attach_fileext, array('jpg','png','jpeg'))){
  return '';
 }
 //压缩图片文件名称
 $sFileNameS = str_replace("." . $attach_fileext, "_" . $iWidth . '_' . $iHeight . '.' . $attach_fileext, $sFile);
 //判断是否已压缩图片,若是则返回压缩图片路径
 if(file_exists($public_path . $sFileNameS)){
  return $sFileNameS;
 }
 //生成压缩图片,并存储到原图同路径下
 resizeImage($public_path . $sFile, $public_path . $sFileNameS, $iWidth, $iHeight);
 if(!file_exists($public_path . $sFileNameS)){
  return $sFile;
 }
 return $sFileNameS;
}

4、生成目标图片

/**
 * 生成图片
 * @param string $im 源图片路径
 * @param string $dest 目标图片路径
 * @param int $maxwidth 生成图片宽
 * @param int $maxheight 生成图片高
 */
function resizeImage($im, $dest, $maxwidth, $maxheight) {
 $img = getimagesize($im);
 switch ($img[2]) {
  case 1:
   $im = @imagecreatefromgif($im);
  break;
  case 2:
   $im = @imagecreatefromjpeg($im);
  break;
  case 3:
   $im = @imagecreatefrompng($im);
  break;
 }
 $pic_width = imagesx($im);
 $pic_height = imagesy($im);
 $resizewidth_tag = false;
 $resizeheight_tag = false;
 if (($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight)) {
  if ($maxwidth && $pic_width > $maxwidth) {
   $widthratio = $maxwidth / $pic_width;
   $resizewidth_tag = true;
  }
  if ($maxheight && $pic_height > $maxheight) {
   $heightratio = $maxheight / $pic_height;
   $resizeheight_tag = true;
  }
  if ($resizewidth_tag && $resizeheight_tag) {
   if ($widthratio < $heightratio){
    $ratio = $widthratio;
   }else{
    $ratio = $heightratio;
   }
  }
  if ($resizewidth_tag && !$resizeheight_tag){
   $ratio = $widthratio;
  }
  if ($resizeheight_tag && !$resizewidth_tag){
   $ratio = $heightratio;
  }
  $newwidth = $pic_width * $ratio;
  $newheight = $pic_height * $ratio;
  if (function_exists("imagecopyresampled")) {
   $newim = imagecreatetruecolor($newwidth, $newheight);
   imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
  } else {
   $newim = imagecreate($newwidth, $newheight);
   imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
  }
  imagejpeg($newim, $dest);
  imagedestroy($newim);
 } else {
  imagejpeg($im, $dest);
 }
}

以上就是PHP对图片的处理的详细内容,更多关于PHP 图片处理的资料请关注三水点靠木其它相关文章!

PHP 相关文章推荐
php记录日志的实现代码
Aug 08 PHP
PHP反转字符串函数strrev()函数的用法
Feb 04 PHP
php遍历目录与文件夹的多种方法详解
Nov 14 PHP
PHP登陆后跳转到登陆前页面实现思路及代码
Jan 17 PHP
PHP+iFrame实现页面无需刷新的异步文件上传
Sep 16 PHP
php使用COPY函数更新配置文件的方法
Jun 18 PHP
PHP的Yii框架中移除组件所绑定的行为的方法
Mar 18 PHP
Thinkphp3.2实用篇之计算型验证码示例
Feb 09 PHP
tp5(thinkPHP5框架)captcha验证码配置及验证操作示例
May 28 PHP
laravel框架 api自定义全局异常处理方法
Oct 11 PHP
在Laravel中实现使用AJAX动态刷新部分页面
Oct 15 PHP
如何在PHP中使用数组
Jun 09 PHP
laravel入门知识点整理
Sep 15 #PHP
分享几种好用的PHP自定义加密函数(可逆/不可逆)
Sep 15 #PHP
PhpStorm+xdebug+postman调试技巧分享
Sep 15 #PHP
laravel中Redis队列监听中断的分析
Sep 14 #PHP
PHP实现限制域名访问的实现代码(本地验证)
Sep 13 #PHP
PHP $O00OO0=urldecode &amp; eval 解密,记一次商业源码的去后门
Sep 13 #PHP
PHP重载基础知识回顾
Sep 10 #PHP
You might like
德生9700DX电路分析
2021/03/02 无线电
十天学会php(1)
2006/10/09 PHP
ExtJS 学习专题(一) 如何应用ExtJS(附实例)
2010/03/11 Javascript
基于jquery的内容循环滚动小模块(仿新浪微博未登录首页滚动微博显示)
2011/03/28 Javascript
Javascript查询DBpedia小应用实例学习
2013/03/07 Javascript
jquery ajax jsonp跨域调用实例代码
2013/12/11 Javascript
JS控制图片翻转示例代码(兼容firefox,ie,chrome)
2013/12/19 Javascript
使用jQuery实现的掷色子游戏动画效果
2014/03/14 Javascript
JS中常用的正则表达式
2016/09/29 Javascript
js实现类bootstrap模态框动画
2017/02/07 Javascript
js学习心得_一个简单的动画库封装tween.js
2017/07/14 Javascript
js读取本地文件的实例
2017/12/22 Javascript
vue中element 上传功能的实现思路
2018/07/06 Javascript
详解如何构建一个Angular6的第三方npm包
2018/09/07 Javascript
原生js实现Flappy Bird小游戏
2018/12/24 Javascript
javascript合并两个数组最简单的实现方法
2019/09/14 Javascript
Python使用Pycrypto库进行RSA加密的方法详解
2016/06/06 Python
python requests 测试代理ip是否生效
2018/07/25 Python
Linux下python3.7.0安装教程
2018/07/30 Python
Python 3.6 -win64环境安装PIL模块的教程
2019/06/20 Python
python高阶函数map()和reduce()实例解析
2020/03/16 Python
如何实现在jupyter notebook中播放视频(不停地展示图片)
2020/04/23 Python
Python导入数值型Excel数据并生成矩阵操作
2020/06/09 Python
Python 可视化神器Plotly详解
2020/12/26 Python
CSS3中Color的一些特性介绍
2012/05/27 HTML / CSS
基于HTML5 Canvas的3D动态Chart图表的示例
2017/11/02 HTML / CSS
美国零售商店:Blue&Cream
2017/04/07 全球购物
美国知名珠宝首饰品牌:Gemvara
2017/10/06 全球购物
最畅销的视频游戏享受高达90%的折扣:CDKeys
2020/02/10 全球购物
PHP如何对用户密码进行加密
2014/07/31 面试题
2014年党员整改措施范文
2014/09/21 职场文书
学习三严三实对照检查材料思想汇报
2014/09/22 职场文书
2014年教师思想工作总结
2014/12/03 职场文书
投资意向协议书
2015/01/29 职场文书
2015年仓库管理员工作总结
2015/04/21 职场文书
记者节感言
2015/08/03 职场文书