裁剪图像,然后将其保存在同一目录中
Crop an image and then save it in the same directory
我想将 100x100px 的图像的一部分从中间裁剪到 20px 高度和 30px 宽度,然后将其保存在另一个文件中,全部使用 PHP。
我正在阅读和测试一些代码,但我想我迷路了。
我想这样做是因为稍后我想使用 OCR 从裁剪后的新 img 中获取文本。
任何帮助都会很棒!
这是我在 php.net
的文档中找到的一些代码
<?php
// Create image instances
$src = imagecreatefrompng('waka.png');
$dest = "Select somehow /images ";
// Copy
imagecopy($dest, $src, 0, 0, 20, 13, 80, 40);
// Output and free from memory
header('Content-Type: image/png');
imagepng($dest);
imagedestroy($dest);
imagedestroy($src);
?>
当我 运行 在我的本地服务器上显示 img 时,我刚收到一些错误消息。
我不太确定我需要更改什么才能获得新的 png。实际上我有 2 个 img waka.png 和 wuku.png 用于测试。
从你的代码开始,这个对我有用:
<?php
// load your source image
$src = imagecreatefrompng('1.png');
// create an image resource of your expected size 30x20
$dest = imagecreatetruecolor(30, 20);
// Copy the image
imagecopy(
$dest,
$src,
0, // 0x of your destination
0, // 0y of your destination
50, // middle x of your source
50, // middle y of your source
30, // 30px of width
20 // 20px of height
);
// The second parameter should be the path of your destination
imagepng($dest, '2.png');
imagedestroy($dest);
imagedestroy($src);
你应该 2.png 是你的裁剪图像。
我想将 100x100px 的图像的一部分从中间裁剪到 20px 高度和 30px 宽度,然后将其保存在另一个文件中,全部使用 PHP。
我正在阅读和测试一些代码,但我想我迷路了。
我想这样做是因为稍后我想使用 OCR 从裁剪后的新 img 中获取文本。
任何帮助都会很棒!
这是我在 php.net
的文档中找到的一些代码<?php
// Create image instances
$src = imagecreatefrompng('waka.png');
$dest = "Select somehow /images ";
// Copy
imagecopy($dest, $src, 0, 0, 20, 13, 80, 40);
// Output and free from memory
header('Content-Type: image/png');
imagepng($dest);
imagedestroy($dest);
imagedestroy($src);
?>
当我 运行 在我的本地服务器上显示 img 时,我刚收到一些错误消息。
我不太确定我需要更改什么才能获得新的 png。实际上我有 2 个 img waka.png 和 wuku.png 用于测试。
从你的代码开始,这个对我有用:
<?php
// load your source image
$src = imagecreatefrompng('1.png');
// create an image resource of your expected size 30x20
$dest = imagecreatetruecolor(30, 20);
// Copy the image
imagecopy(
$dest,
$src,
0, // 0x of your destination
0, // 0y of your destination
50, // middle x of your source
50, // middle y of your source
30, // 30px of width
20 // 20px of height
);
// The second parameter should be the path of your destination
imagepng($dest, '2.png');
imagedestroy($dest);
imagedestroy($src);
你应该 2.png 是你的裁剪图像。