当前位置: 首页 > 编程文章 >

PHP全功能无变形图片裁剪操作类与用法示例

PHP全功能无变形图片裁剪操作类与用法示例

作者:叶知时间:2025-11-01 09:59:55浏览:379

本文介绍一个支持多种模式的PHP图片处理类,可实现任意尺寸裁剪、仅缩小不放大、保留全部信息补白缩放以及精确像素缩放。该类确保图片在变换过程中保持比例不变形,适用于生成高质量缩略图或定制尺寸图像,并提供相关在线工具推荐。

本文目录展开

PHP无变形图片裁剪类概述

本代码示例展示了一个具备完整功能的PHP图片裁剪类及其使用方式,供开发者参考借鉴。

该工具能够胜任常规的图片裁剪及缩略图生成任务,涵盖了所有核心需求,且保证在处理过程中图像不会发生变形。

具体实现逻辑被划分为以下四种不同的处理模式:

1. 将任意图片裁剪至指定尺寸,若原图过大则居中裁剪并最大化缩放,若过小则进行拉伸填充; 2. 对超出部分进行居中裁剪,不足部分不进行拉伸,即仅执行缩小操作而不放大,此模式可能产生空白区域,可利用PNG透明背景进行遮盖; 3. 完整保留原图所有信息,不进行裁剪,仅执行缩放操作,对于不足目标尺寸的区域进行补白处理; 4. 同样保留全部信息且不裁剪,仅执行缩放,但不足部分不补白,生成的图片尺寸等于实际有效像素大小。例如将600x600的图片缩放至适配120x100的容器,最终生成100x100的有效图片,而上述第3种模式则会生成120x100并带有补白。

具体实现代码如下(此处代码已通过本站在线工具点击使用进行了格式化处理):


<?php
/**
* Author : smallchicken
* mode 1 : 强制裁剪,生成图片严格按照需要,不足放大,超过裁剪,图片始终铺满
* mode 2 : 和1类似,但不足的时候 不放大 会产生补白,可以用png消除。
* mode 3 : 只缩放,不裁剪,保留全部图片信息,会产生补白,
* mode 4 : 只缩放,不裁剪,保留全部图片信息,生成图片大小为最终缩放后的图片有效信息的实际大小,不产生补白
* 默认补白为白色,如果要使补白成透明像素,请使用SaveAlpha()方法代替SaveImage()方法
*
* 调用方法:
*
* $ic=new ImageCrop('old.jpg','afterCrop.jpg');
* $ic->Crop(120,80,2);
* $ic->SaveImage();
*    //$ic->SaveAlpha();将补白变成透明像素保存
* $ic->destory();
*
*
*/
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 ImageCrop($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
}
?>

此外,还推荐以下几款实用的图片处理辅助工具供选用:

在线图片转换BASE64工具: 点击使用

ICO图标在线生成工具: 点击使用

在线Email邮箱图标制作工具: 点击使用

在线图片格式转换(jpg/bmp/gif/png)工具: 点击使用

希望上述内容能对您的PHP编程工作提供有益的帮助。

上一篇
PHP实现十进制与36进制互转代码示例

本文提供PHP语言中实现十进制与36进制相互转换的完整代码示例。通过自定义函数分别处理数值到字符映射及字符到数值累加,支持0-9和A-Z共36个字符集,并附带在线工具推荐供开发者参考使用。

下一篇
PHP解决strrev函数翻转中文乱码的方法

在使用PHP内置的strrev函数直接翻转包含中文字符的字符串时,由于UTF-8编码下中文字符由多字节组成,会导致输出出现乱码。本文通过重写一个名为cnstrrev的自定义函数来解决此问题,该函数基于字符ASCII码判断是否为中文,并针对性地处理多字节截断与数组反转,从而正确实现中英文混合字符串的安全翻转。

相关文章

更多
基于PHP封装图片裁剪工具类

