Archive for the ‘php’ Category

PHP生成文件自动下载

星期六, 8月 11th, 2007

通过php把数据库中的内容生成文件,让客户端自动下载,代码:

<?php
$cont = 从数据库中读取的内容;

$cont_len = strlen($cont);

Header(”Content-Disposition: attachment; filename=\”文件名\”");
Header(”Content-Type: application/x-unknown”); // 具体文件类型可以使用相应的mine类型
Header(”Content-Transfer-Encoding: none”);
Header(”Pragma: no-cache”);
Header(”Expires: 0″);
Header(”Content-Length: $cont_len”);

echo $cont;

?>

PHP为网站图片做水印标记程序

星期二, 5月 29th, 2007

// PHP为网站图片做水印标记程序
// 在图片上打上网站网址水印标记
// 服务器必须安装了GD库和FreeType
// 使用TureType字体,可以任意调整字体和文字大小
// 支持 gif 、 jpeg 、 png 文件
// $src : 原图片文件
// $dst : 做水印后的图片保存文件路径
function watermark($src, $dst)
{
 $image_info = getimagesize($src);
 switch($image_info[2])
 {
  case 1:
   $res = imagecreatefromgif($src);
   break;
  case 2:
   $res = imagecreatefromjpeg($src);
   break;
  case 3:
   $res = imagecreatefrompng($src);
   break;
  default:
   return false;
 }

 if(!$res)
 {
  return false;
 }

 $r = 0xFF;
 $g = 0xFF;
 $b = 0xFF;

 $tc = ImageColorAllocate($res, $r, $g, $g); //// 白色

 $font_file = 'times.ttf';             //// 字体文件、可以使用 c:\windows\fonts 目录下的文件
 $mark_text = 'http://www.taojie.com/';  //// 水印文字
 $font_size = 32;                     //// 文字大小

 $arr_ttf = ImageTtfBbox($font_size, 0, $font_file, $mark_text);
 $ttf_width = Abs($arr_ttf[4] - $arr_ttf[0]);
 $ttf_height = Abs($arr_ttf[5] - $arr_ttf[1]);
 $x = ($image_info[0] - $ttf_width) / 2;     //// 水印文字位置,水平居中
 $y = $image_info[1] - $ttf_height;           //// 水印文字垂直位置

 if($x < 0)
 {
  $x = 0;
 }
 if($y < 0)
 {
  $y = 0;
 }

 ImageTtfText($res, $font_size, 0, $x, $y, $tc, $font_file, $mark_text);

 switch($image_info[2])
 {
  case 1:
   $ret = ImageGif($res, $dst);
   break;
  case 2:
   $ret = ImageJpeg($res, $dst);
   break;
  case 3:
   $ret = ImagePng($res, $dst);
   break;
  default:
   return false;
 }

 return $ret;
}


闽ICP备05021301号