PHP图像裁剪缩略裁切类源码及使用方法

最近在做网页拖拽验证码的开源项目,需要在服务端生成图片对应的可移动的色块,但是网上的资源都是做缩略图,对整个图片进行缩放的,所以自己动手,完成了对图片进行裁剪小块的工具

<?php
namespace App\Libs;
/**
* 2016-01-07 15:54:58
* Lixiaoyu
*
* mode 1 : 强制裁剪,生成图片严格按照需要,不足放大,超过裁剪,图片始终铺满
* mode 2 : 和1类似,但不足的时候 不放大 会产生补白,可以用png消除。
* mode 3 : 只缩放,不裁剪,保留全部图片信息,会产生补白,
* mode 4 : 只缩放,不裁剪,保留全部图片信息,生成图片大小为最终缩放后的图片有效信息的实际大小,不产生补白
* 默认补白为白色,如果要使补白成透明像素,请使用SaveAlpha()方法代替SaveImage()方法
*/
class ImageCrop{
var $sImage;
var $dImage;
var $src_file;
var $dst_file;
var $src_width;
var $src_height;
var $src_ext;
var $src_type;
function __construct($src_file,$dst_file=''){
$this->src_file=$src_file;
$this->dst_file=$dst_file;
$this->LoadImage();
}
function SetSrcFile($src_file){
$this->src_file=$src_file;
}
function SetDstFile($dst_file){
$this->dst_file=$dst_file;
}
function LoadImage(){
list($this->src_width, $this->src_height, $this->src_type) = getimagesize($this->src_file);
switch($this->src_type) {
case IMAGETYPE_JPEG :
$this->sImage=imagecreatefromjpeg($this->src_file);
$this->ext='jpg';
break;
case IMAGETYPE_PNG :
$this->sImage=imagecreatefrompng($this->src_file);
$this->ext='png';
break;
case IMAGETYPE_GIF :
$this->sImage=imagecreatefromgif($this->src_file);
$this->ext='gif';
break;
default:
exit();
}
}
function SaveImage($fileName=''){
$this->dst_file=$fileName ? $fileName : $this->dst_file;
switch($this->src_type) {
case IMAGETYPE_JPEG :
imagejpeg($this->dImage,$this->dst_file,100);
break;
case IMAGETYPE_PNG :
imagepng($this->dImage,$this->dst_file);
break;
case IMAGETYPE_GIF :
imagegif($this->dImage,$this->dst_file);
break;
default:
break;
}
}
function OutImage(){
switch($this->src_type) {
case IMAGETYPE_JPEG :
header('Content-type: image/jpeg');
imagejpeg($this->dImage);
break;
case IMAGETYPE_PNG :
header('Content-type: image/png');
imagepng($this->dImage);
break;
case IMAGETYPE_GIF :
header('Content-type: image/gif');
imagegif($this->dImage);
break;
default:
break;
}
}
function SaveAlpha($fileName=''){
$this->dst_file=$fileName ? $fileName . '.png' : $this->dst_file .'.png';
imagesavealpha($this->dImage, true);
imagepng($this->dImage,$this->dst_file);
}
function OutAlpha(){
imagesavealpha($this->dImage, true);
header('Content-type: image/png');
imagepng($this->dImage);
}
function destory(){
imagedestroy($this->sImage);
imagedestroy($this->dImage);
}
function Crop($dst_width,$dst_height,$mode=1,$dst_file=''){
if($dst_file) $this->dst_file=$dst_file;
$this->dImage = imagecreatetruecolor($dst_width,$dst_height);
$bg = imagecolorallocatealpha($this->dImage,255,255,255,127);
imagefill($this->dImage, 0, 0, $bg);
imagecolortransparent($this->dImage,$bg);
$ratio_w=1.0 * $dst_width / $this->src_width;
$ratio_h=1.0 * $dst_height / $this->src_height;
$ratio=1.0;
switch($mode){
case 1: // always crop
if( ($ratio_w < 1 && $ratio_h < 1) || ($ratio_w > 1 && $ratio_h > 1)){
$ratio = $ratio_w < $ratio_h ? $ratio_h : $ratio_w;
$tmp_w = (int)($dst_width / $ratio);
$tmp_h = (int)($dst_height / $ratio);
$tmp_img=imagecreatetruecolor($tmp_w , $tmp_h);
$src_x = (int) (($this->src_width-$tmp_w)/2) ;
$src_y = (int) (($this->src_height-$tmp_h)/2) ;
imagecopy($tmp_img, $this->sImage, 0,0,$src_x,$src_y,$tmp_w,$tmp_h);
imagecopyresampled($this->dImage,$tmp_img,0,0,0,0,$dst_width,$dst_height,$tmp_w,$tmp_h);
imagedestroy($tmp_img);
}else{
$ratio = $ratio_w < $ratio_h ? $ratio_h : $ratio_w;
$tmp_w = (int)($this->src_width * $ratio);
$tmp_h = (int)($this->src_height * $ratio);
$tmp_img=imagecreatetruecolor($tmp_w ,$tmp_h);
imagecopyresampled($tmp_img,$this->sImage,0,0,0,0,$tmp_w,$tmp_h,$this->src_width,$this->src_height);
$src_x = (int)($tmp_w - $dst_width) / 2 ;
$src_y = (int)($tmp_h - $dst_height) / 2 ;
imagecopy($this->dImage, $tmp_img, 0,0,$src_x,$src_y,$dst_width,$dst_height);
imagedestroy($tmp_img);
}
break;
case 2: // only small
if($ratio_w < 1 && $ratio_h < 1){
$ratio = $ratio_w < $ratio_h ? $ratio_h : $ratio_w;
$tmp_w = (int)($dst_width / $ratio);
$tmp_h = (int)($dst_height / $ratio);
$tmp_img=imagecreatetruecolor($tmp_w , $tmp_h);
$src_x = (int) ($this->src_width-$tmp_w)/2 ;
$src_y = (int) ($this->src_height-$tmp_h)/2 ;
imagecopy($tmp_img, $this->sImage, 0,0,$src_x,$src_y,$tmp_w,$tmp_h);
imagecopyresampled($this->dImage,$tmp_img,0,0,0,0,$dst_width,$dst_height,$tmp_w,$tmp_h);
imagedestroy($tmp_img);
}elseif($ratio_w > 1 && $ratio_h > 1){
$dst_x = (int) abs($dst_width - $this->src_width) / 2 ;
$dst_y = (int) abs($dst_height -$this->src_height) / 2;
imagecopy($this->dImage, $this->sImage,$dst_x,$dst_y,0,0,$this->src_width,$this->src_height);
}else{
$src_x=0;$dst_x=0;$src_y=0;$dst_y=0;
if(($dst_width - $this->src_width) < 0){
$src_x = (int) ($this->src_width - $dst_width)/2;
$dst_x =0;
}else{
$src_x =0;
$dst_x = (int) ($dst_width - $this->src_width)/2;
}
if( ($dst_height -$this->src_height) < 0){
$src_y = (int) ($this->src_height - $dst_height)/2;
$dst_y = 0;
}else{
$src_y = 0;
$dst_y = (int) ($dst_height - $this->src_height)/2;
}
imagecopy($this->dImage, $this->sImage,$dst_x,$dst_y,$src_x,$src_y,$this->src_width,$this->src_height);
}
break;
case 3: // keep all image size and create need size
if($ratio_w > 1 && $ratio_h > 1){
$dst_x = (int)(abs($dst_width - $this->src_width )/2) ;
$dst_y = (int)(abs($dst_height- $this->src_height)/2) ;
imagecopy($this->dImage, $this->sImage, $dst_x,$dst_y,0,0,$this->src_width,$this->src_height);
}else{
$ratio = $ratio_w > $ratio_h ? $ratio_h : $ratio_w;
$tmp_w = (int)($this->src_width * $ratio);
$tmp_h = (int)($this->src_height * $ratio);
$tmp_img=imagecreatetruecolor($tmp_w ,$tmp_h);
imagecopyresampled($tmp_img,$this->sImage,0,0,0,0,$tmp_w,$tmp_h,$this->src_width,$this->src_height);
$dst_x = (int)(abs($tmp_w -$dst_width )/2) ;
$dst_y = (int)(abs($tmp_h -$dst_height)/2) ;
imagecopy($this->dImage, $tmp_img, $dst_x,$dst_y,0,0,$tmp_w,$tmp_h);
imagedestroy($tmp_img);
}
break;
case 4: // keep all image but create actually size
if($ratio_w > 1 && $ratio_h > 1){
$this->dImage = imagecreatetruecolor($this->src_width,$this->src_height);
imagecopy($this->dImage, $this->sImage,0,0,0,0,$this->src_width,$this->src_height);
}else{
$ratio = $ratio_w > $ratio_h ? $ratio_h : $ratio_w;
$tmp_w = (int)($this->src_width * $ratio);
$tmp_h = (int)($this->src_height * $ratio);
$this->dImage = imagecreatetruecolor($tmp_w ,$tmp_h);
imagecopyresampled($this->dImage,$this->sImage,0,0,0,0,$tmp_w,$tmp_h,$this->src_width,$this->src_height);
}
break;
}
}// end Crop
/**
*
* 裁切方法
* 2016-01-07 15:05:44
* Lixiaoyu
*
* @param $dst_width 目标长
* @param $dst_height 目标高
* @param $dst_x 裁剪部分和原图左侧的距离
* @param $dst_y 裁剪部分和原图右侧的距离
* @param int $mode 模式
* @param string $dst_file 目标文件路径
*/
function Cut($dst_width,$dst_height,$dst_x,$dst_y,$dst_file='')
{
if ($dst_file) $this->dst_file = $dst_file; //设置目标文件位置
$this->dImage = imagecreatetruecolor($dst_width, $dst_height); //创建了目标文件的大小的画布
$bg = imagecolorallocatealpha($this->dImage, 255, 255, 255, 127); //给画布分配颜色
imagefill($this->dImage, 0, 0, $bg); //给图像用颜色进行填充
imagecolortransparent($this->dImage, $bg); //背景定义成透明色
$ratio_w = 1.0 * $dst_width / $this->src_width; //横向缩放的比例
$ratio_h = 1.0 * $dst_height / $this->src_height; //纵向缩放的比例
//var_dump($this);
//不进行缩放,直接对图像进行裁剪
$ratio = 1.0;
$tmp_w = (int)($dst_width / $ratio);
$tmp_h = (int)($dst_height / $ratio);
$tmp_img = imagecreatetruecolor($dst_width, $dst_height); //创建暂时保存的画布
imagecopy($tmp_img, $this->sImage, 0,0,$dst_x,$dst_y,$dst_width,$dst_height); //拷贝出图像的一部分,进行裁切
imagecopyresampled($this->dImage,$tmp_img,0,0,0,0,$dst_width,$dst_height,$tmp_w,$tmp_h); //把暂时缓存的图片,放到目标文件里面
imagedestroy($tmp_img);
}
}
?>

