imageaffine
使用可选的剪切区域返回包含仿射变换的src图像的图像
PHP 5.5.0 及以上版本。
imageaffine() 函数用于应用仿射变换到给定的图像资源。仿射变换是一种线性变换,通过矩阵操作对图像进行旋转、缩放、平移等变换。
imageaffine(resource $image, array $affine, int $color = 0): bool
成功时返回 true,失败时返回 false。
以下是使用 imageaffine() 函数进行仿射变换的示例:
这个例子展示了如何使用 imageaffine() 将一个图像旋转 45 度并进行缩放。
<?php // 创建一个图像资源 $image = imagecreatefromjpeg('path_to_image.jpg'); // 定义仿射变换矩阵 $affine = [ 'a' => 0.707, // 缩放和旋转因子 'b' => 0.707, 'c' => -0.707, 'd' => 0.707, 'e' => 0, 'f' => 0 ]; // 应用仿射变换 $result = imageaffine($image, $affine); // 检查是否成功 if ($result) { echo "仿射变换成功!"; } else { echo "仿射变换失败!"; } // 输出图像 header('Content-Type: image/jpeg'); imagejpeg($image); imagedestroy($image); ?>
这段代码首先从指定的路径加载一个图像文件,然后定义一个仿射变换矩阵,将图像旋转 45 度并进行缩放。使用 imageaffine() 函数将变换应用到图像,最后输出处理后的图像。