开发者

Add border on image without modify original (GD php)

I would like to add botto开发者_运维问答m 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


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);
}


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); 
} 

?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