33, "score" => 62, "result" => 24, "title" => 33, "base" => 16 ]; $params = $_GET['q'] ?? 'default'; $params = explode(';', base64_decode($params)); $image = imagecreatefromjpeg('group.jpg'); $date = date('d.m.Y'); $points_text = [ "lv" => "Punkti", "ru" => "Пункты", "en" => "Points" ]; $title_1_text = [ "lv" => "Augšup, lejup!", "ru" => "Augšup, lejup!", "en" => "Up, down!" ]; $title_2_text = [ "lv" => "Kas nosaka valūtas kursu?", "ru" => "Kas nosaka valūtas kursu?", "en" => "What determines the exchange rate?" ]; $base_color = imagecolorallocate($image, 6, 0, 95); $base_color_lighter = imagecolorallocate($image, 137, 138, 183); $white_color = imagecolorallocate($image, 255, 255, 255); $fontBold = realpath('TahomaBold.ttf'); $fontRegular = realpath('TahomaRegular.ttf'); $name_params = calculateTextBox($params[0], $fontBold, $fonts["name"], 0); $score_params = calculateTextBox($params[1], $fontBold, $fonts["score"], 0); $result_params = calculateTextBox($params[2], $fontBold, $fonts["result"], 0); $title_1_params = calculateTextBox($title_1_text[$params[3]], $fontBold, $fonts["title"], 0); $title_2_params = calculateTextBox($title_2_text[$params[3]], $fontBold, $fonts["result"], 0); $date_params = calculateTextBox($date, $fontRegular, $fonts["base"], 0); $points_params = calculateTextBox($points_text[$params[3]], $fontRegular, $fonts["base"], 0); $coordinates = [ "name" => [ "x" => ($base_width - $name_params["width"])/2, "y" => 503 + $name_params["height"] ], "score" => [ "x" => ($base_width - $score_params["width"])/2, "y" => 280 + $score_params["height"] ], "result" => [ "x" => ($base_width - $result_params["width"])/2, "y" => 392 + $result_params["height"] ], "date" => [ "x" => ($base_width - $date_params["width"])/2, "y" => 585 + $date_params["height"] ], "title_1" => [ "x" => ($base_width - $title_1_params["width"])/2, "y" => 45 + $title_1_params["height"] ], "title_2" => [ "x" => ($base_width - $title_2_params["width"])/2, "y" => 45 + $title_1_params["height"] + $title_2_params["height"] + 20 ], "points" => [ "x" => ($base_width - $points_params["width"])/2, "y" => 355 + $points_params["height"] ], ]; // splitting name into parts $spacer = 300; // sum of equal spaces that has to be left from both sides if ($name_params["width"] + $spacer > $base_width) { $str_arr = explode(" ", $params[0]); $str = ""; $str_output = 1; foreach ($str_arr as $key => $name_part) { $str_params = calculateTextBox($str, $fontBold, 24, 0); $name_part_params = calculateTextBox($name_part, $fontBold, 24, 0); if ($str_params["width"] + $name_part_params["width"] + $spacer < $base_width) { $str .= $name_part . ' '; } else { imagettftext ($image, 24,0, ($base_width - $str_params["width"])/2, 503 + $str_params["height"] * $str_output, $white_color, $fontBold , $str); $str = $name_part . ' '; $str_output++; } } $str_params = calculateTextBox($str, $fontBold, 24, 0); imagettftext ($image, 24,0, ($base_width - $str_params["width"])/2, 508 + $str_params["height"] * $str_output, $white_color, $fontBold , $str); } else { imagettftext ($image, $fonts["name"],0, $coordinates['name']['x'], $coordinates['name']['y'], $white_color, $fontBold , $params[0]); } imagettftext ($image, $fonts["score"],0, $coordinates['score']['x'], $coordinates['score']['y'], $base_color, $fontBold , $params[1]); imagettftext ($image, $fonts["result"],0, $coordinates['result']['x'], $coordinates['result']['y'], $base_color, $fontBold , $params[2]); imagettftext ($image, $fonts["title"],0, $coordinates['title_1']['x'], $coordinates['title_1']['y'], $white_color, $fontBold , $title_1_text[$params[3]]); imagettftext ($image, $fonts["result"],0, $coordinates['title_2']['x'], $coordinates['title_2']['y'], $white_color, $fontBold , $title_2_text[$params[3]]); imagettftext ($image, $fonts["base"],0, $coordinates['date']['x'], $coordinates['date']['y'], $base_color_lighter, $fontRegular , $date); imagettftext ($image, $fonts["base"],0, $coordinates['points']['x'], $coordinates['points']['y'], $base_color, $fontRegular , $points_text[$params[3]]); header("Content-type: image/jpeg"); return imagejpeg($image); function calculateTextBox($text,$fontFile,$fontSize,$fontAngle) { /************ simple function that calculates the *exact* bounding box (single pixel precision). The function returns an associative array with these keys: left, top: coordinates you will pass to imagettftext width, height: dimension of the image you have to create *************/ $rect = imagettfbbox($fontSize,$fontAngle,$fontFile,$text); $minX = min(array($rect[0],$rect[2],$rect[4],$rect[6])); $maxX = max(array($rect[0],$rect[2],$rect[4],$rect[6])); $minY = min(array($rect[1],$rect[3],$rect[5],$rect[7])); $maxY = max(array($rect[1],$rect[3],$rect[5],$rect[7])); return array( "left" => abs($minX) - 1, "top" => abs($minY) - 1, "width" => $maxX - $minX, "height" => $maxY - $minY, "box" => $rect ); }