Answer the question
In order to leave comments, you need to log in
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.
$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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question