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

naz*_*zir 4 php gd image

我想取出图像底部的文字.如何从底部切割...说从底部切割10个像素.

我想在PHP中这样做.我有很多图片底部有文字.

有办法吗?

Tee*_*kin 19

干得好.

要更改图像的名称,请更改$ 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);

?>
Run Code Online (Sandbox Code Playgroud)


Joh*_*set 6

您可以使用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);

?>
Run Code Online (Sandbox Code Playgroud)

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

  • 你可能会误以为那些将免费开展工作的开发人员. (8认同)