<?php
/**********************
函數使用說明
印出圖片
imagettftext(resource $image , float $size , float $angle , int $x , int $y ,
int $color , string $fontfile , string $text [, array $extrainfo ])
取得字串在圖片中的範圍
imagettfbbox(float size,float angle,float fontfile,string text): array()
(6,7)------------------(4,5)
| |
| |
(0,1)------------------(2,3)
0 左下角 X 位置 1 左下角 Y 位置 2 右下角 X 位置 3 右下角 Y 位置
4 右上角 X 位置 5 右上角 Y 位置 6 左上角 X 位置 7 左上角 Y 位置
***********************/
session_start();
include $_SERVER['DOCUMENT_ROOT'].'/php/GD.php';
$randpic=rand(1,7);
header('Content-Type: image/jpeg');
$im=@LoadJPG($_SERVER['DOCUMENT_ROOT'].'/data/pic/divine/bg'.$randpic.'.jpg')
or die('Cannot Initialize new GD image stream');
//變數
if($_GET["action"]=="w"){
if(empty($_POST["string1"])){
if(!empty($_POST["string2"])){
$_SESSION["string1"]=$_POST["string2"];
$_SESSION["string2"]="";
}else{
$_SESSION["string1"]="死屁孩?";
$_SESSION["string2"]="標題不可為空";
}
}else{
$_SESSION["string1"]=$_POST["string1"];
$_SESSION["string2"]=$_POST["string2"];
}
if(!((int)$_POST["num"]<=12 && (int)$_POST["num"]>=2)){
$_SESSION["string2"].="數字還他媽亂填";
$_SESSION["num"]=-1;
}else{
$_SESSION["num"]=(int)$_POST["num"];
}
$_SESSION["type"]=$_POST["type"];
}
$str_line_1=($_SESSION["string1"]!="")? $_SESSION["string1"]:"str_line_1";
$str_line_2=$_SESSION["string2"];
$rank_num=(!empty($_SESSION["num"]))? $_SESSION["num"]:3;
$img_width=imagesx($im);
$img_height=imagesy($im);
$font=$gd_font_microsoft;
$text_color=imagecolorallocate($im, 255, 255, 255); //R,G,B 0~255
$star=array("牡羊","金牛","雙子","巨蟹","獅子","處女","天秤","天蠍","射手","魔羯","水瓶","雙魚");
$star2=array("牡羊","金牛","雙子","巨蟹","獅子","處女","天秤","天蠍","射手","魔羯","水瓶","雙魚");
shuffle($star); //隨機打亂陣列
shuffle($star2); //隨機打亂陣列
//取得第一行標題和第二行標題的寬度 選擇標題最大寬度者 及 標題字數較長者
$position_str1=imagettfbbox(1, 0, $font, $str_line_1);
$position_str2=imagettfbbox(1, 0, $font, $str_line_2);
if($position_str1[4]-$position_str1[6]>$position_str2[4]-$position_str2[6]){
$max_width=$delta_x_1;
$max_str=$str_line_1;
}else{
$max_width=$delta_x_2;
$max_str=$str_line_2;
}
$new_font_size=10;
do{
$new_title_position=imagettfbbox($new_font_size, 0, $font, $max_str);
$new_font_size++;
}while($new_title_position[4]-$new_title_position[6] < $img_width-100 && $new_font_size <= $img_width/12);
//調整標題位罝(標題水平置中) (若str2無內容,則 $y-2=$y_1)
$new_position_str1 = imagettfbbox($new_font_size, 0, $font, $str_line_1);
$x_1 = ($img_width-($new_position_str1[4]-$new_position_str1[6])) / 2;
$y_1 = ($new_position_str1[1]-$new_position_str1[7])*1.5;
imagettftext($im, $new_font_size, 0, $x_1, $y_1, $text_color, $font, $str_line_1);
if(!empty($str_line_2)){
$new_position_str2=imagettfbbox($new_font_size, 0, $font, $str_line_2);
$x_2 = ($img_width-($new_position_str2[4]-$new_position_str2[6])) / 2;
$y_2 = $y_1*2;
imagettftext($im, $new_font_size, 0, $x_2, $y_2, $text_color, $font, $str_line_2);
}else{
$y_2 = $y_1;
}
$rank_unit_height=($img_height-$y_2)/(2.25*$rank_num+1); //rank行(1.25單位高) + rank+1間隔(1單位高)= $img_height-$y_2(剩下的空間)
//
//(rank_unit_height)
//第一名:(rank_unit_height)*1.25
//(rank_unit_height)
//第二名:(rank_unit_height)*1.25
//
$rank_font_size=$rank_unit_height*1.25;
$rank_font_size=($rank_font_size>$new_font_size)? $new_font_size:$rank_font_size; //避免rank字體大於標題
$y=$y_2;
for($i=1; $i<=$rank_num; $i++){
$y+=2.25*$rank_unit_height;
if($_SESSION['type']=='top'){
$str='TOP'.$i.':'.$star[$i-1].'座';
}else if($_SESSION["type"]=="boy_and_girl"){
$str=$star2[$i-1].'男 x '.$star[$i-1].'女';
}else if($_SESSION["type"]=="girl_and_boy"){
$str=$star2[$i-1].'女 x '.$star[$i-1].'男';
}else if($_SESSION["type"]=="con_and_con"){
$str=$star2[$i-1].'座 x '.$star[$i-1].'座';
}else{
$str='error';
}
//計算水平置中
$rank_possition=imagettfbbox($rank_font_size, 0, $font, $str);
$x=(int)(($img_width-($rank_possition[4]-$rank_possition[6]))/2);
imagettftext($im, $rank_font_size, 0, $x, $y, $text_color, $font, $str);
}
//加入浮水印
$logo_string_2="https://had.name/divine/";
$logo_string="Hadname Online Technology";
$logo_font_size=1;
do{
$logo_position=imagettfbbox($logo_font_size, 0, $font, $logo_string);
$logo_font_size++;
}while($logo_position[4]-$logo_position[6]<$img_width*0.33);
$logo_font_size--;
$logo_w=$logo_position[4]-$logo_position[6];
$logo_h=$logo_position[1]-$logo_position[7];
$logo_x=$img_width*0.66-$logo_font_size;
$logo_y=$img_height-$logo_font_size;
$logo_color=imagecolorallocatealpha($im,255,255,255,64); //最後一個值為0~127 0最不透明 127完全透明
imagettftext($im, $logo_font_size, 0, $logo_x, $logo_y, $logo_color, $font, $logo_string_2);
imagettftext($im, $logo_font_size, 0, $logo_x, $logo_y-$logo_font_size*1.5, $logo_color, $font, $logo_string);
imagepng($im); //產生圖片
imagedestroy($im); //結束$im釋放記憶體
?>