文章的标题转成图片

由 Sniper_DA 于 周二, 2008-08-05 08:07 提交。

Drupal 6 已经不再使用_phptemplate_variables 函数了,它使用的叫做预处理,这个有许多种方法实现。下面的代码实现标题的隐藏

<?php
function lonely_preprocess_page(&$vars) {
 
$vars['title'] = '';
}
?>

现在,我们准备将文章的标题转成图片

  1. 准备字体,我用的是微软雅黑,放置到sites/default/themes/lonely/fonts/msyh.ttf
  2. 编写test.php,使用PHP的GD库将文字转成PNG图片并显示出来,这个文件的路径是test/test.php
    <?php
        header
    ("Content-type: image/png");
       
    $title = $_GET['txt'];
       
    $font = "/srv/www/drupal/sites/default/themes/lonely/fonts/msyh.ttf";
       
    $im = imagecreatetruecolor(500, 30);
       
    $white = imagecolorallocate($im, 255, 255, 255);
       
    $grey = imagecolorallocate($im, 128, 128, 128);
       
    $black = imagecolorallocate($im, 0, 0, 0);
       
    imageColorTransparent($im,$white);
       
    imagefilledrectangle($im, 0, 0, 499, 29, $white);
       
    imagettftext($im, 12, 0, 11, 21, $grey, $font, $title);
       
    imagettftext($im, 12, 0, 10, 20, $black, $font, $title);
       
    imagepng($im);
       
    imagedestroy($im);
    ?>

  3. 每个单独页面上的标题是由page.tpl.php控制的,修改这个文件当中显示标题的代码,换成生成图片的代码
    ......
    <?php //if ($title): print '<h2'. ($tabs ? ' class="with-tabs"' : '') .'>'. $title .'</h2>'; endif; ?>
    <?php if($title):?>
      <img src='http://192.168.0.103/test/test.php?txt=<?php echo $title; ?>'>
    <?php endif;?>
    ......

    这样我们在看每一篇文章的时候就可以看到,标题已经换成图片了。但是首页上还不是,首页上面的标题是由node.tpl.php控制的。

  4. 修改node.tpl.php文件当中的显示标题的代码,换成生成图片的代码

    <h2><a href="<?php print $node_url ?>" title="<?php print $title; ?>"><?php print $title; ?></a></h2>

    替换成
      <h2><a href="<?php print $node_url ?>" title="<?php print $title; ?>"><img src='http://192.168.0.103/test/test.php?txt=<?php echo $title; ?>' alt=<?php print $title; ?>></a></h2>

  5. 现在,任务完成了,如果看不到效果,那是由于Drupal的缓存机制影响,有几种办法解决,最简单的是到admin/build/themes选择主题。

附件大小
drupal.jpg10.85 千字节

好象有这样的模块吧

好象有这样的模块吧。

而且我觉得这样做对文章的收录不利。除非有特殊的用意,否则还是不要这样为好。

########## AD ################
http://www.petkk.com

最近新开的drupal6.x的子站,主要以学习为主,欢迎大家来交流
http://code.petkk.com

赞同楼上,喜欢楼主

赞同楼上,喜欢楼主的探索精神和感谢楼主的分享,但这种东西好像没多少实际应用价值。

-------

http://fhileo.cn
http://www.networkdictionary.com
http://www.networkdictionary.cn

个人感觉下列这段

个人感觉下列这段

<h2><a href="<?php print $node_url ?>" title="<?php print $title; ?>"><img src='http://192.168.0.103/test/test.php?txt=<?php echo $title; ?>' alt=<?php print $title; ?>></a></h2>

应该改成

<a href="<?php print $node_url ?>" title="<?php print $title; ?>"><img src='http://192.168.0.103/test/test.php?txt=<?php echo $title; ?>' alt=<?php print $title; ?>><h2><?php print $title; ?></h2></a>

首先来说h2对图片没作用,二来针对搜索引擎,如果你只是对图片使用了title,对图片有效果,而对页面来说没啥大的效果,一般的做法都是保留标题,但使用css将标题显示在页面之外,这样人看到图片,而搜索引擎看到了文字。

-------

http://fhileo.cn
http://www.networkdictionary.com
http://www.networkdictionary.cn