php 图像裁剪(自定义裁剪图片大小)
- <?php
- /**
- * 图像裁剪
- * @param $title string 原图路径
- * @param $content string 需要裁剪的宽
- * @param $encode string 需要裁剪的高
- */
- function imagecropper($source_path, $target_width, $target_height)
- {
- $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);
- //保存图片到本地(两者选一)
- //$randNumber = mt_rand(00000, 99999). mt_rand(000, 999);
- //$fileName = substr(md5($randNumber), 8, 16) .".png";
- //imagepng($target_image,'./'.$fileName);
- //imagedestroy($target_image);
- //直接在浏览器输出图片(两者选一)
- header('Content-Type: image/jpeg');
- imagepng($target_image);
- imagedestroy($target_image);
- imagejpeg($target_image);
- imagedestroy($source_image);
- imagedestroy($target_image);
- imagedestroy($cropped_image);
- }
- //调用
- //imagecropper('./img033.jpg',300,300);
- imagecropper('./img033.jpg',140,140);
- //imagecropper('./img033.jpg',55,55);
- ?>
php 图像裁剪(自定义裁剪图片大小)的更多相关文章
- 在ASP.NET MVC下实现单个图片上传, 客户端服务端双重限制图片大小和格式, 服务端裁剪图片
在"MVC文件图片ajax上传轻量级解决方案,使用客户端JSAjaxFileUploader插件01-单文件上传"一文中,使用JSAjaxFileUploader这款插件实现了单文 ...
- iOS圆形图片裁剪,原型图片外面加一个圆环
/** * 在圆形外面加一个圆环 */ - (void)yuanHuan{ //0.加载图片 UIImage *image = [UIImage imageNamed:@"AppIcon1 ...
- iOS裁剪,缩放图片白边问题解决办法
几年没来了,感觉还是要写点啥,以后碰见问题 解决就写这吧,当是一个随时的笔记也好. iOS裁剪,缩放图片的代码网上也很多了,但是笔者出现了右边和下边出现白边的情况.出现白边的原因是给的size中的CG ...
- Android 自定义圆形图片 CircleImageView
1.效果预览 1.1.布局中写自定义圆形图片的路径即可 1.2.然后看一看图片效果 1.3.原图是这样的 @mipmap/ic_launcher 2.使用过程 2.1.CircleImageView源 ...
- UIImageView自适应图片大小
窗口大小获取: CGRect screenBounds = [ [UIScreenmainScreen]bounds];//返回的是带有状态栏的Rect CGRect rect = [ [UIScre ...
- ImageButton设置src图片大小
需求&起因 有时候没有找到合适大小的资源,需要修改图片大小而且不改变ImageButton的大小.可以通过缩放图片大小的方式改变外观. ImageView的属性android:scaleTyp ...
- 关于Android中图片大小、内存占用与drawable文件夹关系的研究与分析
原文:关于Android中图片大小.内存占用与drawable文件夹关系的研究与分析 相关: Android drawable微技巧,你所不知道的drawable的那些细节 经常会有朋友问我这个问题: ...
- C#.Net 上传图片,限制图片大小,检查类型完整版
C#.Net 上传图片,限制图片大小,检查类型完整版 源代码: 处理图片类,如检查图片大小,按宽度比例缩小图片 public class CImageLibrary{ public enum Va ...
- Linux中如何使用gThumb批量调整图片大小
Linux中如何使用gThumb批量调整图片大小 导读 如果你的工作涉及到图片编辑和处理,就一定会有同时对多张图片进行批量大小调整的经历.虽然大多数图片编辑应用都能够非常容易地批量调整多张图片,但对于 ...
随机推荐
- Pandas 合并 concat
pandas处理多组数据的时候往往会要用到数据的合并处理,使用 concat是一种基本的合并方式.而且concat中有很多参数可以调整,合并成你想要的数据形式. 1.axis(合并方向):axis=0 ...
- linux ">/dev/null 2>&1 &"
0:表示键盘输入(stdin)1:表示标准输出(stdout),系统默认是1 2:表示错误输出(stderr) command >/dev/null 2>&1 & == ...
- python基础 (编码进阶,文件操作和深浅copy)
1.编码的进阶 字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码. 即先将其他编码的字符串解码(decode)成unicode,再从uni ...
- BZOJ1433或洛谷2055 [ZJOI2009]假期的宿舍
BZOJ原题链接 洛谷原题链接 对于每个需要床位的人向他能睡的床连边,然后就是二分图最大匹配模板了. 这里用匈牙利算法. #include<cstdio> #include<cstr ...
- Java第三次实验敏捷开发与XP实验
实验三-1 1.实验要求: 实验三 敏捷开发与XP实践 http://www.cnblogs.com/rocedu/p/4795776.html, Eclipse的内容替换成IDEA 参考 http: ...
- shiro的rememberMe不生效
问题描述:已经设置了map.put("/**", "user"),但是查看网页Cookie没有值. 问题查思路: 1.确定使用UserFilter过滤器,因为只 ...
- docker学习笔记-安装
安装docker Ubuntu Docker 安装 Docker 支持以下的 Ubuntu 版本: Ubuntu Precise 12.04 (LTS) Ubuntu Trusty 14.04 (LT ...
- [转]kaldi中的特征提取
转:http://blog.csdn.net/wbgxx333/article/details/25778483 本翻译原文http://kaldi.sourceforge.net/feat.html ...
- Android Studio向项目中导入jar包的方法
第一步: 切换到"Project"视图,找到app --> libs目录 第二步: 将需要导入的jar包粘贴到libs目录中,此时还不能看到jar包中的内容 第三步: 右键点 ...
- CentOS防火墙配置
1.查询防火墙状态 service iptables status 2.开启防火墙 service iptables start 3.关闭防火墙 service iptables stop 4.重启防 ...