This site is developed to XHTML and CSS2 W3C standards.
If you see this paragraph, your browser does not support those standards and you
need to upgrade. Visit WaSP
for a variety of options.
Paste #84
Posted by: BitByByte
Posted on: 2026-02-04 22:09:14
Age: 4 days ago
Views: 15
<?php
header("Content-Type: image/png");
$width = 88;
$height = 31;
$boldFontPath = './arialbd.ttf';
$fontPath = './tahoma.ttf';
$from = isset($_GET['from']) ? strtoupper($_GET['from']) : 'USD';
$to = isset($_GET['to']) ? strtoupper($_GET['to']) : 'RUB';
$lang = isset($_GET['lang']) ? strtolower($_GET['lang']) : 'en';
$bgHex = isset($_GET['bgcolor']) ? $_GET['bgcolor'] : 'FEFEFE';
if (!preg_match('/^[A-Z]{3}$/', $from)) $from = 'USD';
if (!preg_match('/^[A-Z]{3}$/', $to)) $to = 'RUB';
if ($lang !== 'ru' && $lang !== 'en') $lang = 'en';
if (!preg_match('/^[0-9A-Fa-f]{6}$/', $bgHex)) $bgHex = 'FEFEFE';
$cacheDir = __DIR__ . '/cache';
if (!is_dir($cacheDir)) mkdir($cacheDir, 0777, true);
$cacheFile = $cacheDir . '/'.md5("$from|$to|$lang|$bgHex").'.png';
$cacheTime = 24 * 3600;
if (file_exists($cacheFile) && (time() - filemtime($cacheFile)) < $cacheTime) {
readfile($cacheFile);
exit;
}
$bgR = hexdec(substr($bgHex, 0, 2));
$bgG = hexdec(substr($bgHex, 2, 2));
$bgB = hexdec(substr($bgHex, 4, 2));
$value = "?";
$json = @file_get_contents("https://api.exchangerate-api.com/v4/latest/USD");
if ($json !== false) {
$data = json_decode($json, true);
if (isset($data['rates'][$from], $data['rates'][$to])) {
$rate = $data['rates'][$to] / $data['rates'][$from];
$value = number_format($rate, 2);
}
}
$image = imagecreatetruecolor($width, $height);
$bg = imagecolorallocate($image, $bgR, $bgG, $bgB);
$text = imagecolorallocate($image, 0, 0, 0);
$textWhite = imagecolorallocate($image, 255, 255, 255);
$border = imagecolorallocate($image, 0, 0, 0);
imagefill($image, 0, 0, $bg);
$headerHeight = 10;
$start = [187,187,208];
$end = [74,74,151];
for ($y = 0; $y < $headerHeight; $y++) {
$r = intval($start[0] + ($y / $headerHeight) * ($end[0] - $start[0]));
$g = intval($start[1] + ($y / $headerHeight) * ($end[1] - $start[1]));
$b = intval($start[2] + ($y / $headerHeight) * ($end[2] - $start[2]));
$c = imagecolorallocate($image, $r, $g, $b);
imageline($image, 0, $y, $width, $y, $c);
}
imagerectangle($image, 0, 0, $width-1, $height-1, $border);
$titles = ['en' => 'Exchange rate', 'ru' => 'Курс валют'];
$title = $titles[$lang];
$titleFontSize = 7;
$box = imagettfbbox($titleFontSize, 0, $boldFontPath, $title);
$titleWidth = $box[2] - $box[0];
$titleX = ($width - $titleWidth) / 2;
imagettftext($image, $titleFontSize, 0, $titleX, 9, $textWhite, $boldFontPath, $title);
$textFontSize = 7;
imagettftext($image, $textFontSize, 0, 4, 20, $text, $fontPath, "$from: $value $to");
imagepng($image, $cacheFile);
imagepng($image);
imagedestroy($image);
?>
Download raw |
Create new paste