KodExplorer/app/kod/ImageThumb.class.php

291 lines
8.6 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
2017-08-23 19:40:27 +00:00
* $cm=new ImageThumb('1.jpg','file');
2016-12-21 08:01:06 +00:00
*
* $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
*/
2017-08-23 19:40:27 +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;
2017-08-26 09:16:57 +00:00
//var_dump($data,$file,memory_get_usage()-$GLOBALS['config']['appMemoryStart']);
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;
2017-08-26 09:16:57 +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;
}
2017-08-26 09:16:57 +00:00
$img = @imagecreatefrompng($file);
2016-12-21 08:01:06 +00:00
imagesavealpha($img,true);
2014-04-01 18:00:42 +00:00
break;
2017-08-26 09:16:57 +00:00
case IMAGETYPE_XBM:
$img = imagecreatefromxbm($file);
break;
case IMAGETYPE_WBMP:
$img = imagecreatefromwbmp($file);
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){
2017-08-26 09:16:57 +00:00
$size = GetImageSize($file);
if(!$size){
return false;
2016-12-21 08:01:06 +00:00
}
2017-08-26 09:16:57 +00:00
return array('width'=>$size[0],"height"=>$size[1]);
2016-12-21 08:01:06 +00:00
}
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){
2017-08-23 19:40:27 +00:00
if(!$img) return false;
2017-01-05 10:47:25 +00:00
ob_get_clean();
2016-12-21 08:01:06 +00:00
$result = false;
2014-04-01 18:00:42 +00:00
switch ($this->echoType) {
2017-08-23 19:40:27 +00:00
case 'link':$result = imagePNG($img);break;
2016-12-21 08:01:06 +00:00
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
2017-09-29 10:22:02 +00:00
if(!function_exists('imageflip')){
/**
* Flip (mirror) an image left to right.
*
* @param image resource
* @param x int
* @param y int
* @param width int
* @param height int
* @return bool
* @require PHP 3.0.7 (function_exists), GD1
*/
define('IMG_FLIP_HORIZONTAL', 0);
define('IMG_FLIP_VERTICAL', 1);
define('IMG_FLIP_BOTH', 2);
function imageflip($image, $mode) {
switch ($mode) {
case IMG_FLIP_HORIZONTAL: {
$max_x = imagesx($image) - 1;
$half_x = $max_x / 2;
$sy = imagesy($image);
$temp_image = imageistruecolor($image)? imagecreatetruecolor(1, $sy): imagecreate(1, $sy);
for ($x = 0; $x < $half_x; ++$x) {
imagecopy($temp_image, $image, 0, 0, $x, 0, 1, $sy);
imagecopy($image, $image, $x, 0, $max_x - $x, 0, 1, $sy);
imagecopy($image, $temp_image, $max_x - $x, 0, 0, 0, 1, $sy);
}
break;
}
case IMG_FLIP_VERTICAL: {
$sx = imagesx($image);
$max_y = imagesy($image) - 1;
$half_y = $max_y / 2;
$temp_image = imageistruecolor($image)? imagecreatetruecolor($sx, 1): imagecreate($sx, 1);
for ($y = 0; $y < $half_y; ++$y) {
imagecopy($temp_image, $image, 0, 0, 0, $y, $sx, 1);
imagecopy($image, $image, 0, $y, 0, $max_y - $y, $sx, 1);
imagecopy($image, $temp_image, 0, $max_y - $y, 0, 0, $sx, 1);
}
break;
}
case IMG_FLIP_BOTH: {
$sx = imagesx($image);
$sy = imagesy($image);
$temp_image = imagerotate($image, 180, 0);
imagecopy($image, $temp_image, 0, 0, 0, 0, $sx, $sy);
break;
}
default: {
return;
}
}
imagedestroy($temp_image);
}
}
2016-12-21 08:01:06 +00:00
if(!function_exists('imagecreatefrombmp')){
function imagecreatefrombmp( $filename ){
2017-08-23 19:40:27 +00:00
return imageGdBMP::load($filename);
2016-12-21 08:01:06 +00:00
}
}