K
K
kamisarlapsha2019-08-03 16:16:30
PHP
kamisarlapsha, 2019-08-03 16:16:30

How to add padding to a picture in php GD?

Hey!
I initially have a wide image. Next, I find the coordinates of the subject in the photo and crop it. The result is a full-width photo of the object. How to add padding to a cropped photo? That is, for example, to have padding 20px on the sides.

wide photo
5d45874f70223910476459.jpeg

cropped photo
5d4587b44f54f100132059.jpeg

Here is the trim code:
$imageWidth = $this->width;
  $imageHeight = $this->height;

        //Тут нахожу координаты ($b_lft,  $b_rt, $b_top, $b_btm) предмета на фото

        $cw = $b_lft + $b_rt;
        $ch = $b_top + $b_btm;
    
        $cropped_width = $imageWidth - $cw; 
        $cropped_height = $imageHeight - $ch;
        
        $image_old = $this->image;
        $this->image = imagecreatetruecolor($cropped_width, $cropped_height);

        $background = imagecolorallocate($this->image, 255, 255, 255);
        imagefilledrectangle($this->image, 0, 0, $cropped_width, $cropped_height, $background);
       
        imagecopy($this->image, $image_old, 0, 0, $b_lft, $b_btm, $cropped_width, $cropped_height);
  imagedestroy($image_old);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Arthur, 2019-08-03
@kamisarlapsha

Add 40 to calculated width before crop?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question