在没有修改原始图像的情况下在图像上添加边框

I would like to add bottom border of 10px height on bottom of every image WITHOUT resizing the original image.

Exemple: i have one jpg 300X200 px, i add black border of 10 px height on the bottom, my image is now 300X210 with black border.

Thanks for your help

Here is a code sample that will draw a border. I have used the GET variable to call the image.

If you for example put this code in your images folder, you can call example.org/images/resize.php?path=image.jpg, without modifying the original image. You can even use mod_rewrite (assuming you are using apache) to apply this filter.

However, this may affect the performance (doing it on the fly), so I recommend at least caching the images on the server, or just saving them to disk.

<?php 

$img = ImageCreateFromJPEG($_GET['path']); 

// Draw border 
$color_black = ImageColorAllocate($img, 0, 0, 0); 
drawBorder($img, $color_black, 10); 


// Output 
header('Content-type: image/jpeg'); 
ImageJPEG($img); 



// Draw a border 
function drawBorder(&$img, &$color, $thickness) 
{  
    $x = ImageSX($img); 
    $y = ImageSY($img); 

    for($i = 0; $i < $thickness; $i++) 
        ImageRectangle($img, 0, 0, $x, $y--, $color_black); 
} 

?>

Function drawBorder code looks wrong. Should be as follows:

function drawBorder(&$img, &$color, $thickness) {
  $x = ImageSX($img);
  $y = ImageSY($img);
  for($i = 0; $i < $thickness; $i++)
    ImageRectangle($img, $i, $i, $x--, $y--, $color);
}