【问题标题】:Add padding to image GD为图像 GD 添加填充
【发布时间】:2015-11-20 19:47:02
【问题描述】:

所以我正在制作黑色背景的图像。我的任务是在内部图像周围添加白色边框,边框和图像之间有 2px 填充。

  1. 上传图片
  2. 然后我在它周围创建白色边框。
  3. 然后我创建黑色背景(图片),上传图片的宽度和高度更大。
  4. 最后,我将带边框的原始图像添加到创建的黑色图像的中心。

所以我被困在第二点,即创建白色边框的地方。我的所有代码如下所示:

public function output()
{
    $this->__set_image_size();

    # setting temp image location
    $this->settings['__temp_image'] = $this->settings['__upload_dir']['temp'] . $this->temp_image_name . '.jpg';

    # generating image
    $this->__generate_background();
    $this->__load_image();
    $this->__draw_white_border();
    $this->__insert_image_to_wrapper();

    # destroy temp image data
    //imagedestroy($this->settings['__temp_image']);

    return $this->settings;
}

和函数__draw_white_border:

private function __draw_white_border()
{
    # draw white border
    $color_white = ImageColorAllocate($this->image_inside, 255, 255, 255);
    $this->__draw_border($this->image_inside, $color_white, 4);
}

private function __draw_border(&$img, &$color, $thickness)
{
    $x = ImageSX($img);
    $y = ImageSY($img);
    for ( $i = 0; $i < $thickness; $i ++ )
        ImageRectangle($img, $i, $i, $x --, $y --, $color);
}

主要问题是:如何在列表第二点的边框和图像之间添加填充或如何制作2px黑色和4px白色的渐变边框?

【问题讨论】:

  • 仍在寻找想法
  • 您是想在不覆盖任何图像的情况下添加边框(因此增加画布大小),还是用边框覆盖图像边缘(因此保持画布大小相同)?
  • 我想在图像周围添加边框,所以不覆盖图像

标签: php image border gd padding


【解决方案1】:

这是一个非常简单的示例,您应该能够适应您的需求:

<?php

// get source image and dimensions.
$src = imagecreatefromstring(file_get_contents('path/to/image'));
$src_w = imagesx($src);
$src_h = imagesy($src);

// create destination image with dimensions increased from $src for borders.
$dest_w = $src_w + 12;
$dest_h = $src_h + 12;
$dest = imagecreatetruecolor($dest_w, $dest_h);

// draw white border (no need for black since new images default to that).
imagerectangle($dest, 1, 1, $dest_w - 2, $dest_h - 2, 0x00ffffff);
imagerectangle($dest, 0, 0, $dest_w - 1, $dest_h - 1, 0x00ffffff);

// copy source image into destination image.
imagecopy($dest, $src, 6, 6, 0, 0, $src_w, $src_h);

// output.
header('Content-type: image/png');
imagepng($dest);
imagedestroy($src);
imagedestroy($dest);
exit;

输入:

结果(请注意,由于页面背景为白色,白色边框不明显):

如果您想将白色边框作为内边框,只需更改您绘制的坐标:

imagerectangle($dest, 5, 5, $dest_w - 6, $dest_h - 6, 0x00ffffff);
imagerectangle($dest, 4, 4, $dest_w - 5, $dest_h - 5, 0x00ffffff);

结果:

【讨论】:

  • 但我想问你 - 你从源代码创建图像。但是如果我之前已经将它加载到$this-&gt;image_inside,如何开始呢?我要加载的代码:code example 我想从这张图片开始。最后将结果保存到$this-&gt;image_inside
  • $this-&gt;image_inside 是一个 GD 资源,因此在我的示例中,您可以简单地将 $src 替换为 $this-&gt;image_inside。要将结果保存到$this-&gt;image_inside,只需将其指向相同的最终图像资源:$this-&gt;image_inside = $dest;
  • 再次感谢 :) 我看到你对 GD 很好,我还有一份更容易的工作,这对我来说太难了。也许你想接受或帮我做?只要邀请我聊天或其他什么,我会展示它。很久以前我就在找这个工作的开发人员,但是没有人有足够的经验来处理它
  • 如果您有需要帮助的特定问题,请将其作为另一个问题发布。我很有可能会看到它(甚至可能会提供帮助),否则 SO 蜂巢思维的另一个节点可能很快就会提供帮助。
  • 是的,我有具体问题and this is the question
猜你喜欢
  • 2012-05-22
  • 1970-01-01
  • 2011-09-21
  • 2017-12-25
  • 2017-03-20
  • 2021-12-11
  • 2014-05-24
  • 2016-07-02
  • 1970-01-01
相关资源
最近更新 更多