KodExplorer/lib/class/imageThumb.class.php

266 lines
7.8 KiB
PHP
Raw Normal View History

2014-04-01 18:00:42 +00:00
<?php
/**
2016-12-21 08:01:06 +00:00
* 功能:图片处理
2014-04-01 18:00:42 +00:00
* 基本参数:$srcFile,$echoType
*
2016-12-21 08:01:06 +00:00
* eg
* $cm=new imageThumb('1.jpg','file');
*
* $cm->Distortion('dis_bei.jpg',150,200); //生成固定宽高缩略图;
* $cm->Prorate('pro_bei.jpg',150,200); //等比缩略图;附带切割
* $cm->Cut('cut_bei.jpg',150,200); //等比缩略图;多出部分切割
* $cm->BackFill('fill_bei.jpg',150,200); //等比缩略图;多出部分填充
*
* $cm->imgRotate('out.jpg',90); //旋转图片
2014-04-01 18:00:42 +00:00
*/
2016-12-21 08:01:06 +00:00
class imageThumb {
2014-04-01 18:00:42 +00:00
var $srcFile = ''; //原图
2016-12-21 08:01:06 +00:00
var $imgData = ''; //图片信息
2014-04-01 18:00:42 +00:00
var $echoType; //输出图片类型link--不保存为文件file--保存为文件
var $im = ''; //临时变量
var $srcW = ''; //原图宽
var $srcH = ''; //原图高
2016-12-21 08:01:06 +00:00
function __construct($srcFile, $echoType){
2014-04-01 18:00:42 +00:00
$this->srcFile = $srcFile;
$this->echoType = $echoType;
2016-12-21 08:01:06 +00:00
$this->im = self::image($srcFile);
if(!$this->im){
return false;
}
2014-04-01 18:00:42 +00:00
$info = '';
2016-12-21 08:01:06 +00:00
$this->imgData = GetImageSize($srcFile, $info);
$this->srcW = imageSX($this->im);
$this->srcH = imageSY($this->im);
return $this;
}
public static function image($file){
$info = '';
$data = GetImageSize($file, $info);
$img = false;
2014-04-01 18:00:42 +00:00
switch ($data[2]) {
2016-12-21 08:01:06 +00:00
case IMAGETYPE_GIF:
2014-04-01 18:00:42 +00:00
if (!function_exists('imagecreatefromgif')) {
2016-12-21 08:01:06 +00:00
break;
2014-04-01 18:00:42 +00:00
}
2016-12-21 08:01:06 +00:00
$img = imagecreatefromgif($file);
2014-04-01 18:00:42 +00:00
break;
2016-12-21 08:01:06 +00:00
case IMAGETYPE_JPEG:
2014-04-01 18:00:42 +00:00
if (!function_exists('imagecreatefromjpeg')) {
2016-12-21 08:01:06 +00:00
break;
2014-04-01 18:00:42 +00:00
}
2016-12-21 08:01:06 +00:00
$img = imagecreatefromjpeg($file);
2014-04-01 18:00:42 +00:00
break;
2016-12-21 08:01:06 +00:00
case IMAGETYPE_PNG:
if (!function_exists('imagecreatefrompng')) {
break;
}
$img = imagecreatefrompng($file);
imagesavealpha($img,true);
2014-04-01 18:00:42 +00:00
break;
2016-12-21 08:01:06 +00:00
case IMAGETYPE_BMP:
$img = imagecreatefrombmp($file);
break;
default:break;
}
return $img;
}
public static function imageSize($file){
$img = self::image($file);
$result = false;
if($img){
$result = array('width'=>imageSX($img),"height"=>imageSY($img));
imageDestroy($img);
}
return $result;
}
2014-04-01 18:00:42 +00:00
// 生成扭曲型缩图
2016-12-21 08:01:06 +00:00
function distortion($toFile, $toW, $toH){
$cImg = $this->creatImage($this->im, $toW, $toH, 0, 0, 0, 0, $this->srcW, $this->srcH);
return $this->echoImage($cImg, $toFile);
2014-04-01 18:00:42 +00:00
}
// 生成按比例缩放的缩图
2016-12-21 08:01:06 +00:00
function prorate($toFile, $toW, $toH){
2014-04-01 18:00:42 +00:00
$toWH = $toW / $toH;
$srcWH = $this->srcW / $this->srcH;
if ($toWH<=$srcWH) {
$ftoW = $toW;
$ftoH = $ftoW * ($this->srcH / $this->srcW);
} else {
$ftoH = $toH;
$ftoW = $ftoH * ($this->srcW / $this->srcH);
}
if ($this->srcW > $toW || $this->srcH > $toH) {
2016-12-21 08:01:06 +00:00
$cImg = $this->creatImage($this->im, $ftoW, $ftoH, 0, 0, 0, 0, $this->srcW, $this->srcH);
return $this->echoImage($cImg, $toFile);
2014-04-01 18:00:42 +00:00
} else {
2016-12-21 08:01:06 +00:00
$cImg = $this->creatImage($this->im, $this->srcW, $this->srcH, 0, 0, 0, 0, $this->srcW, $this->srcH);
return $this->echoImage($cImg, $toFile);
2014-04-01 18:00:42 +00:00
}
}
// 生成最小裁剪后的缩图
2016-12-21 08:01:06 +00:00
function cut($toFile, $toW, $toH){
2014-04-01 18:00:42 +00:00
$toWH = $toW / $toH;
$srcWH = $this->srcW / $this->srcH;
if ($toWH<=$srcWH) {
$ctoH = $toH;
$ctoW = $ctoH * ($this->srcW / $this->srcH);
} else {
$ctoW = $toW;
$ctoH = $ctoW * ($this->srcH / $this->srcW);
}
2016-12-21 08:01:06 +00:00
$allImg = $this->creatImage($this->im, $ctoW, $ctoH, 0, 0, 0, 0, $this->srcW, $this->srcH);
$cImg = $this->creatImage($allImg, $toW, $toH, 0, 0, ($ctoW - $toW) / 2, ($ctoH - $toH) / 2, $toW, $toH);
imageDestroy($allImg);
return $this->echoImage($cImg, $toFile);
2014-04-01 18:00:42 +00:00
}
// 生成背景填充的缩图,默认用白色填充剩余空间,传入$isAlpha为真时用透明色填充
2016-12-21 08:01:06 +00:00
function backFill($toFile, $toW, $toH,$isAlpha=false,$red=255, $green=255, $blue=255){
2014-04-01 18:00:42 +00:00
$toWH = $toW / $toH;
$srcWH = $this->srcW / $this->srcH;
if ($toWH<=$srcWH) {
$ftoW = $toW;
$ftoH = $ftoW * ($this->srcH / $this->srcW);
} else {
$ftoH = $toH;
$ftoW = $ftoH * ($this->srcW / $this->srcH);
}
if (function_exists('imagecreatetruecolor')) {
2016-12-21 08:01:06 +00:00
@$cImg = imageCreateTrueColor($toW, $toH);
2014-04-01 18:00:42 +00:00
if (!$cImg) {
2016-12-21 08:01:06 +00:00
$cImg = imageCreate($toW, $toH);
2014-04-01 18:00:42 +00:00
}
} else {
2016-12-21 08:01:06 +00:00
$cImg = imageCreate($toW, $toH);
2014-04-01 18:00:42 +00:00
}
$fromTop = ($toH - $ftoH)/2;//从正中间填充
$backcolor = imagecolorallocate($cImg,$red,$green, $blue); //填充的背景颜色
if ($isAlpha){//填充透明色
2016-12-21 08:01:06 +00:00
$backcolor=imageColorTransparent($cImg,$backcolor);
2014-04-01 18:00:42 +00:00
$fromTop = $toH - $ftoH;//从底部填充
}
2016-12-21 08:01:06 +00:00
imageFilledRectangle($cImg, 0, 0, $toW, $toH, $backcolor);
2014-04-01 18:00:42 +00:00
if ($this->srcW > $toW || $this->srcH > $toH) {
2016-12-21 08:01:06 +00:00
$proImg = $this->creatImage($this->im, $ftoW, $ftoH, 0, 0, 0, 0, $this->srcW, $this->srcH);
2014-04-01 18:00:42 +00:00
if ($ftoW < $toW) {
2016-12-21 08:01:06 +00:00
imageCopy($cImg, $proImg, ($toW - $ftoW) / 2, 0, 0, 0, $ftoW, $ftoH);
2014-04-01 18:00:42 +00:00
} else if ($ftoH < $toH) {
2016-12-21 08:01:06 +00:00
imageCopy($cImg, $proImg, 0, $fromTop, 0, 0, $ftoW, $ftoH);
2014-04-01 18:00:42 +00:00
} else {
2016-12-21 08:01:06 +00:00
imageCopy($cImg, $proImg, 0, 0, 0, 0, $ftoW, $ftoH);
2014-04-01 18:00:42 +00:00
}
} else {
2016-12-21 08:01:06 +00:00
imageCopyMerge($cImg, $this->im, ($toW - $ftoW) / 2,$fromTop, 0, 0, $ftoW, $ftoH, 100);
2014-04-01 18:00:42 +00:00
}
2016-12-21 08:01:06 +00:00
return $this->echoImage($cImg, $toFile);
2014-04-01 18:00:42 +00:00
}
2016-12-21 08:01:06 +00:00
function creatImage($img, $creatW, $creatH, $dstX, $dstY, $srcX, $srcY, $srcImgW, $srcImgH){
2014-04-01 18:00:42 +00:00
if (function_exists('imagecreatetruecolor')) {
@$creatImg = ImageCreateTrueColor($creatW, $creatH);
2016-12-21 08:01:06 +00:00
@imagealphablending($creatImg,false);//是不合并颜色,直接用$img图像颜色替换,包括透明色;
@imagesavealpha($creatImg,true);//不要丢了$thumb图像的透明色;
if ($creatImg){
imageCopyResampled($creatImg, $img, $dstX, $dstY, $srcX, $srcY, $creatW, $creatH, $srcImgW, $srcImgH);
}else {
2014-04-01 18:00:42 +00:00
$creatImg = ImageCreate($creatW, $creatH);
2016-12-21 08:01:06 +00:00
imageCopyResized($creatImg, $img, $dstX, $dstY, $srcX, $srcY, $creatW, $creatH, $srcImgW, $srcImgH);
2014-04-01 18:00:42 +00:00
}
} else {
$creatImg = ImageCreate($creatW, $creatH);
2016-12-21 08:01:06 +00:00
imageCopyResized($creatImg, $img, $dstX, $dstY, $srcX, $srcY, $creatW, $creatH, $srcImgW, $srcImgH);
2014-04-01 18:00:42 +00:00
}
return $creatImg;
2016-12-21 08:01:06 +00:00
}
// Rotate($toFile, 90);
public function imgRotate($toFile,$degree) {
if (!$this->im ||
$degree % 360 === 0 ||
!function_exists('imageRotate')) {
return false;
}
$rotate = imageRotate($this->im,360-$degree,0);
$result = false;
switch ($this->imgData[2]) {
case IMAGETYPE_GIF:
$result = imagegif($rotate, $toFile);
break;
case IMAGETYPE_JPEG:
$result = imagejpeg($rotate, $toFile,100);//压缩质量
break;
case IMAGETYPE_PNG:
$result = imagePNG($rotate, $toFile);
break;
default:break;
}
imageDestroy($rotate);
imageDestroy($this->im);
return $result;
}
2014-04-01 18:00:42 +00:00
// 输出图片link---只输出不保存文件。file--保存为文件
2016-12-21 08:01:06 +00:00
function echoImage($img, $toFile){
$result = false;
2014-04-01 18:00:42 +00:00
switch ($this->echoType) {
2016-12-21 08:01:06 +00:00
case 'link':$result = imagePNG($img);break;
case 'file':$result = imagePNG($img, $toFile);break;
2014-04-01 18:00:42 +00:00
//return ImageJpeg($img, $to_File);
2016-12-21 08:01:06 +00:00
}
imageDestroy($img);
imageDestroy($this->im);
return $result;
2014-04-01 18:00:42 +00:00
}
}
2016-12-21 08:01:06 +00:00
if(!function_exists('imagecreatefrombmp')){
function imagecreatefrombmp( $filename ){
$file = fopen( $filename, "rb" );
$read = fread( $file, 10 );
while( !feof( $file ) && $read != "" ){
$read .= fread( $file, 1024 );
}
$temp = unpack( "H*", $read );
$hex = $temp[1];
$header = substr( $hex, 0, 104 );
$body = str_split( substr( $hex, 108 ), 6 );
if( substr( $header, 0, 4 ) == "424d" ){
$header = substr( $header, 4 );
// Remove some stuff?
$header = substr( $header, 32 );
// Get the width
$width = hexdec( substr( $header, 0, 2 ) );
// Remove some stuff?
$header = substr( $header, 8 );
// Get the height
$height = hexdec( substr( $header, 0, 2 ) );
unset( $header );
}
$x = 0;
$y = 1;
$image = imagecreatetruecolor( $width, $height );
foreach( $body as $rgb ){
$r = hexdec( substr( $rgb, 4, 2 ) );
$g = hexdec( substr( $rgb, 2, 2 ) );
$b = hexdec( substr( $rgb, 0, 2 ) );
$color = imagecolorallocate( $image, $r, $g, $b );
imagesetpixel( $image, $x, $height-$y, $color );
$x++;
if( $x >= $width ){
$x = 0;
$y++;
}
}
return $image;
}
}