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.MVC的模板标签系统(一)
Sep 05 PHP
提升PHP执行速度全攻略(上)
Oct 09 PHP
一个PHP的QRcode类与大家分享
Nov 13 PHP
PHP swfupload图片上传的实例代码
Sep 30 PHP
zf框架的数据库追踪器使用示例
Mar 13 PHP
php使用session二维数组实例
Nov 06 PHP
PHP中file_get_contents函数抓取https地址出错的解决方法(两种方法)
Sep 22 PHP
php 使用curl模拟登录人人(校内)网的简单实例
Jun 06 PHP
PHP实现多关键字加亮功能
Oct 21 PHP
php中Ioc(控制反转)和Di(依赖注入)
May 07 PHP
php获取文章内容第一张图片的方法示例
Jul 03 PHP
php反序列化长度变化尾部字符串逃逸(0CTF-2016-piapiapia)
Feb 15 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
在PHP3中实现SESSION的功能(三)
2006/10/09 PHP
PHP应用JSON技巧讲解
2013/02/03 PHP
phpmyadmin config.inc.php配置示例
2013/08/27 PHP
利用php下载xls文件(自己动手写的)
2014/04/18 PHP
PHP连接MSSQL时nvarchar字段长度被截断为255的解决方法
2014/12/25 PHP
PHP基于socket实现的简单客户端和服务端通讯功能示例
2017/07/10 PHP
php设计模式之单例模式用法经典示例分析
2019/09/20 PHP
javascript 读取图片文件的大小
2009/06/25 Javascript
关于jQuery对象数据缓存Cache原理以及jQuery.data详解
2013/04/07 Javascript
jQuery入门基础知识学习指南
2015/08/14 Javascript
Vue.js组件tab实现选项卡切换
2020/03/23 Javascript
js实现添加删除表格(两种方法)
2017/04/27 Javascript
实例讲解javascript实现异步图片上传方法
2017/12/05 Javascript
vue中Npm run build 根据环境传递参数方法来打包不同域名
2018/03/29 Javascript
浅谈Angular6的服务和依赖注入
2018/06/27 Javascript
从Vuex中取出数组赋值给新的数组,新数组push时报错的解决方法
2018/09/18 Javascript
react脚手架如何配置less和ant按需加载的方法步骤
2018/11/28 Javascript
js canvas实现写字动画效果
2018/11/30 Javascript
vue-i18n结合Element-ui的配置方法
2019/05/20 Javascript
JavaScript:ES2019 的新特性(译)
2019/08/08 Javascript
实例分析JS中的相等性判断===、 ==和Object.is()
2019/11/17 Javascript
自己编程中遇到的Python错误和解决方法汇总整理
2015/06/03 Python
python中数据爬虫requests库使用方法详解
2018/02/11 Python
python多进程提取处理大量文本的关键词方法
2018/06/05 Python
windows下cx_Freeze生成Python可执行程序的详细步骤
2018/10/09 Python
python读写配置文件操作示例
2019/07/03 Python
Philosophy美国官网:美国美容品牌
2016/08/15 全球购物
Coltorti Boutique官网:来自意大利的设计师品牌买手店
2018/11/09 全球购物
Lookfantastic澳大利亚官网:英国知名美妆购物网站
2021/01/07 全球购物
什么是抽象
2015/12/13 面试题
校园广播稿500字
2014/02/04 职场文书
专科生就业求职信
2014/06/22 职场文书
家长学校教学计划
2015/01/19 职场文书
刑事附带民事代理词
2015/05/25 职场文书
python基础学习之生成器与文件系统知识总结
2021/05/25 Python
Nginx 502 bad gateway错误解决的九种方案及原因
2022/08/14 Servers