⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 txt2img.php

📁 在制作网页时
💻 PHP
字号:
<?php
function txt2img($text, $user_conf = null) {
    if(strlen($text)==0) return '';
    $textimage_cache = true; // set to false if you don't want caching, maybe during development.
    //$txt2img_directory = '/home/xshi/html/txt2img';
    $txt2img_directory = '/usr/html/txt2img';
    // should also include theme.
    $conf = array (
         'font' => 'STKAITI.TTF', // ttf fonts, may be put in the same directory as textimage
         'size' => 14, // a little dependent on server software (todo)
         'color' => array(0,0,0), // colors are accepted in this format array ($red, $green, $blue)
         'background_color' => array(255,255,255), // note: this is also made transparent
         'width' => null, // will be sat dynamically when not specified (pixelvalue)
         'height' => null,
         'padding' => array(0,3,0,3),
         'line_space' => 1,
         'transparent' => 1,
         'word_break' => 0 
    );
    if (isset($user_conf)) {
        foreach ($user_conf as $key => $value) {
            // overwrite
            $conf[$key] = $value;
        }
    }
    $tname='';
    foreach($conf as $para)
    {
      if(is_array($para))
         $tname.='('.implode(',',$para).')';
      else
         $tname.=$para.'_';
    }
    $cache_filename = md5($tname.$text).'.png';
    $cache_directory = $txt2img_directory.'/cache';
    $cache_directory_url = "/txt2img/cache";
    $cache_full_filename = $cache_directory.'/'.$cache_filename;
    // if cached, output and finish up. Don't put alot in here, needs to perform.
    // only do this if we need to create image.
    if (!$textimage_cache or !file_exists($cache_full_filename)) {
        // No cache used, we create the image.
        if (!is_dir($cache_directory) or !is_writable($cache_directory))
            die($cache_directory.' does not exist or is not writable');
        // default config, let other overwrite
        $conf['font'] = $txt2img_directory.'/font/'.$conf['font'];
        $line_dim = imagettfbbox($conf['size'], 0, $conf['font'], 'AJLMYabdfghjklpqry019`@$^&*(,');
        $lineHeight = $line_dim[1] - $line_dim[5];

        $text_dim = imagettfbbox($conf['size'], 0, $conf['font'], $text);
        $text_dim['width'] = $text_dim[2] - $text_dim[0];
        $text_dim['height'] = $lineHeight * $conf['line_space'];

        if (!isset($conf['width'])) $conf['width'] = $text_dim['width']+2;

        if ($text_dim['width'] > $conf['width'])  //word warp (include chinese words)
        {
           $lines = wordwarp($conf,$text);
        }
        else
        {
           $lines = array($text); // signle line.
        }

        if (!isset($conf['height'])) $conf['height'] = $text_dim['height'] * count($lines); 

        $img = imagecreate($conf['width']+$conf['padding'][1]+$conf['padding'][3], $conf['height']+$conf['padding'][0]+$conf['padding'][2]);
        // colors
        foreach (array (
            // place the keys that define a color here.
            // Keep background color first. First allocated becomes background color
            'background_color', 'color'
            ) as $key) {
            $conf[$key] = imagecolorallocate($img, $conf[$key][0], $conf[$key][1], $conf[$key][2]);
        }
        // create text
        $baseline=0.75*$lineHeight;
        $lineHeight *= $conf['line_space'];
        foreach($lines as $key => $line)
        {
          imagettftext(
            $img,
            $conf['size'],
            0, // angle
            $conf['padding'][3], // left
            $lineHeight*$key+$baseline+$conf['padding'][0],
            $conf['color'],
            $conf['font'],
            $line
          );
        }
        // set background color as transparent (maybe create switch on this)
        if($conf['transparent']==1) imagecolortransparent($img, $conf['background_color']);
        // save
        imagepng($img, $cache_full_filename);
        imageDestroy($img); 
    }
    // return html for image.
    list($width, $height, $type, $attr) = getimagesize($cache_full_filename);
    // $base_url from settings.php
    if(isset($conf['align'])) $align='align="'.$conf['align'].'"';
    else $align='';
    $html = '<img src="'.$cache_directory_url.'/'.$cache_filename.'" alt="'.$text.'" '.$align.' '.$attr.' />';
  return $html;
}
//============================================
function imgtextlen(&$conf,$text)
{
   static $long=0;
   $dim = imagettfbbox($conf['size'],0,$conf['font'],$text);
   return $dim[2]-$dim[0];
}
//============================================
function isbdright($ch)
{
  return mb_strpos(" ,.;:'\"]}銆傦紱锛屻

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -