<?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. ANDROID_HOME on Mac OS X

    Where the Android-SDK is installed depends on how you installed it. If you downloaded the SDK throug ...

  2. System.Properties和System.getenv区别

    网上很多使用的是getProperties.说获得系统变量,但是其实不正确.getProperties中所谓的"system properties"其实是指"java s ...

  3. 大熊君学习html5系列之------WebStorage(客户端轻量级存储方案)

    一,开篇分析 Hi,大家好!大熊君又和大家见面了,(*^__^*) 嘻嘻……,这系列文章主要是学习Html5相关的知识点,以学习API知识点为入口,由浅入深的引入实例, 让大家一步一步的体会" ...

  4. Linux/CentOS 搭建 SVN 项目

    1.安装svn yum  -y  install   subversion 2.创建svn仓库路径 mkdir  -p   /opt/svn/project1 mkdir  -p   /opt/svn ...

  5. ReflectionToStringBuilder类

    ReflectionToStringBuilder类是用来实现类中的toString()方法的类,它采用Java反射机制(Reflection),通过reflection包中的AccessibleOb ...

  6. 深入浅出iOS事件机制

    原文地址: http://zhoon.github.io/ios/2015/04/12/ios-event.html 本文章将讲解有关iOS事件的传递机制,如有错误或者不同的见解,欢迎留言指出. iO ...

  7. 安装第三方模块方法和requests

    如何安装第三方模块 pip3         pip3 install xxxx          源码         下载,解压         进入目录 python setup.py inst ...

  8. mysql数据表拷贝

    select * into 目标表名 from 源表名 insert into 目标表名(fld1, fld2) select fld1, 5 from 源表名 以上两句都是将 源表 的数据插入到 目 ...

  9. unlink和close关系

    今天看到nginx用文件锁实现互斥的实现方案时,发现,unlink文件后还可需用fd,很是纳闷!于是搜索到此文,并自测了下,涨姿势了~分享给大家~ 原理: 每一个文件,都可以通过一个struct st ...

  10. SelectionSort,选择排序

    /**算法:选择排序1,从当前未排序的正数中找一个最小的整数,将它放在已排序的整数列表的最后2.要点:选择排序选最小的,往左边选*/ #include <stdio.h>void Sele ...