1. <?php
  2. /**
  3. * 图像裁剪
  4. * @param $title string 原图路径
  5. * @param $content string 需要裁剪的宽
  6. * @param $encode string 需要裁剪的高
  7. */
  8. function imagecropper($source_path, $target_width, $target_height)
  9. {
  10. $source_info = getimagesize($source_path);
  11. $source_width = $source_info[0];
  12. $source_height = $source_info[1];
  13. $source_mime = $source_info['mime'];
  14. $source_ratio = $source_height / $source_width;
  15. $target_ratio = $target_height / $target_width;
  16.  
  17. // 源图过高
  18. if ($source_ratio > $target_ratio)
  19. {
  20. $cropped_width = $source_width;
  21. $cropped_height = $source_width * $target_ratio;
  22. $source_x = 0;
  23. $source_y = ($source_height - $cropped_height) / 2;
  24. }
  25. // 源图过宽
  26. elseif ($source_ratio < $target_ratio)
  27. {
  28. $cropped_width = $source_height / $target_ratio;
  29. $cropped_height = $source_height;
  30. $source_x = ($source_width - $cropped_width) / 2;
  31. $source_y = 0;
  32. }
  33. // 源图适中
  34. else
  35. {
  36. $cropped_width = $source_width;
  37. $cropped_height = $source_height;
  38. $source_x = 0;
  39. $source_y = 0;
  40. }
  41.  
  42. switch ($source_mime)
  43. {
  44. case 'image/gif':
  45. $source_image = imagecreatefromgif($source_path);
  46. break;
  47.  
  48. case 'image/jpeg':
  49. $source_image = imagecreatefromjpeg($source_path);
  50. break;
  51.  
  52. case 'image/png':
  53. $source_image = imagecreatefrompng($source_path);
  54. break;
  55.  
  56. default:
  57. return false;
  58. break;
  59. }
  60.  
  61. $target_image = imagecreatetruecolor($target_width, $target_height);
  62. $cropped_image = imagecreatetruecolor($cropped_width, $cropped_height);
  63.  
  64. // 裁剪
  65. imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height);
  66. // 缩放
  67. imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $cropped_width, $cropped_height);
  68.  
  69. //保存图片到本地(两者选一)
  70. //$randNumber = mt_rand(00000, 99999). mt_rand(000, 999);
  71. //$fileName = substr(md5($randNumber), 8, 16) .".png";
  72. //imagepng($target_image,'./'.$fileName);
  73. //imagedestroy($target_image);
  74.  
  75. //直接在浏览器输出图片(两者选一)
  76. header('Content-Type: image/jpeg');
  77. imagepng($target_image);
  78. imagedestroy($target_image);
  79. imagejpeg($target_image);
  80. imagedestroy($source_image);
  81. imagedestroy($target_image);
  82. imagedestroy($cropped_image);
  83. }
  84.  
  85. //调用
  86. //imagecropper('./img033.jpg',300,300);
  87. imagecropper('./img033.jpg',140,140);
  88. //imagecropper('./img033.jpg',55,55);
  89. ?>

