2010-06-11 92 views
0

如何使用PHP創建帶有白色3px邊框的縮略圖圖像?如何使用PHP創建帶有白色3px邊框的縮略圖圖像?

+4

爲什麼不使用CSS來添加邊框? – 2010-06-11 08:50:08

+0

'' – jigfox 2010-06-11 08:55:32

+1

請參閱[各種縮略圖問題](http://stackoverflow.com/search?q=thumbnail+php)和[給圖像創建邊界以PHP](http://stackoverflow.com/questions/ 3006007 /給邊界到圖像創建的php) – Gordon 2010-06-11 09:10:58

回答

2

實際上還沒有試過,但我覺得這樣的事情可能會奏效

<?php 

/* 
* Function to create a border around an image 
*/ 
function drawBorder($image_name, $r = 0, $g = 0, $b = 0, $thickness = 1) 
{ 
    $image = ImageCreateFromJPEG($image_name); 
    $color = ImageColorAllocate($img, $r, $g, $b); 

    $x1 = 0; 
    $y1 = 0; 
    $x2 = ImageSX($image) - 1; 
    $y2 = ImageSY($image) - 1; 

    for($i = 0; $i < $thickness; $i++) 
    { 
    ImageRectangle($image, $x1++, $y1++, $x2--, $y2--, $color); 
    } 

    return $image; 
} 

?> 

然後運行像

<?php 
header('Content-type: image/jpeg'); 
ImageJPEG(drawBorder("images/foo.jpg", 128, 128, 0, 3)); 
?> 
+0

啊......如果你真的想要白色邊框(錯過了),你只是通過255,255,255作爲rgb值。 WOuld也許很高興有一個switch語句來實際檢查圖像的類型,然後使用適當的ImageCreateFrom *函數。 – inquam 2010-06-11 09:24:09

+0

非常感謝先生 – learner 2010-06-11 09:35:04

0

創建一個jpg(或gif/png),它是6px高度和6px比給定的縮略圖更多。採取所需的邊框顏色作爲背景顏色。那麼在它的縮略圖複製與startingpos的3px,3px的;-)

最佳實踐使用imagick庫

0
function addBorderpng($add){ 
    $border=5; 
    $im=imagecreatefrompng($add); 
    $width=imagesx($im); 
    $height=imagesy($im);  
    $img_adj_width=$width+(2*$border); 
    $img_adj_height=$height+(2*$border); 
    $newimage=imagecreatetruecolor($img_adj_width,$img_adj_height);  

    $border_color = imagecolorallocate($newimage, 255, 255, 255); 
    imagefilledrectangle($newimage,0,0,$img_adj_width, 

$ img_adj_height,$ border_color);

imagecopyresized($newimage,$im,$border,$border,0,0, 

$ width,$ height,$ width,$ height); imagepng($ newimage,$ add,9); chmod(「$ add」,0666);

}