/** * generates an image from arbitrary text. * @param string text to place on image * @param string file name * @return void */ function generate($name, $overwrite = false) { $text = self::get_surname($name->sortkeyprefix); $filename = $name->pageid; $options = wkwThemeAdmin::get_datascape_options(); /* get image path */ $uploads_dir = wp_upload_dir(); $namesDir = $uploads_dir["basedir"] . "/wkw-cache/names"; $filename = $namesDir . "/" . $filename . ".png"; if (file_exists($filename)) { if ($overwrite) { @unlink($filename); } else { return; } } /* settings */ $fontPath = get_template_directory() . '/fonts/HandsomePro-Extended.ttf'; $fontSize = $options["fontsize"]; $textcolour = $name->urltitle ? wkwThemeAdmin::hex2RGB($options["names_colour"]) : wkwThemeAdmin::hex2RGB($options["names_colour_unlinked"]); /* get dimensions of image */ $bbox = imagettfbbox($fontSize, 0, $fontPath, $text); $width = $bbox[4] + 24; $height = $fontSize * 2; /* set baseline */ $y = $fontSize * 1.25; /* create image */ $im = self::createImage($width, $height); /* set colour */ $colour = imagecolorallocate($im, $textcolour["red"], $textcolour["green"], $textcolour["blue"]); /* Add the text */ imagettftext($im, $fontSize, 0, 12, $y, $colour, $fontPath, $text); /* save image */ imagepng($im, $filename, 9); /* clean up */ imagedestroy($im); }