Use

裁剪图像

$ic=new ImageCrop($pathToFile,'./pic/afterCrop'.time().'.jpg');
$ic->Cut(40,30,120,130);
$ic->SaveImage();
//$ic->SaveAlpha();将补白变成透明像素保存
$ic->destory();

实现效果

原图

裁剪之后的图

缩放图像

原图

缩略图

本文重点在于使用图像处理函数 imagecopy 和 imagecopyresampled

bool imagecopy ( resource dstim,resourcesrc_im , int dstx,intdst_y , int srcx,intsrc_y , int srcw,intsrc_h )
将 src_im 图像中坐标从 src_x,src_y 开始,宽度为 src_w,高度为 src_h 的一部分拷贝到 dst_im 图像中坐标为 dst_x 和 dst_y 的位置上。

(0)

相关推荐

  • PHP加Nginx实现动态裁剪图片方案

    许久以前写过一篇也是关于高性能PHP图片动态裁剪方案的文章,那文章使用的是nginx Cache和rewrite实现的,当然再加上CDN,那个方案存在一个问题就是图片并没有实际生成,而是以二进制的形式存在缓存中.如果缓存失效了那么还需要请求php再次生成.如果说到区别这是我暂且认为的吧.利用空余时间,新增了静态生成图片支持,支持对图片3种模式切换,在门户网站自动对图片尺寸进行裁剪,减少服务器带宽,理论上应该也满足了业务的需求吧,图片裁剪使用了Imagick组件. 一.思路再现:1.先写好请求服务

  • PHP实现的自定义图像居中裁剪函数示例【测试可用】

    本文实例讲述了PHP实现的自定义图像居中裁剪函数.分享给大家供大家参考,具体如下: 图像居中裁减的大致思路: 1.首先将图像进行缩放,使得缩放后的图像能够恰好覆盖裁减区域.(imagecopyresampled - 重采样拷贝部分图像并调整大小) 2.将缩放后的图像放置在裁减区域中间.(imagecopy - 拷贝图像的一部分) 3.裁减图像并保存.(imagejpeg | imagepng | imagegif - 输出图象到浏览器或文件) 具体代码: //==================

  • thinkPHP框架实现图像裁剪、缩放、加水印的方法

    本文实例讲述了thinkPHP框架实现图像裁剪.缩放.加水印的方法.分享给大家供大家参考,具体如下: ThinkPHP 图片处理函数,需要文字水印字体,可在windows下 控制面板 > 大图标(右上角) > 字体 找到需要的字体 /** * 图像的裁剪.缩放.加水印 * @param string $path 路径 * @param int $width 裁剪的宽度/限制的高度或宽度,当有$height值时此值为图片的宽度,否则为限制的宽度或高度 * @param int $height [

  • php结合imgareaselect实现图片裁剪

    引用CSS /js/jquery.imgareaselect-0.9.10/css/imgareaselect-default.css 引用js /js/jquery.imgareaselect-0.9.10/scripts/jquery.imgareaselect.min.js /js/AjaxFileUploaderV2.1/ajaxfileupload.js html <div> <img src='blank.jpg' id="mainimg"> <

  • JQuery PHP图片在线裁剪实例

    / * Goofy 2011-11-29 * 图像处理:根据传递过来的坐标参数,x,y,w,h,依次为选取的x坐标,y坐标,w宽度,h高度 通过imagecopy()方法将该区域copy至第一步创建的空白图像中 注意,在创建图像的时候要用imagecreatetruecolor()真彩色,不然用imagecreate()图片会失真 */ 自由图片剪切 无比例 <?php /** * Goofy 2011-11-29 * 图像处理:根据传递过来的坐标参数,x,y,w,h,依次为选取的x坐标,y坐标

  • 使用gd库实现php服务端图片裁剪和生成缩略图功能分享

    裁剪示例: 最终裁剪成的图片: 其中虚线框内就是要裁剪出来的图片,最终保存成100宽的图片.代码如下: 复制代码 代码如下: $src_path = '1.jpg';//创建源图的实例$src = imagecreatefromstring(file_get_contents($src_path)); //裁剪开区域左上角的点的坐标$x = 100;$y = 12;//裁剪区域的宽和高$width = 200;$height = 200;//最终保存成图片的宽和高,和源要等比例,否则会变形$fi

  • php使用imagick模块实现图片缩放、裁剪、压缩示例

    PHP 使用Imagick模块 缩放,裁剪,压缩图片 包括gif图片 缩放 裁剪 复制代码 代码如下: /**  * 图片裁剪  * 裁剪规则:  *   1. 高度为空或为零   按宽度缩放 高度自适应  *   2. 宽度为空或为零  按高度缩放 宽度自适应  *      3. 宽度,高度到不为空或为零  按宽高比例等比例缩放裁剪  默认从头部居中裁剪  * @param number $width  * @param number $height  */ public function

  • PHP 裁剪图片成固定大小代码方法

    做一个首页调用图像,有时候往往需要获得固定大小的图像,因为首页的图像位置通常由设计人员指定好了,如果是做最新发布图像调用,因为不知道客户会上传什么比例的图像,所以,有时候也就没有办法确定图像的比例,前台页面编写人员通常会采用固定 img 元素高宽的办法来达到控制图像不溢出,但如果图像的比例不是需要的比例,就会造成图像调用后变形,很大程度上影响了页面的美观,有解决的方法是,按照原图比例进行缩放,缩放后的图像难免会有空白,空白处填以颜色,这样虽然图像不变形了,但这样会有很多问题,比如,如果用户发一个

  • PHP图片处理之使用imagecopyresampled函数裁剪图片例子

    图片裁剪是指在一个大的背景图片中裁剪出一张指定区域的图片,常见的应用是在用户设置个人头像时,可以从上传的图片中,裁剪出一个合适的区域作为自己的个人头像图片.图像裁剪和图片缩放的相似,所以也是借助imagecopyresampled()函数去实现这个功能.同样也是以JPEG图片格式为例,声明一个图像裁剪函数cut(),代码如下所示: 复制代码 代码如下: <?php     //在一个大的背景图片中裁剪出指定区域的图片,以jpeg图片格式为例     function cut($filename,

  • PHP图片裁剪函数(保持图像不变形)

    为了完成图片上传之后自动的裁剪,然后在前台显示出裁剪出的图片.需求如上,源码如下: 复制代码 代码如下: <? *exif_imagetype -- 判断一个图像的类型 *说明:函数功能是把一个图像裁剪为任意大小的图像,图像不变形 * 参数说明:输入 需要处理图片的 文件名,生成新图片的保存文件名,生成新图片的宽,生成新图片的高 */ // 获得任意大小图像,不足地方拉伸,不产生变形,不留下空白         function my_image_resize($src_file, $dst_f

  • PHP实现图片裁剪、添加水印效果代码

    3.PHP对图像的裁剪 <div> <h4>裁剪之前</h4> <img src="1.png" style="border:1px solid red;"> </div> <?php header("content-type","text/html;charset=utf-8"); /* *图片裁剪 *@param string $filename 图片的ur

  • php+js实现图片的上传、裁剪、预览、提交示例

    首先用到的语言是php.插件imgareaselect(下载地址),没有太多花哨的样式,index.php代码如下: 复制代码 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.or

随机推荐