PHP图片裁剪类
<?php
class ImageTool {
//以宽为标准,如果小于宽,则不剪裁
public static function thumb_img_by_width($src_path, $dest_path,$width,$height){
$info_arr = getimagesize($src_path);
$src_width = $info_arr[0];
$src_height = $info_arr[1];
switch( $info_arr['mime'] ) {
case 'image/gif':
$src_img = imagecreatefromgif( $src_path );
break;
case 'image/jpeg':
$src_img = imagecreatefromjpeg( $src_path );
break;
case 'image/png':
$src_img = imagecreatefrompng( $src_path );
break;
}
if($src_width > $width){
//裁剪
// 改变后的图象的比例
$resize_ratio = ($width) / ($height);
// 实际图象的比例
$ratio = ($src_width) / ($src_height);
if ($ratio >= $resize_ratio) {
// 高度优先
//截取图片中间那一段
$img_center_x = intval(($src_width-($src_height * $resize_ratio))/2);
//如果不需要只截取中间那一段,而是从图片x=0的地方截取,则$img_center_x=0
$newimg = @imagecreatetruecolor($width, $height);
$color = imagecolorallocate($newimg,255,255,255);
imagecolortransparent($newimg,$color);
imagefill($newimg,0,0,$color);
@imagecopyresampled($newimg, $src_img, 0, 0, $img_center_x, 0, $width, $height, (($src_height) * $resize_ratio), $src_height);
// ImageJpeg($newimg);
}else{
// 宽度优先
//截取图片中间那一段
$img_center_y = intval(($src_height-($src_width / $resize_ratio))/2);
//如果不需要只截取中间那一段,而是从图片y=0的地方截取,则$img_center_y=0
$newimg = @imagecreatetruecolor($width, $height);
$color = imagecolorallocate($newimg,255,255,255);
imagecolortransparent($newimg,$color);
imagefill($newimg,0,0,$color);
@imagecopyresampled($newimg, $src_img, 0, 0, 0, $img_center_y, $width, $height, $src_width, (($src_width) / $resize_ratio));
}
}else{
//不裁剪
$newimg = @imagecreatetruecolor($src_width, $src_height);
$color = imagecolorallocate($newimg,255,255,255);
imagecolortransparent($newimg,$color);
imagefill($newimg,0,0,$color);
@imagecopyresampled($newimg, $src_img, 0, 0, 0, 0, $src_width, $src_height, $src_width, $src_height);
}
//统一保存为png格式
imagepng($newimg, $dest_path);
//gc
imagedestroy($src_img);
imagedestroy($newimg);
}
public static function thumb_img($src_path, $dest_path,$width,$height){
$info_arr = getimagesize($src_path);
$src_width = $info_arr[0];
$src_height = $info_arr[1];
switch( $info_arr['mime'] ) {
case 'image/gif':
$src_img = imagecreatefromgif( $src_path );
break;
case 'image/jpeg':
$src_img = imagecreatefromjpeg( $src_path );
break;
case 'image/png':
$src_img = imagecreatefrompng( $src_path );
break;
}
// 改变后的图象的比例
$resize_ratio = ($width) / ($height);
// 实际图象的比例
$ratio = ($src_width) / ($src_height);
if ($ratio >= $resize_ratio) {
// 高度优先
//截取图片中间那一段
$img_center_x = intval(($src_width-($src_height * $resize_ratio))/2);
//如果不需要只截取中间那一段,而是从图片x=0的地方截取,则$img_center_x=0
$newimg = @imagecreatetruecolor($width, $height);
$color = imagecolorallocate($newimg,255,255,255);
imagecolortransparent($newimg,$color);
imagefill($newimg,0,0,$color);
@imagecopyresampled($newimg, $src_img, 0, 0, $img_center_x, 0, $width, $height, (($src_height) * $resize_ratio), $src_height);
// ImageJpeg($newimg);
}else{
// 宽度优先
//截取图片中间那一段
$img_center_y = intval(($src_height-($src_width / $resize_ratio))/2);
//如果不需要只截取中间那一段,而是从图片y=0的地方截取,则$img_center_y=0
$newimg = @imagecreatetruecolor($width, $height);
$color = imagecolorallocate($newimg,255,255,255);
imagecolortransparent($newimg,$color);
imagefill($newimg,0,0,$color);
@imagecopyresampled($newimg, $src_img, 0, 0, 0, $img_center_y, $width, $height, $src_width, (($src_width) / $resize_ratio));
}
//统一保存为png格式
imagepng($newimg, $dest_path);
//gc
imagedestroy($src_img);
imagedestroy($newimg);
}
/*
* 按比例缩放图片,然后居中裁剪
* $src_path 源文件路劲
* $dest_path 裁剪图片存放位置
* $width 裁剪图片宽带
* $height 裁剪图片高度
*/
public static function clip_img($src_path, $dest_path, $width, $height) {
if( file_exists($src_path) ) {
$info_arr = getimagesize($src_path);
$src_width = $info_arr[0];
$src_height = $info_arr[1];
$src_ratio = $src_height / $src_width;
$dest_ratio = $height / $width;
if( $src_ratio < $dest_ratio ) {
$ratio_width = $width;
$ratio_height = ( $height * $src_height ) / $src_width;
}else if( $src_ratio > $dest_ratio ) {
$ratio_height = $height;
$ratio_width = ( $width * $src_width ) / $src_height;
}else {
$ratio_height = $height;
$ratio_width = $width;
}
switch( $info_arr['mime'] ) {
case 'image/gif':
$src_img = imagecreatefromgif( $src_path );
break;
case 'image/jpeg':
$src_img = imagecreatefromjpeg( $src_path );
break;
case 'image/png':
$src_img = imagecreatefrompng( $src_path );
break;
case 'image/bmp':
//待处理
break;
}
//等比例缩放图片
$thumb_img = imagecreatetruecolor( intval($ratio_width), intval($ratio_height) ) ;
$color = imagecolorallocate($thumb_img,255,255,255);
imagecolortransparent($thumb_img,$color);
imagefill($thumb_img,0,0,$color);
imagecopyresampled( $thumb_img, $src_img, 0, 0, 0, 0, $ratio_width, $ratio_height, $src_width, $src_height);
//剪裁
$clip_img = imagecreatetruecolor( intval($width), intval($height) );
imagecopy( $clip_img, $thumb_img, 0, 0, ( $ratio_width - $width ) / 2, ( $ratio_height - $height ) / 2, $width, $height );
//统一保存为png格式
imagepng($clip_img, $dest_path);
//gc
imagedestroy($src_img);
imagedestroy($thumb_img);
imagedestroy($clip_img);
}
}
/*
* 按比例缩放图片
* type = 1 按宽为标准缩放 2 按高为标准缩放
*/
public static function scale_img($src_path,$dest_path,$type,$len){
if( file_exists($src_path) ){
$info_arr = getimagesize($src_path);
$src_width = $info_arr[0];
$src_height = $info_arr[1];
if($type == 1){
$des_width = $len;
$des_height = ( $src_height * $des_width ) / $src_width;
}else{
$des_height = $len;
$des_width = ( $src_width * $des_height ) / $src_height;
}
switch( $info_arr['mime'] ) {
case 'image/gif':
$src_img = imagecreatefromgif( $src_path );
break;
case 'image/jpeg':
$src_img = imagecreatefromjpeg( $src_path );
break;
case 'image/png':
$src_img = imagecreatefrompng( $src_path );
break;
case 'image/bmp':
//待处理
break;
}
$thumb_img = imagecreatetruecolor( intval($des_width), intval($des_height) );
$color = imagecolorallocate($thumb_img,255,255,255);
imagecolortransparent($thumb_img,$color);
imagefill($thumb_img,0,0,$color);
imagecopyresampled( $thumb_img, $src_img, 0, 0, 0, 0, $des_width, $des_height, $src_width, $src_height);
//统一保存为png格式
imagepng($thumb_img, $dest_path);
//gc
imagedestroy($src_img);
imagedestroy($thumb_img);
}
}
}
PHP图片裁剪类的更多相关文章
- bootstrap-wysiwyg 结合 base64 解码 .net bbs 图片操作类 (二) 图片裁剪
图片裁剪参见: http://deepliquid.com/projects/Jcrop/demos.php?demo=thumbnail 一个js插件 http://www.mikes ...
- 使用JCrop进行图片裁剪,裁剪js说明,裁剪预览,裁剪上传,裁剪设计的图片处理的工具类和代码
1.要想制作图片裁剪功能,可以使用网上的裁剪工具JCrop,网址是:https://github.com/tapmodo/Jcrop/ 案例效果如下: 2.引入JCrop的js代码,具体要引入那 ...
- ASP.NET MVC在服务端把异步上传的图片裁剪成不同尺寸分别保存,并设置上传目录的尺寸限制
我曾经试过使用JSAjaxFileUploader插件来把文件.照片以异步的方式上传,就像"MVC文件图片ajax上传轻量级解决方案,使用客户端JSAjaxFileUploader插件01- ...
- struts2+jsp+jquery+Jcrop实现图片裁剪并上传
<1> 使用html标签上传需要裁剪的大图. <2> 在页面呈现大图,使用Jcrop(Jquery)对大图进行裁剪,并且可以进行预览. <3> 选择好截取部分之后发 ...
- Android图片裁剪之自由裁剪
我的博客http://blog.csdn.net/dawn_moon 客户的需求都是非常怪的.我有时候在给客户做项目的时候就想骂客户是sb.可是请你相信我,等你有需求,自己变成客户的时候,给你做项目的 ...
- bootstrap-wysiwyg 结合 base64 解码 .net bbs 图片操作类
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Dr ...
- HTML5本地图片裁剪并上传
最近做了一个项目,这个项目中需要实现的一个功能是:用户自定义头像(用户在本地选择一张图片,在本地将图片裁剪成满足系统要求尺寸的大小).这个功能的需求是:头像最初剪切为一个正方形.如果选择的图片小于规定 ...
- 拍照、本地图片工具类(兼容至Android7.0)
拍照.本地图片工具类:解决了4.4以上剪裁会提示"找不到文件"和6.0动态授予权限,及7.0报FileUriExposedException异常问题. package com.hb ...
- 好用的开源库(二)——uCrop 图片裁剪
最近想要实现图片裁剪的功能,在Github上找到了这个uCrop,star的人挺多的,便是决定入坑,结果长达一个小时的看资料+摸索,终于是在项目中实现了图片裁剪的功能,今天便是来介绍一下uCrop的使 ...
随机推荐
- 产经新闻:公交WiFi这次能扛多久
来源:16WiFi.流量咪 http://www.16wifi.com/18820/mtbd/html/1096904.html 不靠谱也许是成长的烦恼,也可能是本性使然.公交WiFi就给人一种不靠 ...
- R语言学习笔记-机器学习1-3章
在折腾完爬虫还有一些感兴趣的内容后,我最近在看用R语言进行简单机器学习的知识,主要参考了<机器学习-实用案例解析>这本书. 这本书是目前市面少有的,纯粹以R语言为基础讲解的机器学习知识,书 ...
- WSDL2java简单使用
一.使用工具WSDL2java把接口转为本地可调用的.java文件 工具的目录结构: 设置WSDL2Java(URL).bat中的参数 set Axis_Lib=.\lib set Java_Cmd= ...
- ThinkPHP2.2框架执行流程图,ThinkPHP控制器的执行流程
ThinkPHP2.2框架执行原理.流程图在线手册 ThinkPHP控制器的执行流程 对用户的第一次URL访问 http://<serverIp>/My/index.php/Index/s ...
- Go - 数组 和 切片(array、slice)
一.数组 与其他大多数语言类似,Go语言的数组也是一个元素类型相同的定长的序列. (1)数组的创建 数组有 3 种创建方式: 1) [length]Type 2) [length]Type{value ...
- CSS实现多个Div等高,类似表格布局
<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles"%> <% ...
- [转载]Java数组扩容算法及Java对它的应用
原文链接:http://www.cnblogs.com/gw811/archive/2012/10/07/2714252.html Java数组扩容的原理 1)Java数组对象的大小是固定不变的,数组 ...
- 在android程序中加入widget(窗口小部件)并与之交互的关键代码
摘要: widget(窗口小部件)可以增强应用程序的交互性, 是很多应用中都会用到的功能,本文不求大而全,但是会给出程序与widget交互的关键代码 正文: 其实widget是嵌入(embedded) ...
- Form 详细属性--2016年12月4日
属性 名称 说明 AcceptButton 获取或设置当用户按 Enter 键时所单击的窗体上的按钮. AccessibilityObject 获取分配给该控件的 Accessib ...
- angularjs向后台传参,后台收不到数据
angularjs中封装了一个$http服务,用来请求远程资源 参见:HTTP API 其中封装过的$http.post和$http.get使用起来比较方便 后台是php,用$_POST['name' ...