如何使用 PHP 从底部剪切图像?

发布于 2024-08-22 08:41:28 字数 101 浏览 5 评论 0原文

我想去掉图像底部的文字。我怎样才能从底部切割它......比如说从底部切割10个像素。

我想用 PHP 来做这个。我有很多底部有文字的图像。

有办法做到吗?

I want to take out the text in the bottom of an image. How can I cut it from bottom ...say 10 pixels to cut from bottom.

I want do this in PHP. I have lots of images that have text in the bottom.

Is there a way to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

﹉夏雨初晴づ 2024-08-29 08:41:28

干得好。

要更改图像的名称,请更改 $in_filename(当前为“source.jpg”)。您也可以在其中使用 URL,尽管显然这会表现得更差。

更改 $new_height 变量以设置要裁剪的底部数量。

尝试一下 $offset_x、$offset_y、$new_width 和 $new_height,你就会明白的。

请让我知道它有效。 :)

希望有帮助!

<?php

$in_filename = 'source.jpg';

list($width, $height) = getimagesize($in_filename);

$offset_x = 0;
$offset_y = 0;

$new_height = $height - 15;
$new_width = $width;

$image = imagecreatefromjpeg($in_filename);
$new_image = imagecreatetruecolor($new_width, $new_height);
imagecopy($new_image, $image, 0, 0, $offset_x, $offset_y, $width, $height);

header('Content-Type: image/jpeg');
imagejpeg($new_image);

?>

Here you go.

To change the name of the image, change $in_filename (currently 'source.jpg'). You can use URLs in there as well, although obviously that will perform worse.

Change the $new_height variable to set how much of the bottom you want cropped.

Play around with $offset_x, $offset_y, $new_width and $new_height, and you'll figure it out.

Please let me know that it works. :)

Hope it helps!

<?php

$in_filename = 'source.jpg';

list($width, $height) = getimagesize($in_filename);

$offset_x = 0;
$offset_y = 0;

$new_height = $height - 15;
$new_width = $width;

$image = imagecreatefromjpeg($in_filename);
$new_image = imagecreatetruecolor($new_width, $new_height);
imagecopy($new_image, $image, 0, 0, $offset_x, $offset_y, $width, $height);

header('Content-Type: image/jpeg');
imagejpeg($new_image);

?>
痴意少年 2024-08-29 08:41:28

您可以使用 GD 图像库 在 PHP 中操作图像。您正在寻找的函数是imagecopy(),它将图像的一部分复制到另一个图像上。下面是来自 PHP.net 的示例,大致按照您的描述进行操作:

<?php

$width = 50;
$height = 50;

$source_x = 0;
$source_y = 0;

// Create images
$source = imagecreatefromjpeg('source.jpg');
$new = imagecreatetruecolor($width, $height);

// Copy
imagecopy($source, $new, 0, 0, $source_x, $source_y, $width, $height);

// Output image
header('Content-Type: image/jpeg');
imagejpeg($new);

?>

要裁剪源图像,请根据您的喜好更改 $source_x$source_y 变量。

You may use the GD Image Library to manipulate images in PHP. The function you're looking for is imagecopy(), which copies part of an image onto another. Here's an example from PHP.net that does roughly what you describe:

<?php

$width = 50;
$height = 50;

$source_x = 0;
$source_y = 0;

// Create images
$source = imagecreatefromjpeg('source.jpg');
$new = imagecreatetruecolor($width, $height);

// Copy
imagecopy($source, $new, 0, 0, $source_x, $source_y, $width, $height);

// Output image
header('Content-Type: image/jpeg');
imagejpeg($new);

?>

To crop the source image, change the $source_x and $source_y variables to your liking.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文