public function text(){// 需要缩放图片$path = 'uploads/20220121/8c5b13e7abb423709ab11a0b6498af84.jpg'; $height = 0;  $width = 0;// 判断图片是否存在  如果存在 按照原图片的一半进行缩放if (file_exists($path)){$imgInfo = getimagesize($path);  // 获取图片的信息$width = $imgInfo[0]/2;   // 源图片高的1/2$height = $imgInfo[1]/2;  // 源图片高的1/2}$this->image_cropper($path,$width,$height,'uploads');}/*** 图像裁剪* @param $source_path string 原图路径* @param $target_width string 需要裁剪的宽* @param $target_height string 需要裁剪的高* @param $target_path string 需要保存的路径*/function image_cropper($source_path, $target_width, $target_height, $target_path){$source_info     = getimagesize($source_path);$source_width     = $source_info[0];$source_height     = $source_info[1];$source_mime     = $source_info['mime'];$source_ratio     = $source_height / $source_width;$target_ratio     = $target_height / $target_width;if ($source_ratio > $target_ratio) // 源图过高{$cropped_width = $source_width;$cropped_height = $source_width * $target_ratio;$source_x = 0;$source_y = ($source_height - $cropped_height) / 2;}elseif ($source_ratio < $target_ratio){  // 源图过宽$cropped_width = $source_height / $target_ratio;$cropped_height = $source_height;$source_x = ($source_width - $cropped_width) / 2;$source_y = 0;}else{ // 源图适中$cropped_width = $source_width;$cropped_height = $source_height;$source_x = 0;$source_y = 0;}switch ($source_mime){case 'image/gif':$source_image = imagecreatefromgif($source_path);break;case 'image/jpeg':$source_image = imagecreatefromjpeg($source_path);break;case 'image/png':$source_image = imagecreatefrompng($source_path);break;default:return false;break;}$target_image = imagecreatetruecolor($target_width, $target_height);$cropped_image = imagecreatetruecolor($cropped_width, $cropped_height);// 裁剪imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height);// 缩放imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $cropped_width, $cropped_height);//保存图片到本地(两者选一)$dir = $target_path. '/'. date("Ymd") . '/';if (!is_dir($dir)) {mkdir($dir, 0777);}$fileName = $dir.date("YmdHis").uniqid().'.jpg';if(!imagejpeg($target_image,'./'.$fileName)){$fileName = '';}imagedestroy($target_image);return $fileName;}

php等比例剪切缩放图片相关推荐

  1. Web前端_配合swiper滑动插件实现同比例双指缩放图片

    参考过的一篇文章:https://blog.csdn.net/gl0ry/article/details/56055414 html实例代码: 缩放的图片需要放在class 为list 的div里面 ...

  2. 关于图片按比例自适应缩放

    今天在看视觉规范的时候,大高走过来说 帮他们那边看一个问题.又是一个关于自适应的问题.不过,我喜欢.瞄了一下,需求是这样的:用户上传照片,照片的尺寸未知:需要生成一个预览,这个预览图 要根据提供给用户 ...

  3. python 等比例缩放图片 自写

    Python等比例缩放图片 使用了 OpenCV 进行图片的读取 输入:利用 cv2.imread 函数读取的 Mat 矩阵 输出:缩放后的 Mat 矩阵(示例代码为缩放到 512x512 大小,也可 ...

  4. [Xcode 实际操作]六、媒体与动画-(1)使用图形上下文按一定比例缩放图片

    目录:[Swift]Xcode实际操作 本文将演示如何通过图形上下文,来实现图片缩放的功能. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] 1 import UI ...

  5. java 等比缩放图片_java处理图片按比例缩放功能

    java中的图片按比例缩放功能 1. 按固定长宽进行缩放 /* * 图片缩放,w,h为缩放的目标宽度和高度 * src为源文件目录,dest为缩放后保存目录 */ public static void ...

  6. python调整屏幕缩放比例_python实现批量按比例缩放图片效果

    本文实例为大家分享了python实现批量按比例缩放图片的具体代码,供大家参考,具体内容如下 把脚本文件放在要缩放的文件夹下面. 双击运行脚本,输入要缩放的系数.脚本会在当前目录下创建一个scaledI ...

  7. php 改变图片的宽高,php缩放图片(根据宽高的等比例缩放)示例

    php缩放图片(根据宽高的等比例缩放)示例 发布于 2014-12-07 08:53:31 | 149 次阅读 | 评论: 0 | 来源: 网友投递 PHP开源脚本语言PHP(外文名: Hyperte ...

  8. [原创] 图片操作的类(ImageLibrary)-按比例缩放图片

    [原创] 图片操作的类(ImageLibrary)-按比例缩放图片 http://www.vjsdn.com/bbs/bbsTopicDetails.aspx?pid=241 转载于:https:// ...

  9. java实现按比例缩放图片技巧

    java实现按比例缩放图片技巧 案例需求: 根据指定的文件名称,用户给定的缩放比例来完成缩放图片 思路分析: 1.文件对象File 2.BufferedImage构造器: 图像数据的访问的缓冲器 3. ...

最新文章

  1. ros的密码忘记解决方法
  2. maven学习6 Eclipse下Tomcat常用设置
  3. mongo在哪创建管理员_MongoDB添加用户
  4. mysql导入sql文件过大或连接超时的解决办法
  5. 算法导论 算法_算法导论
  6. try... except异常处理结构
  7. 6-2 有序数组的插入 (20分)
  8. 如何将列表分成大小均匀的块?
  9. keras 多GPU训练,单GPU权重保存和预测
  10. 斯诺登逃亡之路上的守护天使都有谁
  11. F(x) - NU ABO 韩中(繁)ass字幕
  12. C++_基础1.1初识——Hellow world
  13. 华为中兴为何对未来信心十足?
  14. C++文件操作的具体方法
  15. Java初转型-Maven入门
  16. response.reset()
  17. windows清理c盘的三种有效方法
  18. 最新ie图标变灰css,css+js整站变灰(兼容IE7+)
  19. C++ bind2nd用法
  20. 毛笔字体设计 让经典字体焕然新生

热门文章

  1. 【托业】【怪兽】TEST04
  2. 7、PN结的电容效应
  3. 配音秀显示服务器去配音了,英语配音秀
  4. libevent configure: error: openssl is a must but can not be found.error while loading shared librari
  5. 实现路由器自动登录校园网(edu)
  6. C语言初学零基础(一)——安装Visual Studio2022
  7. 水平输送水汽通量matlab,水汽通量的经向输送分析
  8. MBA案例分析: 美国西南航空的十年发展战略规划
  9. 手写Spring DI依赖注入,嘿,你的益达!
  10. 公司倒闭后不注销有什么后果?不注销公司的几大结果