duandiao3961 2015-06-20 20:46
浏览 42

图像GD - 将图像插入方形(PHP)

I'm trying to insert image over image. I'm drawing an empty image with square inside it where next image have to be stored:

private function __generate_background()
{
    $img = imagecreatetruecolor($this->settings['__image_size_data']['wrapper']['width'], $this->settings['__image_size_data']['wrapper']['height']);
    $bg = imagecolorallocate($img, 0, 0, 0);

    # draw background
    imagefilledrectangle($img, 0, 0, 120, 20, $bg);

    # set border line width and color
    imagesetthickness($img, 1);
    $img_color_white = imagecolorallocate($img, 255, 255, 255);

    # draw rectangle
    imagerectangle(
        $img,
        $this->__wrapper_padding['top'], # px from the left corner [start]
        $this->settings['__image_size_data']['image']['height'], # px from the top corner [start and end]
        ($this->settings['__image_size_data']['image']['width'] + $this->__wrapper_padding['left']),
        $this->__wrapper_padding['top'],
        $img_color_white
    );

    # save image
    imagejpeg($img, $this->settings['__temp_image'], 100);
}

and this is what I have after this step:

img1

the next step is to insert uploaded image inside the square (that one with white borders). Everything looks fine, but white borders disapper - the image should be wrapper with white border... I hope you will understand what I mean. This is the code:

private function __insert_image_to_wrapper()
{
    # loaded image from uploaded one - $this->image_inside
    # load image wrapper (black content with square inside)
    $this->temp_data['image_wrapper'] = imagecreatefromjpeg($this->settings['__temp_image']);

    list($width, $height) = getimagesize($this->settings['__image']);
    list($newWidth, $newHeight) = getimagesize($this->settings['__temp_image']);

    $out = imagecreatetruecolor($newWidth, $newHeight);
    imagecopyresampled($out, $this->temp_data['image_wrapper'], 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
    imagecopyresampled($out, $this->image_inside, $this->__wrapper_padding['left'], $this->__wrapper_padding['top'], 0, 0, $newWidth, $newHeight, $newWidth, $newHeight);
    imagejpeg($out, $this->settings['__temp_image'], 100);
}

and this is what I get..

img2

as you can see image is over borders.. Where is the problem?

  • 写回答

1条回答 默认 最新

  • duandai6373 2015-06-20 21:06
    关注

    You draw your uploaded image over your border. You could try to draw your image before your rectangle. So something like

       private function __generate_background() {
            // init, background, ...
    
            $this->__insert_image_to_wrapper();
    
            # draw rectangle
            imagerectangle(
                $img,
                $this->__wrapper_padding['top'], # px from the left corner [start]
                $this->settings['__image_size_data']['image']['height'], # px from the top corner [start and end]
                ($this->settings['__image_size_data']['image']['width'] + $this->__wrapper_padding['left']),
                $this->__wrapper_padding['top'],
                $img_color_white
            );
    
            # save image
            imagejpeg($img, $this->settings['__temp_image'], 100);
        }
    
    评论

报告相同问题?