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 相关文章推荐
第十一节 重载 [11]
Oct 09 PHP
用PHP和ACCESS写聊天室(二)
Oct 09 PHP
用PHP的ob_start();控制您的浏览器cache!
Feb 14 PHP
php db类库进行数据库操作
Mar 19 PHP
PHP中去掉字符串首尾空格的方法
May 19 PHP
php不用正则验证真假身份证
Nov 06 PHP
php统计文章排行示例
Mar 04 PHP
php实现改变图片直接打开为下载的方法
Apr 14 PHP
Yii2框架BootStrap样式的深入理解
Nov 07 PHP
Yii2下点击验证码的切换实例代码
Mar 14 PHP
PHP7使用ODBC连接SQL Server2008 R2数据库示例【基于thinkPHP5.1框架】
May 06 PHP
深入浅析安装PhpStorm并激活的步骤详解
Sep 17 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
配置PHP使之能同时支持GIF和JPEG
2006/10/09 PHP
深入理解PHP原理之错误抑制与内嵌HTML分析
2011/05/02 PHP
golang与php实现计算两个经纬度之间距离的方法
2016/07/22 PHP
PHP文件与目录操作示例
2016/12/24 PHP
javascript 读取xml,写入xml 实现代码
2009/07/10 Javascript
基于javascipt-dom编程 table对象的使用
2013/04/22 Javascript
解析jquery easyui tree异步加载子节点问题
2017/03/08 Javascript
JavaScript 通过Ajax 动态加载CheckBox复选框
2017/08/31 Javascript
js使用xml数据载体实现城市省份二级联动效果
2017/11/08 Javascript
AngularJS基于http请求实现下载php生成的excel文件功能示例
2018/01/23 Javascript
jquery中attr、prop、data区别与用法分析
2019/09/25 jQuery
云服务器部署Node.js项目的方法步骤(小白系列)
2020/03/23 Javascript
如何在vue中使用百度地图添加自定义覆盖物(水波纹)
2020/11/03 Javascript
Vue解决移动端弹窗滚动穿透问题
2020/12/15 Vue.js
[05:31]DOTA2英雄梦之声_第04期_光之守卫
2014/06/23 DOTA
Python 多线程的实例详解
2017/09/07 Python
解读python logging模块的使用方法
2018/04/17 Python
python计算二维矩形IOU实例
2020/01/18 Python
python中def是做什么的
2020/06/10 Python
Python ckeditor富文本编辑器代码实例解析
2020/06/22 Python
CSS3解决移动页面上点击链接触发色块的问题
2016/06/03 HTML / CSS
详解HTML5中垂直上下居中的解决方案
2017/12/20 HTML / CSS
Ted Baker美国官网:英国时尚品牌
2018/10/29 全球购物
有原因的手表:Flex Watches
2019/03/23 全球购物
馥绿德雅美国官方网站:Rene Furterer头皮护理专家
2019/05/01 全球购物
英国排名第一的餐具品牌:Denby Pottery
2019/11/01 全球购物
日本化妆品植村秀俄罗斯官方网站:Shu Uemura俄罗斯
2020/02/01 全球购物
国贸专业个人求职信分享
2013/12/04 职场文书
党的群众路线教育实践活动公开承诺书
2014/03/28 职场文书
节约用水倡议书
2014/04/16 职场文书
中文专业毕业生自荐信
2014/05/24 职场文书
三月法制宣传月活动总结
2014/07/03 职场文书
民主生活会对照检查材料
2014/09/22 职场文书
canvas多重阴影发光效果实现
2021/04/20 Javascript
使用python求解迷宫问题的三种实现方法
2022/03/17 Python
Python中生成随机数据安全性、多功能性、用途和速度方面进行比较
2022/04/14 Python