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的使 ...
随机推荐
- arcgis engine 监听element的添加、更新和删除事件(使用IMovePointFeedback)
VB代码: 复制进程序稍作修改变量名和事件逻辑即可使用. Members AllPropertiesMethodsInheritedNon-inherited Description Displa ...
- 线程安全的无锁RingBuffer的实现
这里的线程安全,是指一个读线程和一个写线程,读写两个线程是安全的,而不是说多个读线程和多个写线程是安全的.. 在程序设计中,我们有时会遇到这样的情况,一个线程将数据写到一个buffer中,另外一个线程 ...
- [nginx学习之道]linux的nginx安装
准备:首先要安装下一些gcc库用于编译 和一些nginx的扩展lib包: [root@localhost nginx-]# yum -y install gcc gcc-c++ autoconf au ...
- 【荐】PDO防 SQL注入攻击 原理分析 以及 使用PDO的注意事项
我们都知道,只要合理正确使用PDO,可以基本上防止SQL注入的产生,本文主要回答以下几个问题: 为什么要使用PDO而不是mysql_connect? 为何PDO能防注入? 使用PDO防注入的时候应该特 ...
- highcharts的表名
line:直线图 spline:曲线图 area:面积图 areaspline:曲线面积图 arearange:面积范围图 areasplinerange:曲线面积范围图 column:柱状图 col ...
- FileZillaFTP使用教程
FileZillaServer.exe服务启动和关闭程序 FileZilla Server Interface.exe 服务 管理程序 配置ftp服务器的用户名,密码,目录,目录读写权限 启动File ...
- Ubuntu 16.04 安装 Apache, MySQL, PHP7
1.安装之前先更新系统 1 sudo apt update 2.安装Apache2 1 sudo apt install apache2 3.安装PHP 1 sudo apt install php ...
- css中background背景属性概
css中background背景属性概 background:url(背景图片路径) no-repeat;/*不重复默认在左上方*/background:url(背景图片路径) no-repeat ...
- yii中的cookie的发送和读取
cookies: //新创建的cookie会从本地传到服务器上,然后从服务器获取. (1) cookie的发送 $cookies = Yii::$app->response->cookie ...
- css垂直居中 两种方法
在前端面试的时候我们经常会被问道怎样使一个元素在页面垂直居中呢,这也是一个老生常谈的问题了. 解决的方法基本都是使用定位来实现 div{display: fixed;left: 50%;top: 50 ...