php 图像裁剪(自定义裁剪图片大小)的更多相关文章

  1. 在ASP.NET MVC下实现单个图片上传, 客户端服务端双重限制图片大小和格式, 服务端裁剪图片

    在"MVC文件图片ajax上传轻量级解决方案,使用客户端JSAjaxFileUploader插件01-单文件上传"一文中,使用JSAjaxFileUploader这款插件实现了单文 ...

  2. iOS圆形图片裁剪,原型图片外面加一个圆环

    /** *  在圆形外面加一个圆环 */ - (void)yuanHuan{ //0.加载图片 UIImage *image = [UIImage imageNamed:@"AppIcon1 ...

  3. iOS裁剪,缩放图片白边问题解决办法

    几年没来了,感觉还是要写点啥,以后碰见问题 解决就写这吧,当是一个随时的笔记也好. iOS裁剪,缩放图片的代码网上也很多了,但是笔者出现了右边和下边出现白边的情况.出现白边的原因是给的size中的CG ...

  4. Android 自定义圆形图片 CircleImageView

    1.效果预览 1.1.布局中写自定义圆形图片的路径即可 1.2.然后看一看图片效果 1.3.原图是这样的 @mipmap/ic_launcher 2.使用过程 2.1.CircleImageView源 ...

  5. UIImageView自适应图片大小

    窗口大小获取: CGRect screenBounds = [ [UIScreenmainScreen]bounds];//返回的是带有状态栏的Rect CGRect rect = [ [UIScre ...

  6. ImageButton设置src图片大小

    需求&起因 有时候没有找到合适大小的资源,需要修改图片大小而且不改变ImageButton的大小.可以通过缩放图片大小的方式改变外观. ImageView的属性android:scaleTyp ...

  7. 关于Android中图片大小、内存占用与drawable文件夹关系的研究与分析

    原文:关于Android中图片大小.内存占用与drawable文件夹关系的研究与分析 相关: Android drawable微技巧,你所不知道的drawable的那些细节 经常会有朋友问我这个问题: ...

  8. C#.Net 上传图片,限制图片大小,检查类型完整版

    C#.Net 上传图片,限制图片大小,检查类型完整版 源代码: 处理图片类,如检查图片大小,按宽度比例缩小图片 public class CImageLibrary{   public enum Va ...

  9. Linux中如何使用gThumb批量调整图片大小

    Linux中如何使用gThumb批量调整图片大小 导读 如果你的工作涉及到图片编辑和处理,就一定会有同时对多张图片进行批量大小调整的经历.虽然大多数图片编辑应用都能够非常容易地批量调整多张图片,但对于 ...

随机推荐

  1. Pandas 合并 concat

    pandas处理多组数据的时候往往会要用到数据的合并处理,使用 concat是一种基本的合并方式.而且concat中有很多参数可以调整,合并成你想要的数据形式. 1.axis(合并方向):axis=0 ...

  2. linux ">/dev/null 2>&1 &"

    0:表示键盘输入(stdin)1:表示标准输出(stdout),系统默认是1 2:表示错误输出(stderr) command >/dev/null 2>&1 &  == ...

  3. python基础 (编码进阶,文件操作和深浅copy)

    1.编码的进阶 字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码. 即先将其他编码的字符串解码(decode)成unicode,再从uni ...

  4. BZOJ1433或洛谷2055 [ZJOI2009]假期的宿舍

    BZOJ原题链接 洛谷原题链接 对于每个需要床位的人向他能睡的床连边,然后就是二分图最大匹配模板了. 这里用匈牙利算法. #include<cstdio> #include<cstr ...

  5. Java第三次实验敏捷开发与XP实验

    实验三-1 1.实验要求: 实验三 敏捷开发与XP实践 http://www.cnblogs.com/rocedu/p/4795776.html, Eclipse的内容替换成IDEA 参考 http: ...

  6. shiro的rememberMe不生效

    问题描述:已经设置了map.put("/**", "user"),但是查看网页Cookie没有值. 问题查思路: 1.确定使用UserFilter过滤器,因为只 ...

  7. docker学习笔记-安装

    安装docker Ubuntu Docker 安装 Docker 支持以下的 Ubuntu 版本: Ubuntu Precise 12.04 (LTS) Ubuntu Trusty 14.04 (LT ...

  8. [转]kaldi中的特征提取

    转:http://blog.csdn.net/wbgxx333/article/details/25778483 本翻译原文http://kaldi.sourceforge.net/feat.html ...

  9. Android Studio向项目中导入jar包的方法

    第一步: 切换到"Project"视图,找到app --> libs目录 第二步: 将需要导入的jar包粘贴到libs目录中,此时还不能看到jar包中的内容 第三步: 右键点 ...

  10. CentOS防火墙配置

    1.查询防火墙状态 service iptables status 2.开启防火墙 service iptables start 3.关闭防火墙 service iptables stop 4.重启防 ...