热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

:本篇文章主要介绍了PHP图像裁剪缩略裁切类源码及使用方法,对于PHP教程有兴趣的同学可以参考一下。
最近在做网页拖拽验证码的开源项目,需要在服务端生成图片对应的可移动的色块,但是网上的资源都是做缩略图,对整个图片进行缩放的,所以自己动手,完成了对图片进行裁剪小块的工具

<&#63;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 &#63; $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 &#63; $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 &#63; $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 &#63; $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 &#63; $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 &#63; $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 &#63; $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);
}
}
&#63;>

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 的位置上。

以上就介绍了PHP图像裁剪缩略裁切类源码及使用方法,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

推荐阅读
  • 如何实现织梦DedeCms全站伪静态
    本文介绍了如何通过修改织梦DedeCms源代码来实现全站伪静态,以提高管理和SEO效果。全站伪静态可以避免重复URL的问题,同时通过使用mod_rewrite伪静态模块和.htaccess正则表达式,可以更好地适应搜索引擎的需求。文章还提到了一些相关的技术和工具,如Ubuntu、qt编程、tomcat端口、爬虫、php request根目录等。 ... [详细]
  • PHP玩家基地系统毕业设计(附源码、运行环境)的用户登录界面、游戏管理和玩家作品管理
    本文介绍了一个PHP玩家基地系统的毕业设计,包括用户登录界面、游戏管理和玩家作品管理等功能。附带源码和运行环境,并提供免费赠送本源代码和数据库的方式,请私信获取详细信息。摘要共计约XXX字。 ... [详细]
  • 本文描述了作者第一次参加比赛的经历和感受。作者是小学六年级时参加比赛的唯一选手,感到有些紧张。在比赛期间,作者与学长学姐一起用餐,在比赛题目中遇到了一些困难,但最终成功解决。作者还尝试了一款游戏,在回程的路上感到晕车。最终,作者以110分的成绩取得了省一会的资格,并坚定了继续学习的决心。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 基于layUI的图片上传前预览功能的2种实现方式
    本文介绍了基于layUI的图片上传前预览功能的两种实现方式:一种是使用blob+FileReader,另一种是使用layUI自带的参数。通过选择文件后点击文件名,在页面中间弹窗内预览图片。其中,layUI自带的参数实现了图片预览功能。该功能依赖于layUI的上传模块,并使用了blob和FileReader来读取本地文件并获取图像的base64编码。点击文件名时会执行See()函数。摘要长度为169字。 ... [详细]
  • 搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的详细步骤
    本文详细介绍了搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的步骤,包括环境说明、相关软件下载的地址以及所需的插件下载地址。 ... [详细]
  • PHP图片截取方法及应用实例
    本文介绍了使用PHP动态切割JPEG图片的方法,并提供了应用实例,包括截取视频图、提取文章内容中的图片地址、裁切图片等问题。详细介绍了相关的PHP函数和参数的使用,以及图片切割的具体步骤。同时,还提供了一些注意事项和优化建议。通过本文的学习,读者可以掌握PHP图片截取的技巧,实现自己的需求。 ... [详细]
  • 关羽败走麦城时路过马超封地 马超为何没有出手救人
    对当年关羽败走麦城,恰好路过马超的封地,为啥马超不救他?很感兴趣的小伙伴们,趣历史小编带来详细的文章供大家参考。说到英雄好汉,便要提到一本名著了,没错,那就是《三国演义》。书中虽 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • PHP设置MySQL字符集的方法及使用mysqli_set_charset函数
    本文介绍了PHP设置MySQL字符集的方法,详细介绍了使用mysqli_set_charset函数来规定与数据库服务器进行数据传送时要使用的字符集。通过示例代码演示了如何设置默认客户端字符集。 ... [详细]
  • Java序列化对象传给PHP的方法及原理解析
    本文介绍了Java序列化对象传给PHP的方法及原理,包括Java对象传递的方式、序列化的方式、PHP中的序列化用法介绍、Java是否能反序列化PHP的数据、Java序列化的原理以及解决Java序列化中的问题。同时还解释了序列化的概念和作用,以及代码执行序列化所需要的权限。最后指出,序列化会将对象实例的所有字段都进行序列化,使得数据能够被表示为实例的序列化数据,但只有能够解释该格式的代码才能够确定数据的内容。 ... [详细]
  • 橱窗设计的表现手法及其应用
    本文介绍了橱窗设计的表现手法,包括直接展示、寓意与联想、夸张与幽默等。通过对商品的折、拉、叠、挂、堆等陈列技巧,橱窗设计能够充分展现商品的形态、质地、色彩、样式等特性。同时,寓意与联想可以通过象形形式或抽象几何道具来唤起消费者的联想与共鸣,创造出强烈的时代气息和视觉空间。合理的夸张和贴切的幽默能够明显夸大商品的美的因素,给人以新颖奇特的心理感受,引起人们的笑声和思考。通过这些表现手法,橱窗设计能够有效地传达商品的个性内涵,吸引消费者的注意力。 ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • faceu激萌变老特效的使用方法详解
    本文介绍了faceu激萌变老特效的使用方法,包括打开faceu激萌app、点击贴纸、选择热门贴纸中的变老特效,然后对准人脸进行拍摄,即可给照片添加变老特效。操作简单,适合新用户使用。 ... [详细]
author-avatar
点燃半吱煙
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有