我的常用生成缩略图与加水印函数(PHP)

发布时间:2007年11月23日      浏览次数:1190 次
GDImage.inc.php函数文件
<?
//==========================================
// 函数: Img_Create($sourFile,$newName,$width(可省略),$height(可省略))
// 功能: 生成缩略图(输出到浏览器)
// 参数: $sourFile 图片源文件路径
// 参数: $newName 图片缩略图文件路径
// 参数: $width 生成缩略图的宽度
// 参数: $height 生成缩略图的高度
// 返回: true 成功,false 失败
//==========================================
function Img_Create($sourFile,$newName,$width=100,$height=100)
{
      if (file_exists($sourFile))
      {
            $imageInfo = getInfo($sourFile);
            switch ($imageInfo["type"])
            {
                  case 1: //gif
                        $img = imagecreatefromgif($sourFile);
                        break;
                  case 2: //jpg
                        $img = imagecreatefromjpeg($sourFile);
                        break;
                  case 3: //png
                        $img = imagecreatefrompng($sourFile);
                        break;
                  default:
                        return false;
                        break;
            }
            if (!$img)
            return false;
      
            $width = ($width > $imageInfo["width"]) ? $imageInfo["width"] : $width;
            $height = ($height > $imageInfo["height"]) ? $imageInfo["height"] : $height;
            $srcW = $imageInfo["width"];
            $srcH = $imageInfo["height"];
            if ($srcW * $width > $srcH * $height)
                  $height = round($srcH * $width / $srcW);
            else
                  $width = round($srcW * $height / $srcH);
            //*
            if (function_exists("imagecreatetruecolor")) //GD2.0.1
            {
                  $new = imagecreatetruecolor($width, $height);
                  ImageCopyResampled($new, $img, 0, 0, 0, 0, $width, $height, $imageInfo["width"], $imageInfo["height"]);
            }
            else
            {
                  $new = imagecreate($width, $height);
                  ImageCopyResized($new, $img, 0, 0, 0, 0, $width, $height, $imageInfo["width"], $imageInfo["height"]);
            }
            //*/
            if (file_exists($newName))
            unlink($newName);
            if (ImageJPEG($new,$newName))
            {      return true; }
            else
            {      return false; }
            ImageDestroy($new);
            ImageDestroy($img);
      }
      else
      {
            return false;
      }
}
//==========================================
// 函数: Img_Text($sourFile, $text ,$fontName)
// 功能: 给图片加水印
// 参数: $sourFile 图片文件名
// 参数: $text 文本数组(包含二个字符串)
// 参数: $fontName 水印字体的文件路径DEFAULT.TTF默认与函数在同一文件夹,用时自已找一个字体文件
// 返回: true 成功,false 失败
//==========================================
function Img_Text($sourFile, $text , $fontName = "DEFAULT.TTF")
{
      if (file_exists($sourFile) && file_exists($fontName))
      {
            $imageInfo = getInfo($sourFile);
            switch ($imageInfo["type"])
            {
                  case 1: //gif
                        $img = imagecreatefromgif($sourFile);
                        break;
                  case 2: //jpg
                        $img = imagecreatefromjpeg($sourFile);
                        break;
                  case 3: //png
                        $img = imagecreatefrompng($sourFile);
                        break;
                  default:
                        return false;
                        break;
            }
            if (!$img)
            return false;
            
            $width = $imageInfo["width"];
            $height = $imageInfo["height"];
            //*
            if (function_exists("imagecreatetruecolor")) //GD2.0.1
            {
                  $new = imagecreatetruecolor($width, $height);
                  ImageCopyResampled($new, $img, 0, 0, 0, 0, $width, $height, $imageInfo["width"], $imageInfo["height"]);
            }
            else
            {
                  $new = imagecreate($width, $height);
                  ImageCopyResized($new, $img, 0, 0, 0, 0, $width, $height, $imageInfo["width"], $imageInfo["height"]);
            }
            $white = imageColorAllocate($new, 255, 255, 255);
            $black = imageColorAllocate($new, 0, 0, 0);
            $alpha = imageColorAllocateAlpha($new, 230, 230, 230, 40);
            ImageFilledRectangle($new, 0, $height-23, $width, $height, $alpha); //透明背景颜色
            ImageFilledRectangle($new, 13, $height-20, 15, $height-7, $black); //在左边加一条竖线
            ImageTTFText($new, 9, 0, 19, $height-9, $white, $fontName, $text); //加水印文字
            ImageTTFText($new, 9, 0, 21, $height-7, $white, $fontName, $text); //加水印文字
            ImageTTFText($new, 9, 0, 22, $height-6, $white, $fontName, $text); //加水印文字
            //ImageTTFText($new, 9, 0, 22, $height-6, $alpha, $fontName, $text); //加水印文字
            ImageTTFText($new, 9, 0, 20, $height-8, $black, $fontName, $text); //加水印文字
            //*/
            if (ImageJPEG($new,$sourFile))
            {      return true; }
            else
            {      return false; }
            ImageDestroy($new);
            ImageDestroy($img);
      }
      else
      {
            return false;
      }
}
//==========================================
// 函数: getInfo($file)
// 功能: 返回图像信息
// 参数: $file 文件路径
// 返回: 图片信息数组
//==========================================
function getInfo($file)
{
$file = $file;
$data = getimagesize($file);
$imageInfo["width"] = $data[0];
$imageInfo["height"]= $data[1];
$imageInfo["type"] = $data[2];
$imageInfo["name"] = basename($file);
return $imageInfo;
}
?>
调用示例
<?
require_once("GDImage.inc.php");
if (Img_Create("1/logo_i.gif","1/min1.jpg"))
echo "生成缩略图成功</br>";
else
echo "生成缩略图失败</br>";
if (Img_Text("1/logo_i.gif","Web site,very good!"))
echo "添加水印成功</br>";
else
echo "添加水印失败</br>";
?>
文章来源:桂林唯创网络
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!