Home  >  Article  >  Backend Development  >  PHP loads fonts and saves them as images, _PHP tutorial

PHP loads fonts and saves them as images, _PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:45:37987browse

php loads fonts and saves them as images,

[php] view plaincopyPHP loads fonts and saves them as images, _PHP tutorialPHP loads fonts and saves them as images, _PHP tutorial
  1. // Set the content-type
  2. header("Content-type: image/png");
  3. // Create the image
  4. $im = imagecreatetruecolor(400, 100);
  5. // Create some colors
  6. $white = imagecolorallocate($im, 255, 255, 255);
  7. $grey = imagecolorallocate($im, 128, 128, 128);
  8. $black = imagecolorallocate($im, 0, 0, 0);
  9. imagefilledrectangle($im, 0, 0, 399, 100, $white);
  10. // The text to draw
  11. $text = 'Dictionary.com';
  12. // Replace path by your own font path
  13. $font = 'fontName.ttf';
  14. // Add some shadow to the text
  15. //imagettftext($im, 60, 0, 11, 21, $grey, $font, $text);
  16. // Add the text
  17. imagettftext($im, 60, 0, 0, 70, $black, $font, $text);
  18. // Using imagepng() results in clearer text compared with imagejpeg()
  19. imagepng($im);
  20. imagedestroy($im);

If you want to save the picture, you can use the following code

[html] view plaincopyPHP loads fonts and saves them as images, _PHP tutorialPHP loads fonts and saves them as images, _PHP tutorial
    1. ob_start();
    2. imagejpeg($im);
    3. $img = ob_get_contents();
    4. ob_end_clean();
    5. $size = strlen($img);
    6. $fp2=@fopen('tst.jpg', "a");
    7. fwrite($fp2,$img);
    8. fclose($fp2);

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1040168.htmlTechArticlephp loads fonts and saves them as images, [php] view plaincopy //Setthecontent-type header( "Content-type :image/png"); //Createtheimage $im=imagecreatetruecolor(400,100); //Createsome...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn