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 05 PHP
如何用phpmyadmin设置mysql数据库用户的权限
Jan 09 PHP
php定义数组和使用示例(php数组的定义方法)
Mar 29 PHP
PHP中创建图像并绘制文字的例子
Nov 19 PHP
一个非常完美的读写ini格式的PHP配置类分享
Feb 12 PHP
joomla数据库操作示例代码
Jan 06 PHP
PHP实现的随机IP函数【国内IP段】
Jul 20 PHP
PHP入门教程之面向对象基本概念实例分析
Sep 11 PHP
php中让人头疼的浮点数运算分析
Oct 10 PHP
PHP封装curl的调用接口及常用函数详解
May 31 PHP
Thinkphp5+Redis实现商品秒杀代码实例讲解
Dec 29 PHP
你真的了解PHP中的引用符号(&)吗
May 12 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
咖啡的传说和历史
2021/03/03 新手入门
php实现执行某一操作时弹出确认、取消对话框
2013/12/30 PHP
深入浅析php json 格式控制
2015/12/24 PHP
PHP设置头信息及取得返回头信息的方法
2016/01/25 PHP
详解php反序列化
2020/06/10 PHP
tbody元素支持嵌套的注意方法
2007/03/24 Javascript
Extjs4 类的定义和扩展实例
2013/06/28 Javascript
javascript轻量级模板引擎juicer使用指南
2014/06/22 Javascript
JS实现点击按钮后框架内载入不同网页的方法
2015/05/05 Javascript
每天一篇javascript学习小结(Function对象)
2015/11/16 Javascript
javascript实现查找数组中最大值方法汇总
2016/02/13 Javascript
JS实现保留n位小数的四舍五入问题示例
2016/08/03 Javascript
学习JavaScript图片预加载模块
2016/11/07 Javascript
EditPlus 正则表达式 实战(3)
2016/12/15 Javascript
BootStrap Select清除选中的状态恢复默认状态
2017/06/20 Javascript
jQuery事件_动力节点Java学院整理
2017/07/05 jQuery
webpack+express实现文件精确缓存的示例代码
2020/06/11 Javascript
[01:34]2014DOTA2 TI预选赛预选赛 选手比赛房大揭秘!
2014/05/20 DOTA
使用Python的Twisted框架实现一个简单的服务器
2015/04/16 Python
Hadoop中的Python框架的使用指南
2015/04/22 Python
Python实现给文件添加内容及得到文件信息的方法
2015/05/28 Python
Python快速从注释生成文档的方法
2016/12/26 Python
django 开发忘记密码通过邮箱找回功能示例
2018/04/17 Python
PyQt5每天必学之带有标签的复选框
2018/04/19 Python
使用Python制作表情包实现换脸功能
2019/07/19 Python
python障碍式期权定价公式
2019/07/19 Python
django使用django-apscheduler 实现定时任务的例子
2019/07/20 Python
python框架flask表单实现详解
2019/11/04 Python
施华洛世奇巴西官网:SWAROVSKI巴西
2019/12/03 全球购物
大学英语演讲稿(中英文对照)
2014/01/14 职场文书
教师开学感言
2014/02/14 职场文书
市场总经理岗位职责
2014/04/11 职场文书
小学班干部竞选演讲稿
2014/04/24 职场文书
《三亚落日》教学反思
2014/04/26 职场文书
2015年高三班主任工作总结
2015/05/21 职场文书
QT与javascript交互数据的实现
2021/05/26 Javascript