本文介绍如何基于PHP语言封装一个实用的图片裁剪工具类。通过面向对象的设计思路,该类能够简化图片处理流程,提供便捷的接口以实现指定尺寸的图片截取功能,帮助开发者高效完成图像编辑任务。

2026.11.14 473
PHP按比例截取图片的实现逻辑与工具推荐

本文介绍了在PHP中利用GD库实现图片按指定比例截取的核心方法,详细解析了宽高比判断与坐标计算逻辑。同时提供了多种实用的在线图片处理工具链接,涵盖格式转换、图标生成及Base64编码等功能,辅助开发者高效完成图像预处理任务。

2025.09.08 959
PHP无损图片裁剪与缩放函数实现

本文提供了一段PHP代码,用于将图片裁剪并缩放到指定尺寸,同时保持图像不变形且不留空白。该功能通过判断图像类型并调用相应的GD库函数实现,适用于需要统一图片尺寸的Web开发场景。

2025.06.01 728
PHP实现图片裁剪与添加水印功能

本文介绍利用PHP进行图片裁剪和添加水印的具体实现方法。通过相关函数,可以将原始图片按照指定尺寸进行裁剪处理,并在图像上叠加文字或图形水印,以满足内容保护或品牌展示的需求。

2026.09.28 124
PHP时间戳与日期字符串相互转换方法详解

本文介绍PHP中时间数值(Unix时间戳)与日期字符串之间的相互转换方法。通过strtotime和date函数,实现将当前时间、指定日期转换为时间戳,以及将时间戳格式化为可读的日期时间字符串,并提供相关在线计算工具推荐。

2025.11.30 1,031
PHP操作MySQL数据库:从工具选择到实战案例

本文介绍了使用PHP操作MySQL数据库的完整流程,涵盖命令行、Navicat及phpMyAdmin等常用管理工具。重点讲解HTTP GET与POST方式在表单数据传递中的区别与安全特性。通过mysqli扩展的基础步骤(创建连接、选择库、设置编码、执行SQL、关闭连接),演示了增删改查的具体实现,并对比了fetch_assoc、fetch_row和fetch_array三种结果集获取方法的差异。最后结合注册与登录两个实际案例,展示如何将前端用户输入安全地存入数据库并进行验证。

2026.01.07 1,004

专题推荐

更多
php图片裁剪

先确认目标尺寸和希望保留的画面区域。比例不同的图可先在裁剪工具里确认构图,再读等比截取或缩放案例。业务还需要水印时,参考组合示例检查裁剪和添加水印的顺序。

PHP 类与对象定义及继承

先看类声明、对象实例与属性访问。理解对象的创建和销毁后,再读继承、方法重载和关键字对照。已有代码遇到继承行为差异时,可进一步查看 final 方法与反射实例。

PHP 定时任务配置与运行

先确定任务运行间隔以及是否允许重叠。固定时间触发先看 Cron 案例,框架内和常驻服务再读对应示例。

Yii2 队列配置与常驻运行

先用一个最小任务跑通入队和消费,再检查 Web 与 Console 是否加载了相同的队列配置。确认命令能手动运行后再配置 Supervisor,最后验证进程重启时的处理情况。

PHP Session 过期时间设置方法

浏览器关闭、服务端回收和业务规定的闲置超时,可能在不同时间发生。对照配置项与应用层时间戳的做法,检查会话何时仍可读、何时应要求重新登录。

在线工具箱

更多
图片裁剪
图片裁剪

从图片中截取需要的区域,重新安排主体位置与画面比例。

键码查询
键码查询

按下键盘按键时查看浏览器记录的键名和事件信息。

.env 配置检查
.env 配置检查

本地检查 .env 键值语法、引号、重复键及空值,预览解析结果,辅助排查 PHP 和 Laravel 环境配置。

进制转换
进制转换

转换二进制、十进制、十六进制等数值表示,辅助程序调试与进制学习。

HTML 标签安全清理
HTML 标签安全清理

清理 HTML 片段中的标签或危险内容,辅助整理待展示的文本。