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的使 ...
随机推荐
- (转)MySQL索引原理及慢查询优化
转自美团技术博客,原文地址:http://tech.meituan.com/mysql-index.html 建索引的一些原则: 1.最左前缀匹配原则,非常重要的原则,mysql会一直向右匹配直到遇到 ...
- vim 支持C++11 lambda表达式
http://www.vim.org/scripts/script.php?script_id=3797 Tar contains just the required .vim files, so u ...
- Windows 7 激活时的坑
前段时间,桌面上有两个文件用各种方法删除不了. 然后今天终于进了PE系统,使用DG把这两货干掉了. 重启进入Windows,提示我 不是正版,今天必须激活,桌面变成了一片黑... 打开小马激活工具OE ...
- C C++ 语法
非常酷的网站: http://yige.org/cpp/defined_data_types.php 在Linux下有一个目录/proc/$(pid),这个目录保存了进程号为pid的进程运行时的所有信 ...
- C# BlockCollection
1.BlockCollection集合是一个拥有阻塞功能的集合,它就是完成了经典生产者消费者的算法功能. 它没有实现底层的存储结构,而是使用了IProducerConsumerCollection接口 ...
- [ios基础]IOS应用程序的生命周期问题
—程序的生命周期 a.程序的生命周期是指应用程序启动到应用程序结束整个阶段的全过程 b.每一个IOS应用程序都包含一个UIApplication对象,IOS系统通过该U ...
- SSAS动态添加分区(一)
一.动态分区的好处就不说了,随着时间的推移,不可能一个度量值组都放在一个分区中,处理速度非常慢,如何动态添加分区,如何动态处理分区,成为了很多新手BI工程师一个头痛的问题,废话不多说,分享一下我的经验 ...
- Problem to be sovled
Given an array A of N integers, we draw N discs in a 2D plane such that the I-th disc is centered on ...
- centos 安装redis(一台机器可以安装多个redis)
我在运行时redis版本是2.8 操作前设置以管理员身份: 打开终端输入 su - 安装redis需要确保系统已经安装了(gcc gcc-c++)# yum -y install gcc gcc-c+ ...
- destoon二次开发基础代码
标签调用规则 http://help.destoon.com/develop/22.html 数据字典 http://help.destoon.com/dict.php destoon各类调用汇总 h ...