<?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图片裁剪类的更多相关文章

  1. bootstrap-wysiwyg 结合 base64 解码 .net bbs 图片操作类 (二) 图片裁剪

    图片裁剪参见: http://deepliquid.com/projects/Jcrop/demos.php?demo=thumbnail        一个js插件 http://www.mikes ...

  2. 使用JCrop进行图片裁剪,裁剪js说明,裁剪预览,裁剪上传,裁剪设计的图片处理的工具类和代码

     1.要想制作图片裁剪功能,可以使用网上的裁剪工具JCrop,网址是:https://github.com/tapmodo/Jcrop/ 案例效果如下: 2.引入JCrop的js代码,具体要引入那 ...

  3. ASP.NET MVC在服务端把异步上传的图片裁剪成不同尺寸分别保存,并设置上传目录的尺寸限制

    我曾经试过使用JSAjaxFileUploader插件来把文件.照片以异步的方式上传,就像"MVC文件图片ajax上传轻量级解决方案,使用客户端JSAjaxFileUploader插件01- ...

  4. struts2+jsp+jquery+Jcrop实现图片裁剪并上传

    <1> 使用html标签上传需要裁剪的大图. <2> 在页面呈现大图,使用Jcrop(Jquery)对大图进行裁剪,并且可以进行预览. <3> 选择好截取部分之后发 ...

  5. Android图片裁剪之自由裁剪

    我的博客http://blog.csdn.net/dawn_moon 客户的需求都是非常怪的.我有时候在给客户做项目的时候就想骂客户是sb.可是请你相信我,等你有需求,自己变成客户的时候,给你做项目的 ...

  6. bootstrap-wysiwyg 结合 base64 解码 .net bbs 图片操作类

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Dr ...

  7. HTML5本地图片裁剪并上传

    最近做了一个项目,这个项目中需要实现的一个功能是:用户自定义头像(用户在本地选择一张图片,在本地将图片裁剪成满足系统要求尺寸的大小).这个功能的需求是:头像最初剪切为一个正方形.如果选择的图片小于规定 ...

  8. 拍照、本地图片工具类(兼容至Android7.0)

    拍照.本地图片工具类:解决了4.4以上剪裁会提示"找不到文件"和6.0动态授予权限,及7.0报FileUriExposedException异常问题. package com.hb ...

  9. 好用的开源库(二)——uCrop 图片裁剪

    最近想要实现图片裁剪的功能,在Github上找到了这个uCrop,star的人挺多的,便是决定入坑,结果长达一个小时的看资料+摸索,终于是在项目中实现了图片裁剪的功能,今天便是来介绍一下uCrop的使 ...

随机推荐

  1. 高版本api在低版本中的兼容

    直接上例子,看如何避免crash. eg:根据给出路径,获取此路径所在分区的总空间大小. 文档说明:获取文件系统用量情况,在API level 9及其以上的系统,可直接调用File对象的相关方法,以下 ...

  2. 界面通信之block传值

    block传值有两种方式 ⽅式⼀: 使⽤block属性实现回调传值 ⽅式⼆: 在⽅法中定义block实现回调传值 方式一比较便于理解而且常用,下面介绍方式一是如何传值的 使用block属性传值和代理传 ...

  3. 深入理解javascript原型和闭包(3)——prototype原型

    既typeof之后的另一位老朋友! prototype也是我们的老朋友,即使不了解的人,也应该都听过它的大名.如果它还是您的新朋友,我估计您也是javascript的新朋友. 在咱们的第一节(深入理解 ...

  4. 处理 input 上传图片,浏览器读取图片大小过程中遇到到的坑(兼容IE8\9)

    为了 解决这个坑~ 已经 累傻了.. 周末再 写吧..

  5. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

  6. 15个JavaScript本地存储技术的函数库和工具

    当构建更复杂的JavaScript应用程序运行在用户的浏览器是非常有用的,它可以在浏览器中存储信息,这样的信息可以被共享在不同的页面,浏览会话. 在最近的过去,这将有可能只被cookies文本文件保存 ...

  7. postgresql利用pg_upgrade升级数据库(从8.4升级到9.5)

    其他见:http://my.oschina.net/ensn/blog/636766 本文利用pg_upgrade实现将8.4.18版本升级到9.5.0版本,8.4.18版本为RedHat系统自带pg ...

  8. js倒计时代码 适合于促销-倒计时代码

    <div class="tiem_price clearfix fonts" style="margin-top:15px;"> <div c ...

  9. errno.h

    linux 中c语言使用errno.h头文件来记录错误信息以及定义返回错误代码的宏. strerror(errno)打印错误信息 1. warning: implicit declaration of ...

  10. 基于Z-WAVE 协议的LED智能照明系统的研究笔记

    LED调光基础: ☆:LED照明调光控制信号的方式有两种: 1. 通过PWM信号控制LED灯具开关电源的占空比从而实现调光: 2. 通过调光控制信号和交流电源供电线合用的两线式或三线式(例如LED相控 ...