2011-06-30 60 views
1

這一個是空的圖像寫在圖像的中心文字書寫幫助我使用PHP GD

enter image description here

這裏之前是我的代碼

<?php 
function LoadJpeg($imgname) 
{ 
    $im = @imagecreatefromjpeg($imgname); 

    $grey = imagecolorallocate($im, 255, 255, 0); 

    // The text to draw 
    $text = 'http://www.stackoverflow.com'; 

    // Replace path by your own font path 
    $font = 'CONSOLA.TTF'; 

    list($width, $height) = getimagesize($imgname); 

    // wants to know how to use this width/height dynamically // 

    imagettftext($im, 20, 45, 200, 450, $grey, $font, $text); 

    return $im; 
} 

header('Content-Type: image/jpeg'); 

$img = LoadJpeg('Blue_hills.jpg'); 

imagejpeg($img); 
imagedestroy($img); 
?> 

圖片上

輸入文字後,

enter image description here

Wh在我想要的是垂直和水平中心在45度的文字。請幫助我。謝謝大家。

+0

我討厭在照片上寫字。尤其是網址。 –

回答

2

使用imagettfbbox函數檢索文本渲染需要的尺寸。然後使用該信息來計算目標圖像中您應該定位的x,y座標,以便文本相對於寬度/高度居中。

0

我對PHP知之甚少(因爲我一直在做這件事)。

我建議製作一個新的方形圖像,將其設置爲與imagecolortransparent()具有透明背景。然後將文本寫入透明圖像。

接下來,我會嘗試使用imagecopyresized()將文本複製和縮放到新窗口。使用原稿高度和寬度的最小值作爲目標尺寸。偏移量會像(max($ width,$ height)-min($ width,$ height))/ 2。將偏移量應用於更大的尺寸。