当前位置:首页 > PHP

php 实现切图

2026-02-16 00:36:13PHP

使用 PHP 实现图片切割

GD 库基础切割方法

安装 GD 库后可以通过 imagecrop() 函数实现基础切割

$srcImage = imagecreatefromjpeg('input.jpg');
$croppedImage = imagecrop($srcImage, ['x' => 100, 'y' => 50, 'width' => 300, 'height' => 200]);
imagejpeg($croppedImage, 'output.jpg');
imagedestroy($srcImage);
imagedestroy($croppedImage);

等比例分割多张图片

将大图分割为指定数量的小图

function splitImage($srcPath, $rows, $cols, $outputPrefix) {
    $image = imagecreatefromjpeg($srcPath);
    $width = imagesx($image);
    $height = imagesy($image);

    $tileWidth = $width / $cols;
    $tileHeight = $height / $rows;

    for ($row = 0; $row < $rows; $row++) {
        for ($col = 0; $col < $cols; $col++) {
            $tile = imagecrop($image, [
                'x' => $col * $tileWidth,
                'y' => $row * $tileHeight,
                'width' => $tileWidth,
                'height' => $tileHeight
            ]);
            imagejpeg($tile, "{$outputPrefix}_{$row}_{$col}.jpg");
            imagedestroy($tile);
        }
    }
    imagedestroy($image);
}

保持宽高比的智能裁剪

根据目标尺寸自动计算最佳裁剪区域

function smartCrop($srcPath, $targetWidth, $targetHeight) {
    $src = imagecreatefromjpeg($srcPath);
    $srcWidth = imagesx($src);
    $srcHeight = imagesy($src);

    $srcRatio = $srcWidth / $srcHeight;
    $targetRatio = $targetWidth / $targetHeight;

    if ($srcRatio > $targetRatio) {
        $cropWidth = $srcHeight * $targetRatio;
        $cropHeight = $srcHeight;
        $x = ($srcWidth - $cropWidth) / 2;
        $y = 0;
    } else {
        $cropWidth = $srcWidth;
        $cropHeight = $srcWidth / $targetRatio;
        $x = 0;
        $y = ($srcHeight - $cropHeight) / 2;
    }

    $cropped = imagecrop($src, [
        'x' => $x,
        'y' => $y,
        'width' => $cropWidth,
        'height' => $cropHeight
    ]);

    $resized = imagescale($cropped, $targetWidth, $targetHeight);
    imagejpeg($resized, 'smart_crop_output.jpg');

    imagedestroy($src);
    imagedestroy($cropped);
    imagedestroy($resized);
}

使用 Imagick 扩展处理

Imagick 提供更强大的图像处理能力

$imagick = new Imagick('input.jpg');
$imagick->cropImage(200, 150, 50, 30); // 宽度,高度,X起点,Y起点
$imagick->writeImage('output_crop.jpg');
$imagick->clear();

批量处理目录图片

遍历目录处理所有图片文件

php 实现切图

$directory = 'images/';
$files = scandir($directory);

foreach ($files as $file) {
    if (in_array(pathinfo($file, PATHINFO_EXTENSION), ['jpg', 'png'])) {
        $srcPath = $directory . $file;
        $outputPath = $directory . 'cropped_' . $file;

        $image = imagecreatefromjpeg($srcPath);
        $cropped = imagecrop($image, ['x' => 0, 'y' => 0, 'width' => 500, 'height' => 500]);

        if ($cropped !== false) {
            imagejpeg($cropped, $outputPath);
            imagedestroy($cropped);
        }

        imagedestroy($image);
    }
}

标签: php
分享给朋友:

相关文章

php实现打印功能

php实现打印功能

PHP实现打印功能的方法 在PHP中实现打印功能可以通过多种方式完成,包括直接输出到浏览器、生成PDF文件、调用打印机接口等。以下是几种常见的方法: 直接输出HTML内容 PHP可以通过echo或p…

php实现分页

php实现分页

PHP实现分页的基本方法 使用原生PHP实现分页功能需要结合数据库查询和HTML展示。以下是核心实现步骤: 数据库查询与分页逻辑 // 连接数据库 $conn = new mysqli('loca…

php 实现登录

php 实现登录

PHP 登录功能实现 使用 PHP 实现登录功能通常需要结合数据库验证和会话管理。以下是一个基本的登录实现流程: 数据库准备 创建用户表存储用户名和密码(建议使用哈希加密): CREATE TAB…

php实现一个

php实现一个

PHP 实现一个简单的计算器 创建一个基本的计算器,支持加、减、乘、除运算。以下是一个完整的实现代码示例: <?php $result = ""; if ($_SERVER["REQUES…

php 实现无限分类

php 实现无限分类

无限分类的实现方法 无限分类通常用于构建多层级结构的数据,如商品分类、菜单管理等。以下是几种常见的实现方式: 邻接列表模型(Adjacency List) 这是最简单的实现方式,每个分类记录存储其父…

php 实现md5

php 实现md5

使用 PHP 内置函数实现 MD5 PHP 提供了 md5() 函数,可以直接计算字符串的 MD5 哈希值。该函数接受一个字符串参数,返回 32 字符的十六进制哈希值。 $string = "H…