当前位置: 首页 > 面试题库 >

在PHP中从图像裁剪空白

韦睿
2023-03-14
问题内容

是否可以在PHP中删除图像周围的空格?

注意:澄清一下,我的意思是像photoshops装饰功能

谢谢。


问题答案:

要修剪所有空白,就像您所说的那样,围绕图像的有趣部分,首先我们找出“空白”在哪里停止,然后我们复制这些边界内的所有内容。

//load the image
$img = imagecreatefromjpeg("http://ecx.images-amazon.com/images/I/413XvF0yukL._SL500_AA280_.jpg");

//find the size of the borders
$b_top = 0;
$b_btm = 0;
$b_lft = 0;
$b_rt = 0;

//top
for(; $b_top < imagesy($img); ++$b_top) {
  for($x = 0; $x < imagesx($img); ++$x) {
    if(imagecolorat($img, $x, $b_top) != 0xFFFFFF) {
       break 2; //out of the 'top' loop
    }
  }
}

//bottom
for(; $b_btm < imagesy($img); ++$b_btm) {
  for($x = 0; $x < imagesx($img); ++$x) {
    if(imagecolorat($img, $x, imagesy($img) - $b_btm-1) != 0xFFFFFF) {
       break 2; //out of the 'bottom' loop
    }
  }
}

//left
for(; $b_lft < imagesx($img); ++$b_lft) {
  for($y = 0; $y < imagesy($img); ++$y) {
    if(imagecolorat($img, $b_lft, $y) != 0xFFFFFF) {
       break 2; //out of the 'left' loop
    }
  }
}

//right
for(; $b_rt < imagesx($img); ++$b_rt) {
  for($y = 0; $y < imagesy($img); ++$y) {
    if(imagecolorat($img, imagesx($img) - $b_rt-1, $y) != 0xFFFFFF) {
       break 2; //out of the 'right' loop
    }
  }
}

//copy the contents, excluding the border
$newimg = imagecreatetruecolor(
    imagesx($img)-($b_lft+$b_rt), imagesy($img)-($b_top+$b_btm));

imagecopy($newimg, $img, 0, 0, $b_lft, $b_top, imagesx($newimg), imagesy($newimg));

//finally, output the image
header("Content-Type: image/jpeg");
imagejpeg($newimg);

我的旧示例假设图像的所有侧面都具有相同的“边框”,只是为了澄清注释:)

//load the image
$img = imagecreatefromjpeg("img.jpg");

//find the size of the border.
$border = 0;
while(imagecolorat($img, $border, $border) == 0xFFFFFF) {
  $border++;
}

//copy the contents, excluding the border
//This code assumes that the border is the same size on all sides of the image.
$newimg = imagecreatetruecolor(imagesx($img)-($border*2), imagesy($img)-($border*2));
imagecopy($newimg, $img, 0, 0, $border, $border, imagesx($newimg), imagesy($newimg));

//finally, if you want, overwrite the original image
imagejpeg($newimg, "img.jpg");


 类似资料:
  • 问题内容: 下面的代码可以很好地裁剪图像,这是我想要的,但是对于较大的图像,它也可以正常工作。有什么办法可以缩小图像吗? 想法是,在裁剪之前,我将能够使每个图像的大小大致相同,以便每次都能获得良好的效果 代码是 问题答案: 如果要生成缩略图,则必须首先使用调整图像大小。您必须调整图像的大小,以使图像较小侧的尺寸等于拇指的相应侧。 例如,如果源图像为1280x800px,拇指为200x150px,则

  • 我试图在从图库中选择图像后使用intent来裁剪图像。以下是我的代码片段 在这里,我使用PICK_IMAGE_REQUEST意图句柄调用上面的代码段 由于我在裁剪后使用了相同的意图,即PICK_IMAGE_REQUEST,可能会出现什么问题

  • 我需要对matlab中的图像执行以下操作: 加载图像 计算图像的FFT(快速傅立叶变换) 将频率分量移到中心 像follow一样裁剪图像(如果图像分辨率为1000x1000,则图像所需的部分类似于以下坐标:100100800800。这是一个较小的图像。(应用过滤器去除高频的想法) 反向移位 傅里叶逆变换 . . . 我的代码如下所示: 问题是,当我想裁剪图像时,我的功能无法裁剪类型为“复杂双”的图

  • 使用 Illustrator 中的“图像裁剪”功能裁剪链接或嵌入的图像。 裁剪图像 您可以在 Illustrator 中裁剪链接或嵌入的图像。在裁剪时,您可以使用直观的构件控件处理选定的图像。“图像裁剪”功能只对当前选定的图像有效。此外,链接的图像在裁剪后会变为嵌入的图像。 注意: 裁剪掉的图像部分会被丢弃,且无法恢复。 在裁剪图像时,无法变换图像。在选择“裁剪图像”选项后,如果您尝试变换图像,则

  • 问题内容: 我想用PHP裁剪图像并保存文件。我知道您应该使用GD库,但我不确定如何使用。有任何想法吗? 谢谢 问题答案: 您可以用来裁剪图像的必需部分。该命令如下所示: 来自PHP.net的代码- 从源图像中裁剪出一个80x40像素的图像

  • 我将<code>背景 1.back_xml: 2.瓷砖.xml 现在,我将back.xml设置为< code >背景以< code>LinearLayout工作正常。 我需要有一个圆角,以及它的边框。但是我只有圆角的边框,而不是图像,我的代码中有什么问题吗? 这是我的照片: