我有很多(大约 1000 张)图像(打印屏幕),我需要裁剪这些图像(所有裁剪的图像都在完整图像的同一区域中)。
我怎样才能在 php 中做到这一点?或者也许 GIMP 支持一些宏脚本来做到这一点?
提前致谢。
我有很多(大约 1000 张)图像(打印屏幕),我需要裁剪这些图像(所有裁剪的图像都在完整图像的同一区域中)。
我怎样才能在 php 中做到这一点?或者也许 GIMP 支持一些宏脚本来做到这一点?
提前致谢。
除了使用 GD,正如 Greg 和 n1313 建议的那样,您还可以为此使用ImageMagick,例如类似于 Greg 的解决方案,但使用
$original_image = new Imagick($file->getRealPath());
$dest_image = $original_image->clone();
$dest_image->cropImage($width, $height, $x, $y);
$dest_image->writeImage('./resized/' . $file->getFilename());
You might check for exceptions when creating the new image (i.e. image cannot be opened) or returned false on cropImage and writeImage (image cannot be cropped or cannot be written).
您可以使用GD 图像函数在 PHP 中完成。
您的脚本可能看起来像这样(未经测试):
$it = new RecursiveDirectoryIterator('./screenshots');
foreach ($it as $file)
{
if (!preg_match('/\.jpe?g$/i', $file->getFilename()))
continue;
$src = imagecreatefromjpeg($file->getRealPath());
$dest = imagecreatetruecolor(1000, 1000);
imagecopyresampled($desc, $src, 0, 0, X_OFFSET, Y_OFFSET, 1000, 1000, WIDTH, HEIGHT);
imagejpeg($dest, './resized/' . $file->getFilename());
}
要使用 PHP 做到这一点,您需要一个GD 图像处理库。然后你可以使用imagecopy()函数来裁剪你的图像和许多其他的 GD 函数来以你需要的任何方式操作图像。