电信主站 网通分站
购买流程 付款方式 常见问题 在线提问 续租服务 购物车
用户名: 密 码: 忘记密码?
首 页
域名注册
虚拟主机
双线主机
服务器租用
VPS主机
企业邮局
代理专区
客服中心
虚拟主机行业资讯 虚拟主机评测对比 互联网最新动态 技术学院 站长资讯 在线教程 网站运营
搜索优化 服务器 网络编程 图形图象 站长之家 网页制作 操作系统
冲浪宝典 软件教学 视频通信 办公软件 邮件系统 网络安全 认证考试
您当前位置:西部数码->资讯中心-> 在线教程-> PHP
给图片生成缩略图和加版权的类-PHP教程,PHP应用
作者:网友供稿 点击:12
  西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!虚拟主机可在线rar解压,自动数据恢复设置虚拟目录等.虚拟主机免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金!
文章页数:[1] 

给图片生成缩略图和加版权的类

最近几天看了一下php的图片处理方面的功能,以前这方面的需求比较少,也就没怎么看,最近有空看了一下。感觉图片处理一些简单的功能还可以,复杂的就算了,gd库都2.0.1了,还是不支持中文,看了几篇文章,想使用中文只能先将gb2312转换成unicode再写入图片,太麻烦了,索性只使用英文算了。

在图像生成部分可以定义图片的最大高,宽,比较适用于新闻及相册等系统。

gd2.0.1在图片处理上有很大提高,我试了下imagecopyresized和imagecopyresampled,后者处理的图片明显好于前者,据手册上讲后者对改变大小后的图片重新采样基本保持不失真,生成缩略图的效果还真不错。

-------------------------------------------------------------
下面是类
-----------------------
//====================================================
// filename:gdimage.inc.php
// summary: 图片处理程序
// author: ice_berg16(寻梦的稻草人)
// createtime: 2004-10-12
// lastmodifed:2004-10-12
// copyright (c)2004 ice_berg16@163.com
//====================================================

class gdimage
{
var $sourcepath; //图片存储路径
var $gallerypath; //图片缩略图存储路径
var $tofile = false; //是否生成文件
var $fontname; //使用的ttf字体名称
var $maxwidth = 500; //图片最大宽度
var $maxheight = 600; //图片最大高度

//==========================================
// 函数: gdimage($sourcepath ,$gallerypath, $fontpath)
// 功能: constructor
// 参数: $sourcepath 图片源路径(包括最后一个"/")
// 参数: $gallerypath 生成图片的路径
// 参数: $fontpath 字体路径
//==========================================
function gdimage($sourcepath, $gallerypath, $fontpath)
{
$this->sourcepath = $sourcepath;
$this->gallerypath = $gallerypath;
$this->fontname = $fontpath . "04b_08__.ttf";
}

//==========================================
// 函数: makethumb($sourfile,$width=128,$height=128)
// 功能: 生成缩略图(输出到浏览器)
// 参数: $sourfile 图片源文件
// 参数: $width 生成缩略图的宽度
// 参数: $height 生成缩略图的高度
// 返回: 0 失败 成功时返回生成的图片路径
//==========================================
function makethumb($sourfile,$width=128,$height=128)
{
$imageinfo = $this->getinfo($sourfile);
$sourfile = $this->sourcepath . $sourfile;
$newname = substr($imageinfo["name"], 0, strrpos($imageinfo["name"], ".")) . "_thumb.jpg";
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 0;
break;
}
if (!$img)
return 0;

$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 ($this->tofile)
{
if (file_exists($this->gallerypath . $newname))
unlink($this->gallerypath . $newname);
imagejpeg($new, $this->gallerypath . $newname);
return $this->gallerypath . $newname;
}
else
{
imagejpeg($new);
}
imagedestroy($new);
imagedestroy($img);

}
//==========================================
// 函数: watermark($sourfile, $text)
// 功能: 给图片加水印
// 参数: $sourfile 图片文件名
// 参数: $text 文本数组(包含二个字符串)
// 返回: 1 成功 成功时返回生成的图片路径
//==========================================
function watermark($sourfile, $text)
{
$imageinfo = $this->getinfo($sourfile);
$sourfile = $this->sourcepath . $sourfile;
$newname = substr($imageinfo["name"], 0, strrpos($imageinfo["name"], ".")) . "_mark.jpg";
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 0;
break;
}
if (!$img)
return 0;

