$add="images/a.jpg";
//$add2="images/1.jpg"; // Remove comment if a new image is to be created
$border=30; // Change the value to adjust width
$im=ImageCreateFromJpeg($add);
$width=ImageSx($im);
$height=ImageSy($im);
Our image a.jpg is stored inside the images directory. We care crating a border of width 30 for that the variable $border is assigned a value of 30. This value can be changed from a script. For example we can take the input from the user asking them what should be the border width and then assign that value to $border. $img_adj_width=$width+(2*$border);
$img_adj_height=$height+(2*$border);
$newimage=imagecreatetruecolor($img_adj_width,$img_adj_height);
Now let us fix the border color and fill the new image with the rectangle
$border_color = imagecolorallocate($newimage, 255, 255, 255);
imagefilledrectangle($newimage,0,0,$img_adj_width,$img_adj_height,$border_color);
Finally let us create the new image by resizing the image
imageCopyResized($newimage,$im,$border,$border,0,0,$width,$height,$width,$height);
ImageJpeg($newimage,$add,100); // change here to $add2 if a new image is to be created
chmod("$add",0666); // change here to $add2 if a new image is to be created
Now our new image with border is created. We can create a new image by assigning a new path and image location $add2.
This script can be modified a little and can be kept inside a loop to change all the picture of a directory by adding border to all. We need to read all the files of the directory and then apply our script to add border.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.