添加填充图像GD
所以我正在制作一个黑色背景的图像。我的任务是在图像内部添加白色边框,并在边框和图像之间填充2px。添加填充图像GD
- 上传图像
- 然后我周围创造白色边框。
- 然后我创建上传图片的宽度和高度更大的黑色背景(图片)。
- 最后,我将边框的原始图像添加到创建的黑色图像的中心。
因此,我在第二点创建了白色边框。我所有的代码如下所示:
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
// 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-> image_inside',该如何启动?我的代码加载:[代码示例](http://paste.ofcode.org/rJ86zYeVj25kjEwLhSWjwV),我想从这张图片开始。并保存结果到'$ this-> image_inside' –
'$ this-> image_inside'是一个GD资源,所以你可以在我的例子中简单地用'$ this-> image_inside'代替'$ src'。要将结果保存到'$ this-> image_inside',只需将它指向相同的最终图像资源:'$ this-> image_inside = $ dest;'。 – timclutton
仍在寻找创意 –
你想添加边框而不覆盖任何图像(因此增加画布大小),或者用边框覆盖图像的边缘(以便保持画布大小相同)? – timclutton
我想在图像周围添加边框,所以没有覆盖图像 –