$width = ($this->maxwidth > $imageinfo["width"]) ? $imageinfo["width"] : $this->maxwidth;
$height = ($this->maxheight > $imageinfo["height"]) ? $imageinfo["height"] : $this->maxheight;
$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"]);
}
$white = imagecolorallocate($new, 255, 255, 255);
$black = imagecolorallocate($new, 0, 0, 0);
$alpha = imagecolorallocatealpha($new, 230, 230, 230, 40);
//$rectw = max(strlen($text[0]),strlen($text[1]))*7;
imagefilledrectangle($new, 0, $height-26, $width, $height, $alpha);
imagefilledrectangle($new, 13, $height-20, 15, $height-7, $black);
imagettftext($new, 4.9, 0, 20, $height-14, $black, $this->fontname, $text[0]);
imagettftext($new, 4.9, 0, 20, $height-6, $black, $this->fontname, $text[1]);
//*/
if ($this->tofile)
{
if (file_exists($this->gallerypath . $newname))
unlink($this->gallerypath . $newname);
imagejpeg($new, $this->gallerypath . $newname);
return $this->gallerypath . $newname;
}
else
{
imagejpeg($new);
}
imagedestroy($new);
imagedestroy($img);

}
//==========================================
// 函数: displaythumb($file)
// 功能: 显示指定图片的缩略图
// 参数: $file 文件名
// 返回: 0 图片不存在
//==========================================
function displaythumb($file)
{
$thumbname = substr($file, 0, strrpos($file, ".")) . "_thumb.jpg";
$file = $this->gallerypath . $thumbname;
if (!file_exists($file))
return 0;
$html = "";
echo $html;
}
//==========================================
// 函数: displaymark($file)
// 功能: 显示指定图片的水印图
// 参数: $file 文件名
// 返回: 0 图片不存在
//==========================================
function displaymark($file)
{
$markname = substr($file, 0, strrpos($file, ".")) . "_mark.jpg";
$file = $this->gallerypath . $markname;
if (!file_exists($file))
return 0;
$html = "";
echo $html;
}
//==========================================
// 函数: getinfo($file)
// 功能: 返回图像信息
// 参数: $file 文件路径
// 返回: 图片信息数组
//==========================================
function getinfo($file)
{
$file = $this->sourcepath . $file;
$data = getimagesize($file);
$imageinfo["width"] = $data[0];
$imageinfo["height"]= $data[1];
$imageinfo["type"] = $data[2];
$imageinfo["name"] = basename($file);
return $imageinfo;
}

}

?>
----------------------------------
下面是使用方法
这个类使用了一个04b_08__.ttf字体
使用类的时候指定该字体路径即可
-----------------------------------
require_once("gdimage.inc.php");

//header("content-type: image/jpeg");//输出到浏览器的话别忘了打开这个
$img = new gdimage(ib_upload_path, ib_gallery, ib_temp);
$text = array("ice-berg.org","all rights reserved");
$img->maxwidth = $img->maxheight = 300;
$img->tofile = true;
$img->watermark("mm.jpg", $text);
$img->makethumb("mm.jpg");
$img->displaythumb("mm.jpg");
$img->displaymark("mm.jpg");


文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!
相关主题
文章页数:[1] 
Google
热门文章
·Windows下的PHP5.0安装配制详解-PHP教程,PHP安装
·PHP在XP下IIS和Apache2服务器上的安装-PHP教程,PHP应用
·最近忙于FTP,好站多多!有好多好东东哦!不敢独享!-PHP教程,PHP基础
·PHP 5.0 Pear安装方法-PHP教程,PHP安装
·PHP开发利器-PRADO 1.6(4)-PHP教程,PHP应用
·Sun Sparc Solaris 2.6 Apache-1.3.12+MySQL-3.23.5+PHP-3.0.15 安装记-PHP教程,PHP应用
·php5学习笔记(转)-PHP教程,PHP应用
·APACHE安装笔记-PHP教程,PHP安装
·PHP.MVC的模板标签系统(四)-PHP教程,PHP应用
·PHP.MVC的模板标签系统(二)-PHP教程,PHP应用

最新文章
·PHP源码-利用 QQWry.Dat 实现 IP 地址高效检索
·Php高手带路--问题汇总解答[2]
·PHPQQ编程(2):取QQ在线状态
·php5手动最简安装方法
·福利彩票幸运号码自动生成器
·PHP开发利器-PRADO 1.6
·在Apache 服务器上启用PHP支持
·Windows2000_pro下安装Apache+PHP4+My
·php文件上传的实现
·PHP开发框架的现状和展望


 
 


版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!

特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
  打印  刷新  关闭
返回首页 |关于我们 | 联系我们 | 付款方式 | 创业联盟 | 虚拟主机 | 资讯中心 | 友情链接 | 网站地图

版权所有 西部数码(www.west263.com)
CopyRight (c) 2002~2006 west263.com all right reserved.
公司地址:四川成都市万和路90号天象大厦4楼 邮编:610031
电话总机:028-86262244 86263048 86263408 86263960 86264018 86267838
售前咨询:总机转201 202 203 204 206 208
售后服务:总机转211 212 213 214
财务咨询:总机转224 223 传真:028-86264041 财务QQ:点击发送消息给对方635483282
售前咨询QQ:点击发送消息给对方2182518 点击发送消息给对方241975952 点击发送消息给对方275026793 点击发送消息给对方408235859
售后服务QQ:点击发送消息给对方17708515 点击发送消息给对方307742704 点击发送消息给对方287976517 点击发送消息给对方363783715
《中华人民共和国增值电信业务经营许可证》编号:川B2-20030065号