图片处理

<?
//图片类
class image{
private $img_width;
private $img_height;
private $img_quality;
private $img_pct;
private $imglink = array();
private $img_dwidth;
private $img_dheight;
private $img_type;
private $img_fileimgdir;

function __construct($imgwidth=240, $imgheight=180, $imgquality=80, $imgpct=0){
$this->img_width = $imgwidth;
$this->img_height = $imgheight;
$this->img_quality = $imgquality;
$this->img_pct = $imgpct;
}
function image(){
$this->__construct();
}

function set($imgwidth=240, $imgheight=180, $imgquality=80, $imgpct=0){
$this->img_width = $imgwidth;
$this->img_height = $imgheight;
$this->img_quality = $imgquality;
$this->img_pct = $imgpct;
}
//创建图片画布大小及背景色
function imgcreate($bj = 'ci', $rgb = '', $alpha = 0, $truecolor = true){
$this->imglink[$bj] = $truecolor ? imagecreatetruecolor($this->img_width, $this->img_height): imagecreate($this->img_width, $this->img_height);
if($rgb != '') {
$color = $this->setcolor($this->imglink[$bj], $rgb, $alpha);
imagefill($this->imglink[$bj], 0, 0, $color);
}
return $this->imglink[$bj];
}
//设置颜色
function setcolor($imglink, $rgb, $alpha = 0){
return imagecolorallocatealpha($imglink,$rgb[0],$rgb[1],$rgb[2],$alpha);
}

//获得图片的大小和类型
function getimgtype($filedir = '', $bj = 'oi'){
$imgsize = @getimagesize($filedir) or exit('文件不存在2');
$this->img_dwidth = $imgsize[0];
$this->img_dheight = $imgsize[1];
$this->img_fileimgdir = file_exists($filedir) ? $filedir : exit("文件不存在3");
switch($imgsize[2])
{
case 1:
$this->img_type = 'gif';
break;
case 2:
$this->img_type = 'jpeg';
break;
case 3:
$this->img_type = 'png';
break;
default :
$this->img_type = '';
exit("无效的图片类型");
}

if(($this->img_dwidth == 0 || $this->img_dheight == 0) && $this->imglink[$bj] != '')
{
$this->img_dwidth = imageSx($this->imglink[$bj]);
$this->img_dheight = imageSy($this->imglink[$bj]);
}
//return $this->img_type;

}

//打开图片
function openimg($bj = 'oi'){
$type = $this->img_type;
switch($type)
{
case 'gif':
$this->imglink[$bj] = imagecreatefromgif($this->img_fileimgdir);
break;
case 'jpeg':
$this->imglink[$bj] = imagecreatefromjpeg($this->img_fileimgdir);
break;
case 'png':
$this->imglink[$bj] = imagecreatefrompng($this->img_fileimgdir);
break;
default :
break;
}
//return $this->imglink[$bj];
}

//缩放图片
function thumb($filedir = '', $bj = 'oi', $cj = 'ci',$maxwidth = 240, $maxheight = 180){
$this->getimgtype($filedir, $bj);
$this->openimg($bj);
if($maxwidth > 0 && $maxheight > 0)
{
if($maxwidth/$maxheight < $this->img_dwidth/$this->img_dheight && $maxheight >= $this->img_dheight)
{
$width = $maxheight/$this->img_dheight * $this->img_dwidth;
$height = $maxheight;
}
elseif($maxwidth/$maxheight > $this->img_dwidth / $this->img_dheight && $maxwidth>=$this->img_dwidth)
{
$height = $maxwidth / $this->img_dwidth * $this->img_dheight;
$width = $maxwidth;
}
else
{
$width = $maxwidth;
$height = $maxheight;
}
$this->img_width = $width;
$this->img_height = $height;
//echo $height." ".$width." ".$this->img_dwidth." ".$this->img_dheight;

}

if($height != $this->img_dwidth && $width != $this->img_dheight){
$ox = $oy = 0;
if($this->img_type == 'gif')
$this->imgcreate($cj, NUlL, 0, false);
else
$this->imgcreate($cj, NUlL, 0, true);

if(function_exists('imagecopyresampled'))
{
//if(empty($linktow)) echo 1; elseif(empty($linkone)) echo 2;
imagecopyresampled($this->imglink[$cj], $this->imglink[$bj], 0, 0, $ox, $oy, $width, $height, $this->img_dwidth, $this->img_dheight);
}
else
{
imagecopyresized($this->imglink[$cj], $this->imglink[$bj], 0, 0, $ox, $oy, $width, $height, $this->img_dwidth, $this->img_dheight);
}
//$createimgfun = 'image'.$this->img_type;
//$createimgfun($this->imglink[$cj],filedir);
unlink($this->img_fileimgdir);
return $this->conimg($cj);
}
}
//字体文件
//给图片添加水印(文字或是图片水印)
function water($filedir = '', $bj = 'oi', $str = 'www.hellokey.org', $font = 14, $tori = 1){
$this->getimgtype($filedir, $bj); //获得图片
$this->openimg($bj); //打开图片资源
switch($tori){
case 1:
$ttf = '1.ttf';
break;
case 2:
$ttf = '2.ttf';
break;
case 3:
$ttf = '3.ttf';
break;
case 4:
$ttf = 'heiti.ttf';
break;
default:
$ttf = '1.ttf';
break;
}

$slen = strlen($str);
$fontcode = false; //字符编码
for($i=0;$i<$slen;$i++)
{
if(ord($str{$i}) > 0x80)
{
$str = iconv('gbk','utf-8',$str);
$ttf = 'simsun.ttc';
$fontcode = true;
break;
}
}
//计算字体宽度
$fontattribute = imagettfbbox($font,0,$ttf,$str);
$fontwidth = $fontattribute[2] - $fontattribute[0];
$fontheight = $fontarrtibute[1] - $fontattribute[7];

/*$len = 0;$fonttype = false;
for($i=0;$i < $slen;$i++)
{
if(ord($str{$i}) > 0x80){
$i++;
$len++;
$fonttype = true;
continue;
}
$len++;
}
//生成的字体图片长
switch($fonttype){
case true;
$wordlen = 16 * $len;
break;
case false;
$wordlen = 9 * $slen;
break;
}*/
//写入字体
if($this->img_dwidth < $fontwidth) {
$expandsrcw = $fontwidth + 5; //宽
$expandsrch = ceil($fontwidth/$this->img_dwidth * $this->img_dheight); //高

if($this->img_type == 'gif'){
$this->imglink['wc'] = imagecreate($expandsrcw,$expandsrch); //创建图片画布
}
else
{
$this->imglink['wc'] = imagecreatetruecolor($expandsrcw,$expandsrch);//创建图片画布

}
/*($expandsrch - $fontheight)*9.005*/
if(function_exists('imagecopyresampled'))
{
imagecopyresampled($this->imglink['wc'], $this->imglink[$bj], 0, 0, 0, 0, $expandsrcw, $expandsrch, $this->img_dwidth, $this->img_dheight);
imagettftext($this->imglink['wc'], $font, 0, 0, 20, imagecolorallocate($this->imglink['wc'], 0, 0, 0), $ttf, $str);
imagecopyresampled($this->imglink[$bj], $this->imglink['wc'], 0,0,0,0, $this->img_dwidth,$this->img_dheight,$expandsrcw,$expandsrch);
}
else
{
imagecopyresized($this->imglink['wc'],$this->imglink[$bj],0,0,0,0,$expandsrcw,$expandsrch,$this->img_dwidth,$this->img_dheight);
imagettftext($this->imglink['wc'], $font, 0, 0, 20, imagecolorallocate($this->imglink['wc'], 0, 0, 0), $ttf, $str);
imagecopyresized($this->imglink[$bj], $this->imglink['wc'], 0,0,0,0, $this->img_dwidth,$this->img_dheight,$expandsrcw,$expandsrch);
}
}
else
{
//原图片大于字体大小,判断在图片中的位置居于右下角
if(($this->img_dwidth - $fontwidth - 5) >= 0){ $px = $this->img_dwidth - $fontwidth - 5; }else{ $px = ($this->img_dwidth - $fontwidth)*9.5;}
if(($this->img_dheight - $fontheight - 5) >=0){ $py = $this->img_dheight - $fontheight - 5; }else{ $py = ($this->img_dheight - $fontheight)*9.5;}
imagettftext($this->imglink[$bj],$font, 0, $px, $py, imagecolorallocate($this->imglink[$bj], 0, 0, 0), $ttf, $str);

}

unlink($this->img_fileimgdir);
return $this->conimg($bj);
//return $fontwidth.'<br />'.($this->img_dheight - $fontheight - 5);

}

//保存或是输出图片
function conimg($bj = 'oi'){
$newname = false;
$filepath = '';
if($this->img_fileimgdir == '')
{
header("Content-type: image/jpeg");
imagejpeg($this->imglink[$bj]);
}else
{
$filepath = dirname($this->img_fileimgdir);
$newname = $filepath.'/'.time().rand(1,50).'.'.$this->img_type;
$imgfun = 'image'.$this->img_type;
if($this->img_type == 'jpeg')
{
$imgfun($this->imglink[$bj], $newname, $this->img_quality);
}else
{
$imgfun($this->imglink[$bj], $newname);
}

}
$this->cancel();
return $newname ? $newname : false;
}

//注销变量
function cancel(){
if($this->imglink != '')
{
foreach($this->imglink as $value)
{
imagedestroy($value);
}
$this->imglink = NULL;
}
}

//php5注销
function __destruct(){
if($this->imglink != '')
{
foreach($this->imglink as $value)
{
imagedestroy($value);
}
}
}
}

posted on 2013-08-29 15:43  木子牛  阅读(200)  评论(0编辑  收藏  举报

导航