首先 - 如果你想擴大圖像的寬度的餘量,我會採取圖像的原始寬度,加上你的邊緣寬度(讓我們稱它爲$ extraWidth,並假定它在某處定義)並用新寬度調整imagecreatetruecolor調用。 然後,您分配一種顏色,併爲您的顏色填充一個矩形的剩餘空間,如下所示。
$im = imagecreatetruecolor($dst_width+$extraWidth, $dst_height);
$color = imagecolorallocate($im, 0, 0, 255); // this is blue - change to what you want
imagefilledrectangle($im, $dst_width, 0, $dst_width+$extraWidth, $dst_height, $color);
請注意我沒有測試過這個代碼,只是提到了文檔。
$pixel_height_of_character = 30; // change this to actual pixel height of a char
$pixel_gap_between_chars = 3;
$start_from_edge_of_margin = 3;
$string_chars = str_split($_POST['color']);
$start = ($dst_height/2) - ((count($string_chars) * ($pixel_height_of_character+$pixel_gap_between_chars))/2)
// probably should round this
// also need to deduct one half of pixel gap from the result for centering purposes - i think - double check my math.
$left = $start_from_edge_of_margin + $dst_width;
foreach($string_chars as $char){
$top = $start + $pixel_height_of_character;
imagettftext($im, 30, 0, $left, $top, $red, $font, $char);
$start = $top + $pixel_gap_between_chars;
}
所以,這是相當多的解釋。
基本上,你計算每個字符的尺寸 - 然後使用這些尺寸計算第一個字符必須在哪裏開始 - 然後在循環中一次繪製一個字符。
該代碼完全無法表示 - 如果提供的單詞太長,則會超出圖像的邊界,因此您應該對此進行測試。此外,它可能不會完美 - 但這是一個好的開始。
修改後的代碼:
function UploadImage($img_name){
$vdir_upload = "img/upload/";
$vfile_upload = $vdir_upload . $img_name;
$file_name = basename($_FILES["img_1"]["name"]);
move_uploaded_file($_FILES["img_1"]["tmp_name"], $vfile_upload);
switch (strtolower(pathinfo($file_name, PATHINFO_EXTENSION))) {
case "jpg" :
$im_src = imagecreatefromjpeg($vfile_upload);
break;
case "jpeg" :
$im_src = imagecreatefromjpeg($vfile_upload);
break;
case "gif" :
$im_src = imagecreatefromgif($vfile_upload);
break;
case "png" :
$im_src = imagecreatefrompng($vfile_upload);
break;
default :
trigger_error("Error Bad Extention");
exit();
break;
}
$src_width = imageSX($im_src);
$src_height = imageSY($im_src);
$dst_width = 1979;
$dst_height = ($dst_width/$src_width)*$src_height;
$im = imagecreatetruecolor($dst_width+$extraWidth, $dst_height);
$color = imagecolorallocate($im, 0, 0, 255); // this is blue - change to what you want
imagefilledrectangle($im, $dst_width, 0, $dst_width+$extraWidth, $dst_height, $color);
imagecopyresampled($im, $im_src, 0, 0, 0, 0, $dst_width, $dst_height, $src_width, $src_height);
$font = 'Aliquam.ttf';
$red = imagecolorallocate($im, 255, 0, 0);
$pixel_height_of_character = 30; // change this to actual pixel height of a char
$pixel_gap_between_chars = 3;
$start_from_edge_of_margin = 3;
$string_chars = str_split($_POST['color']);
$start = ($dst_height/2) - ((count($string_chars) * ($pixel_height_of_character+$pixel_gap_between_chars))/2)
// probably should round this
// also need to deduct one half of pixel gap from the result for centering purposes - i think - double check my math.
$left = $start_from_edge_of_margin + $dst_width;
foreach($string_chars as $char){
$top = $start + $pixel_height_of_character;
imagettftext($im, 30, 0, $left, $top, $red, $font, $char);
$start = $top + $pixel_gap_between_chars;
}
imagejpeg($im,$vdir_upload . $_POST["number"].".jpg");
imagedestroy($im_src);
imagedestroy($im);
}
看一看在GD一些教程,繪製一個矩形,畫在上面的文字。 – n00dle