Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php GD向图像添加边框无效_Php_Gd - Fatal编程技术网

Php GD向图像添加边框无效

Php GD向图像添加边框无效,php,gd,Php,Gd,因此,我已经尝试在上传的图像周围添加间隔为的白色边框: private function __draw_white_border() { // get source image and dimensions. $src = $this->file_data['im']; $src_w = imagesx($src); $src_h = imagesy($src); // create destination image with dimensions

因此,我已经尝试在上传的图像周围添加间隔为的白色边框:

private function __draw_white_border()
{
    // get source image and dimensions.
    $src = $this->file_data['im'];
    $src_w = imagesx($src);
    $src_h = imagesy($src);
    // create destination image with dimensions increased from $src for borders.
    $dest_w = $src_w + 10;
    $dest_h = $src_h + 10;
    $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, 5, 5, 0, 0, $src_w, $src_h);
}

该函数在不在类中时运行良好,但在该类中不起作用。似乎这个功能不保存图像或其他东西,我不知道。。它应该从
$this->file_data['im']
加载图像,并保存到同一个目标以备进一步操作。问题出在哪里?

运行此方法后,您不会将修改后的图像放回可以再次访问的位置。此代码(参见最后一行)将其放回原始图像的来源

private function __draw_white_border()
{
    // get source image and dimensions.
    $src = $this->file_data['im'];
    $src_w = imagesx($src);
    $src_h = imagesy($src);
    // create destination image with dimensions increased from $src for borders.
    $dest_w = $src_w + 10;
    $dest_h = $src_h + 10;
    $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, 5, 5, 0, 0, $src_w, $src_h);

    $this->file_data['im'] = $dest;
}

您需要创建两个图像,并将主图像分层到稍大的第二个图像上。通过这种方式,可以在主图像的边缘看到较大的图像,使其看起来像一个边框


我使用它来正确地分层图像。如果目标映像更大,则假设为100x100px。源映像是80x80px。您将在源图像的x和y上添加10,这将使源图像位于100x100px图像的中心。如果100x100px图像是纯黑色图像,它将生成一个干净的黑色边框

您打算如何处理
$dest
?@Kenney查看我添加的库,生成的图像将用于“演示海报”生成器。这是第一步中的一步,图像应该在其自身周围加上边框,为了更好地理解,这张图片应该在以后生成的图像中实现。我的意思是让您发现,
$dest
包含您生成的图像,但您不需要对它做任何处理,所以它被丢弃了。是的,我看到加载的图像是资源id#54,生成的图像是资源id#55。但我不知道如何合并它或其他东西..以前尝试过这个,但不起作用。。也许库中有一些bug?但这就是我正在做的,不是吗?试着将图像写入文件并检查文件的外观。图像JPEG($dest,“path”,90);如果图像有边框,那么您需要使用$destyes进行更多操作,当我使用J imagejpeg编写图像时-它有边框。但我使用不同的名称保存图像,当我尝试使用相同的名称保存图像时,我将使用以前使用的名称-没有发生任何情况啊,好的,看起来它可能是两件事情中的一件。1.您没有覆盖图像的权限。2.图像正在使用中,无法覆盖。尝试将$dest写入临时映像路径。销毁此方法中使用的所有对象,以确保原始图像未被锁定,并尝试用副本替换原始文件。查看apaches错误日志以了解发生了什么