Skip to content

Commit

Permalink
improve text to png icons, sharper and fix width
Browse files Browse the repository at this point in the history
- height of text image generator to 16px makes it a bit sharper
- Fixes ConX#26 by setting width of img to auto to prevent cutting of the
image
- calculating the text width before creating the image to remove the
too wide margins
  • Loading branch information
Klap-in committed Dec 23, 2023
1 parent 2aac3bb commit 48c206b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
6 changes: 4 additions & 2 deletions action.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,17 @@ protected function makelist()
'icon' => $ico,
'open' => $button['pretag'],
'close' => $button['posttag'],
'sample' => $button['sample'] ?? ''
'sample' => $button['sample'] ?? '',
'class' => $button['icon'] ? '' : 'textbutton'
];
} else {
$buttonlist[] = [
'type' => 'insert',
'title' => $button['label'],
'icon' => $ico,
'insert' => $button['code'],
'block' => true
'block' => true,
'class' => $button['icon'] ? '' : 'textbutton'
];
}
}
Expand Down
32 changes: 17 additions & 15 deletions genpng.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,32 @@
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");

/*image generation code*/
if (isset($_GET['text'])) {
$width = strlen($_GET['text']) * 11;
} else {
$width = 66;
}
$bg = imagecreatetruecolor($width, 15);
/* image generation code */

//Text to be written
$text = $_GET['text'] ?? "No Name";

//font
$font = './DejaVuSans.ttf'; //path to font you want to use
$fontsize = 10; //size of font

//calculate width from bounding box for the text
$tb = imagettfbbox($fontsize, 0, $font, $text);
$width = $tb[2] - $tb[0];

$bg = imagecreatetruecolor($width, 16);

//This will make it transparent
imagesavealpha($bg, true);
$trans_colour = imagecolorallocatealpha($bg, 0, 0, 0, 127);
imagefill($bg, 0, 0, $trans_colour);

//Text to be written
$text = isset($_GET['text']) ? $_GET['text'] : "No Name";

// Black Text
$black = imagecolorallocate($bg, 0, 0, 0);

$font = './DejaVuSans.ttf'; //path to font you want to use
$fontsize = 10; //size of font

//Writes text to the image using fonts using FreeType 2
imagettftext($bg, $fontsize, 0, 10, 12, $black, $font, $text);
//Writes text to the image using fonts using a TrueType font
//no x margin, because button adds margin as well
imagettftext($bg, $fontsize, 0, 0, 12, $black, $font, $text);

//Create image
imagepng($bg);
Expand Down
6 changes: 5 additions & 1 deletion style.less
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
input.button.b {
margin: .5em 0 0 .5em;
}
}
}
form#cb_button_list {
th, td {
padding: .3em;
Expand All @@ -35,3 +35,7 @@
margin: .5em;
}
}

div.picker button.toolbutton.textbutton img{
width: auto;
}

0 comments on commit 48c206b

Please sign in to comment.