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 变量定义方法
Jun 14 PHP
PHP无限分类代码,支持数组格式化、直接输出菜单两种方式
May 18 PHP
dhtmlxTree目录树增加右键菜单以及拖拽排序的实现方法
Apr 26 PHP
PHP JS Ip地址及域名格式检测代码
Sep 27 PHP
XAMPP安装与使用方法详细解析
Nov 27 PHP
php把session写入数据库示例
Feb 26 PHP
php 批量添加多行文本框textarea一行一个
Jun 03 PHP
使用PHP接受文件并获得其后缀名的方法
Aug 05 PHP
浅析Laravel5中队列的配置及使用
Aug 04 PHP
PHP crypt()函数的用法讲解
Feb 15 PHP
PHP+mysql防止SQL注入的方法小结
Apr 27 PHP
asp.net和php的区别点总结
Oct 10 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
for循环连续求和、九九乘法表代码
2012/02/20 PHP
php通过前序遍历树实现无需递归的无限极分类
2015/07/10 PHP
thinkPHP中volist标签用法示例
2016/12/06 PHP
php如何利用pecl安装mongodb扩展详解
2019/01/09 PHP
PHP实现会员账号单唯一登录的方法分析
2019/03/07 PHP
php中关于换行的实例写法
2019/09/26 PHP
jquery 元素相对定位代码
2010/10/15 Javascript
jQuery ajax(复习)—Baidu ajax request分离版
2013/01/24 Javascript
瀑布流布局并自动加载实现代码
2013/03/12 Javascript
在JavaScript中构建ArrayList示例代码
2014/09/17 Javascript
百度地图API之本地搜索与范围搜索
2015/07/30 Javascript
jQuery实现下滑菜单导航效果代码
2015/08/25 Javascript
JavaScript代码实现禁止右键、禁选择、禁粘贴、禁shift、禁ctrl、禁alt
2015/11/17 Javascript
AngularJS中$watch和$timeout的使用示例
2016/09/20 Javascript
js仿淘宝商品放大预览功能
2017/03/15 Javascript
vue货币过滤器的实现方法
2017/04/01 Javascript
详解Vue使用命令行搭建单页面应用
2017/05/24 Javascript
jQuery简介_动力节点Java学院整理
2017/07/04 jQuery
详解小程序输入框闪烁及重影BUG解决方案
2018/08/31 Javascript
详解基于vue-cli3.0如何构建功能完善的前端架子
2018/10/09 Javascript
微信小程序导航栏滑动定位功能示例(实现CSS3的positionsticky效果)
2019/01/24 Javascript
jquery.pager.js实现分页效果
2019/07/29 jQuery
python 默认参数问题的陷阱
2016/02/29 Python
Windows下搭建python开发环境详细步骤
2020/07/20 Python
Python爬取网易云音乐上评论火爆的歌曲
2017/01/19 Python
Python实现1-9数组形成的结果为100的所有运算式的示例
2017/11/03 Python
pandas修改DataFrame列名的实现方法
2019/02/22 Python
flask框架jinja2模板与模板继承实例分析
2019/08/01 Python
python matplotlib 画dataframe的时间序列图实例
2019/11/20 Python
Django 实现将图片转为Base64,然后使用json传输
2020/03/27 Python
class类在python中获取金融数据的实例方法
2020/12/10 Python
CSS超出文本指定宽度用省略号代替和文本不换行
2016/05/05 HTML / CSS
Trina Turk官网:美国时装和泳装品牌
2018/06/10 全球购物
美国葡萄酒网上商店:Martha Stewart Wine Co.
2019/03/17 全球购物
西安交大自主招生自荐信
2014/01/27 职场文书
MySQL数据迁移相关总结
2021/04/29 MySQL