소스 검색

Add Image Library

Zhu Jinhui 7 년 전
부모
커밋
ad35ffc80b
3개의 변경된 파일567개의 추가작업 그리고 0개의 파일을 삭제
  1. 72 0
      src/Library/Cc3des.php
  2. 310 0
      src/Library/Image.php
  3. 185 0
      src/Library/ImageFilter.php

+ 72 - 0
src/Library/Cc3des.php

@@ -0,0 +1,72 @@
+<?php
+/**
+*
+* PHP版3DES加解密类
+*
+* 可与java的3DES(DESede)加密方式兼容
+*
+* @Author:蓝凤(ilanfeng.com)
+*
+* @version: V0.1 2011.02.18
+*
+*/
+namespace Qii\Library;
+
+class Cc3des{
+ 
+    //加密的时候只用替换key就行了,ecb模式不需要提供iv值
+    public $key    = "0123456789QWEQWEEWQQ1234";
+    public $iv    = "33889955"; //like java: private static byte[] myIV = { 50, 51, 52, 53, 54, 55, 56, 57 };
+ 
+    //解密
+    public function decrypt($string) {
+        $td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_ECB, '');
+        srand();
+        $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
+        $key = substr($this->key, 0, mcrypt_enc_get_key_size($td));
+        mcrypt_generic_init($td, $key, $iv);
+        $value = @pack("H*", $string);
+        $ret = trim(mdecrypt_generic($td, $value));
+        // 去掉多余的补位
+        $ret = $this->pkcs5_unpad($ret);
+        mcrypt_generic_deinit($td);
+        mcrypt_module_close($td);
+        return $ret;
+    }
+ 
+    //加密
+    public function encrypt($value) {
+        $td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_ECB, '');
+        srand();
+        $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
+        $key = substr($this->key, 0, mcrypt_enc_get_key_size($td));
+        mcrypt_generic_init($td, $key, $iv);
+        $value = $this->pkcs5_pad($value, mcrypt_get_block_size(MCRYPT_3DES, 'ecb'));
+        $ret = mcrypt_generic($td, $value);
+        mcrypt_generic_deinit($td);
+        mcrypt_module_close($td);
+        return strtoupper(bin2hex($ret));
+    }
+ 
+    /*
+     * 位数补齐
+     */
+    private function pkcs5_pad($text, $blocksize) {
+        $pad = $blocksize - (strlen($text) % $blocksize);
+        return $text . str_repeat(chr($pad), $pad);
+    }
+     
+    /*
+     * 去除补位
+     */
+    private function pkcs5_unpad($text) {
+        $pad = ord($text{strlen($text) - 1});
+        if($pad > strlen($text)) {
+            return false;
+        }
+        if(strspn($text, chr($pad), strlen($text) - $pad) != $pad) {
+            return false;
+        }
+        return substr($text, 0, -1 * $pad);
+    }
+}

+ 310 - 0
src/Library/Image.php

@@ -0,0 +1,310 @@
+<?php
+namespace Qii\Library;
+
+class Image
+{
+	public $config = ['width' => 100, 'height' => 100];
+	public $error = [];
+
+	public function __construct($config)
+	{
+		if(is_array($config)){
+			$this->config = array_merge($this->config, $config);
+		}
+	}
+
+	public function setError($key, $val)
+	{
+		$this->error[$key] = $val;
+	}
+
+
+	/**
+	 * 根据配置文件批量缩放图片
+	 * @param $images 图片路径
+	 * @param $config [{width: 100, height: 100}, ...]
+	 * @return array
+	 */
+	public function autoResize($images, $config)
+	{
+		$config = array_merge($this->config, $config);
+		$data = array();
+		if (is_array($images)) {
+			foreach ($images AS $image) {
+				$data[] = $this->autoResize($image, $config);
+			}
+		} else {
+			$thumbs = array();
+			foreach ($config['size'] AS $key => $value) {
+				$pathInfo = pathinfo($images);
+				$thumbs[$key] = $small = $pathInfo['dirname'] . '/' . $pathInfo['filename'] . '.' . $key . '.' . $pathInfo['extension'];
+				$this->cutScale($images, $small, $config['size'][$key]['width'], $config['size'][$key]['height']);
+				$this->resizeSamll($small, $small, $config['size'][$key]['width']);
+			}
+			return $thumbs;
+		}
+		return $data;
+	}
+
+	/**
+	 * 图片缩放,等比缩放
+	 *
+	 * @param String $bigImg 原图
+	 * @param String $smallImg 缩放以后的图片
+	 * @param Int $width 宽度
+	 * @return Bool
+	 */
+	public function resizeSmall($bigImg, $smallImg, $width = 392)
+	{
+			// 图片路径
+		if (!file_exists($bigImg)) {
+			$this->setError(__METHOD__, $bigImg . "文件不存在");
+			return false;
+		} else {
+			ini_set("memory_limit", "128M");
+			$fileName = $bigImg;
+			// 获取原图片的尺寸
+			list($widthOrig, $heightOrig) = getimagesize($fileName);
+			//根据比例,计算新图片的尺寸
+			$height = ($width / $widthOrig) * $heightOrig;
+			//新建一个真彩色图像
+			$destImage = imagecreate($width, $height);
+			//从 JPEG 文件或 URL 新建一图像
+			$imageInfo = getimagesize($bigImg);//获取大图信息
+			switch ($imageInfo[2]) {//判断图像类型
+				case 1:
+					$image = imagecreatefromgif($bigImg);
+					break;
+				case 2:
+					$image = imagecreatefromjpeg($bigImg);
+					break;
+				case 3:
+					$image = imagecreatefrompng($bigImg);
+					$color = imagecolorallocate($image, 255, 255, 255);
+					imagecolortransparent($image, $color);
+					imagefill($image, 0, 0, $color);
+					break;
+				default:
+					$image = imagecreatefromjpeg($fileName);
+					break;
+			}
+			//重采样拷贝部分图像并调整大小
+			imagecopyresampled($destImage, $image, 0, 0, 0, 0, $width, $height, $widthOrig, $heightOrig);
+			// 将图片保存到服务器
+			imagejpeg($destImage, $smallImg, 100);
+			//销毁图片,释放内存
+			imagedestroy($destImage);
+			return true;
+		}
+	}
+	/**
+	 * 缩放并按给定长宽比裁切图片
+	 * @param string $bigImg 原图路径
+	 * @param string $smallImg 缩放以后的文件路径
+	 * @param int $width 缩放宽度
+	 * @param int $height 缩放高度
+	 */
+	public function cutScale($bigImg, $smallImg = 'test.jpg', $width = 90, $height = 130)
+	{	
+		if (!file_exists($bigImg)) {
+			$this->setError(__METHOD__, $bigImg . "文件不存在");
+			return false;
+		}
+		ini_set("memory_limit", "128M");
+		//大图文件地址,缩略宽,缩略高,小图地址
+		$image = getimagesize($bigImg);//获取大图信息
+		switch ($image[2]) {//判断图像类型
+			case 1:
+				$im = imagecreatefromgif($bigImg);
+				break;
+			case 2:
+				$im = imagecreatefromjpeg($bigImg);
+				break;
+			case 3:
+				$im = imagecreatefrompng($bigImg);
+				$color = imagecolorallocate($im, 255, 255, 255);
+				imagecolortransparent($im, $color);
+				imagefill($im, 0, 0, $color);
+				break;
+		}
+
+		$srcW = imagesx($im);//获取大图宽
+		$srcH = imagesy($im);//获取大图高
+
+		//计算比例
+		//检查图片高度和宽度
+		$srcScale = sprintf("%.2f", ($srcW / $srcH));//原图比例
+		$destScale = sprintf("%.2f", ($width / $height));//缩略图比例
+
+		//echo "<p>原始比例:".$srcScale.";目标比例".$destScale."</p>";
+		if ($srcScale > $destScale) {
+			//说明高度不够,就以高度为准
+			$myH = $srcH;
+			$myW = intval($srcH * ($width / $height));
+			//获取开始位置
+			$myY = 0;
+			$myX = intval(($srcW - $myW) / 2);
+		} elseif ($srcScale < $destScale) {
+			//宽度不够就以宽度为准
+			$myW = $srcW;
+			$myH = intval($srcW * ($height / $width));
+			$myX = 0;
+			$myY = intval(($srcH - $myH) / 2);
+		} else {
+			if ($srcW > $srcH) {
+				//echo "<p>case 1:</p>";
+				$myH = $srcH;
+				$myW = intval($srcH * ($width / $height));
+				//获取开始位置
+				$myY = 0;
+				$myX = intval(($srcW - $myW) / 2);
+			}
+			if ($srcW < $srcH) {
+				//echo "case 2";
+				$myW = $srcW;
+				$myH = intval($srcW * ($height / $width));
+				$myX = 0;
+				$myY = intval(($srcH - $myH) / 2);
+			}
+		}
+		if ($srcW == $srcH) {
+			$myW = intval($srcH * ($width / $height));
+			$myH = $srcH;
+
+			$myX = intval(($srcW - $myW) / 2);
+			$myY = 0;
+		}
+		//echo "<p>SW:" . $srcW ."W:" .$myW . "</p><p>X".$myX."</p><p>SH".$srcH.";H:" . $myH ."<p>Y".$myY."</p>";
+		//从中间截取图片
+		if($image[2] == 3)
+		{
+			$tn = imagecreate($myW, $myH);//创建小图
+		}
+		else
+		{
+			$tn = imagecreatetruecolor($myW, $myH);
+		}
+		
+		imagecopy($tn, $im, 0, 0, $myX, $myY, $myW, $myH);
+		if($image[2] == 3)
+		{
+			imagepng($tn, $smallImg, 9);
+		}else{
+			imagejpeg($tn, $smallImg, 100);//输出图像
+		}
+		
+		imagedestroy($im);
+	}
+	/**
+	 *
+	 * 剪切圖片到指定大小
+	 * @param String $bigImg 原始圖片
+	 * @param Int $width 寬
+	 * @param Int $height高
+	 * @param String $smallImg 縮放後保存的圖片
+	 */
+	public function cutSmall($bigImg, $smallImg, $width, $height)
+	{
+		if (!file_exists($bigImg)) {
+			$this->setError(__METHOD__, $bigImg . "文件不存在");
+			return false;
+		}
+		ini_set("memory_limit", "128M");
+		//大图文件地址,缩略宽,缩略高,小图地址
+		$imgage = getimagesize($bigImg);//获取大图信息
+		switch ($imgage[2]) {//判断图像类型
+			case 1:
+				$im = imagecreatefromgif($bigImg);
+				break;
+			case 2:
+				$im = imagecreatefromjpeg($bigImg);
+				break;
+			case 3:
+				$im = imagecreatefrompng($bigImg);
+				break;
+		}
+		$srcW = imagesx($im);//获取大图宽
+		$srcH = imagesy($im);//获取大图高
+		if($image[2] == 3)
+		{
+			$tn = imagecrate($width, $height);//创建小图
+		}
+		else
+		{
+			$tn = imagecreatetruecolor($width, $height);
+		}
+		
+		imagecopy($tn, $im, 0, 0, 0, 0, $width, $height);
+		if($image[2] == 3)
+		{
+			imagepng($tn, $smallImg, 9);
+		}else{
+			imagejpeg($tn, $smallImg, 100);//输出图像
+		}
+		imagedestroy($im);
+	}
+
+
+	/**
+	 * 按比例缩放图片并限制图片的最大宽度或高度
+	 * @param string $bigImg 原图地址
+	 * @param string $smallImg 缩略图地址
+	 * @param int $maxValue 最大宽高
+	 */
+	public function resizeMaxSize($bigImg, $smallImg, $maxValue = 392)
+	{
+		// 图片路径
+		if (!file_exists($bigImg)) {
+			$this->setError(__METHOD__, $bigImg . "文件不存在");
+			return false;
+		}
+		ini_set("memory_limit", "128M");
+		$fileName = $bigImg;
+		// 获取原图片的尺寸
+		list($widthOrig, $heightOrig) = getimagesize($fileName);
+		$width = $widthOrig;
+		$height = $heightOrig;
+		//根据比例,计算新图片的尺寸
+		if ($widthOrig > $heightOrig) {
+			if ($widthOrig > $maxValue) {
+				$width = $maxValue;
+				$height = ($maxValue / $widthOrig) * $heightOrig;
+			}
+		} else {
+			if ($heightOrig > $maxValue) {
+				$height = $maxValue;
+				$width = ($maxValue / $heightOrig) * $widthOrig;
+			}
+		}
+		//$height = ($width / $widthOrig) * $heightOrig;
+
+		//新建一个真彩色图像
+		$destImage = imagecreate($width, $height);
+		//从 JPEG 文件或 URL 新建一图像
+		$imageInfo = getimagesize($bigImg);//获取大图信息
+		switch ($imageInfo[2]) {//判断图像类型
+			case 1:
+				$image = imagecreatefromgif($bigImg);
+				break;
+			case 2:
+				$image = imagecreatefromjpeg($bigImg);
+				break;
+			case 3:
+				$image = imagecreatefrompng($bigImg);
+				$color = imagecolorallocate($image, 255, 255, 255);
+				imagecolortransparent($image, $color);
+				imagefill($image, 0, 0, $color);
+				break;
+			default:
+				$image = imagecreatefromjpeg($fileName);
+				break;
+		}
+		//重采样拷贝部分图像并调整大小
+		imagecopyresampled($destImage, $image, 0, 0, 0, 0, $width, $height, $widthOrig, $heightOrig);
+		// 将图片保存到服务器
+		imagejpeg($destImage, $smallImg, 100);
+		//销毁图片,释放内存
+		imagedestroy($destImage);
+		return true;
+	}
+}

+ 185 - 0
src/Library/ImageFilter.php

@@ -0,0 +1,185 @@
+<?php
+//
+// +-----------------------------------+
+// |        Image Filter v 1.0         |
+// |      http://www.SysTurn.com       |
+// +-----------------------------------+
+//
+//
+//   This program is free software; you can redistribute it and/or modify
+//   it under the terms of the ISLAMIC RULES and GNU Lesser General Public
+//   License either version 2, or (at your option) any later version.
+//
+//   ISLAMIC RULES should be followed and respected if they differ
+//   than terms of the GNU LESSER GENERAL PUBLIC LICENSE
+//
+//   This program is distributed in the hope that it will be useful,
+//   but WITHOUT ANY WARRANTY; without even the implied warranty of
+//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//   GNU General Public License for more details.
+//
+//   You should have received a copy of the license with this software;
+//   If not, please contact support @ S y s T u r n .com to receive a copy.
+//
+/**
+ * 使用方法:
+ * 
+ * $filter = new \Qii\Library\ImageFilter();
+ * $score = $filter->getScore($_FILES['img']['tmp_name']);
+ * if($score){
+ * 	$msg = $score >= 60 ? 'Nude picture' : 'Non-Nude picture';
+ * }
+ * 
+ */ 
+namespace Qii\Library;
+
+class ImageFilter
+{                              #R  G  B
+    var $colorA = 7944996;     #79 3B 24
+    var $colorB = 16696767;    #FE C5 BF
+ 
+ 
+    var $arA = array();
+    var $arB = array();
+ 
+    public function __construct()
+    {
+        $this->arA['R'] = ($this->colorA >> 16) & 0xFF;
+        $this->arA['G'] = ($this->colorA >> 8) & 0xFF;
+        $this->arA['B'] = $this->colorA & 0xFF;
+ 
+        $this->arB['R'] = ($this->colorB >> 16) & 0xFF;
+        $this->arB['G'] = ($this->colorB >> 8) & 0xFF;
+        $this->arB['B'] = $this->colorB & 0xFF;
+    }
+ 
+    public function getScore($image)
+    {
+        $x = 0; $y = 0;
+        $img = $this->_getImageResource($image, $x, $y);
+        if(!$img) return false;
+ 
+        $score = 0;
+ 
+        $xPoints = array($x/8, $x/4, ($x/8 + $x/4), $x-($x/8 + $x/4), $x-($x/4), $x-($x/8));
+        $yPoints = array($y/8, $y/4, ($y/8 + $y/4), $y-($y/8 + $y/4), $y-($y/8), $y-($y/8));
+        $zPoints = array($xPoints[2], $yPoints[1], $xPoints[3], $y);
+ 
+ 
+        for($i=1; $i<=$x; $i++)
+        {
+            for($j=1; $j<=$y; $j++)
+            {
+                $color = @imagecolorat($img, $i, $j);
+                if($color >= $this->colorA && $color <= $this->colorB)
+                {
+                    $color = array('R'=> ($color >> 16) & 0xFF, 'G'=> ($color >> 8) & 0xFF, 'B'=> $color & 0xFF);
+                    if($color['G'] >= $this->arA['G'] && $color['G'] <= $this->arB['G'] && $color['B'] >= $this->arA['B'] && $color['B'] <= $this->arB['B'])
+                    {
+                        if($i >= $zPoints[0] && $j >= $zPoints[1] && $i <= $zPoints[2] && $j <= $zPoints[3])
+                        {
+                            $score += 3;
+                        }
+                        elseif($i <= $xPoints[0] || $i >=$xPoints[5] || $j <= $yPoints[0] || $j >= $yPoints[5])
+                        {
+                            $score += 0.10;
+                        }
+                        elseif($i <= $xPoints[0] || $i >=$xPoints[4] || $j <= $yPoints[0] || $j >= $yPoints[4])
+                        {
+                            $score += 0.40;
+                        }
+                        else
+                        {
+                            $score += 1.50;
+                        }
+                    }
+                }
+            }
+        }
+ 
+        imagedestroy($img);
+ 
+        $score = sprintf('%01.2f', ($score * 100) / ($x * $y));
+        if($score > 100) $score = 100;
+        return $score;
+    }
+ 
+    public function getScoreAndFill($image, $outputImage)
+    {
+        $x = 0; $y = 0;
+        $img = $this->_GetImageResource($image, $x, $y);
+        if(!$img) return false;
+ 
+        $score = 0;
+ 
+        $xPoints = array($x/8, $x/4, ($x/8 + $x/4), $x-($x/8 + $x/4), $x-($x/4), $x-($x/8));
+        $yPoints = array($y/8, $y/4, ($y/8 + $y/4), $y-($y/8 + $y/4), $y-($y/8), $y-($y/8));
+        $zPoints = array($xPoints[2], $yPoints[1], $xPoints[3], $y);
+ 
+ 
+        for($i=1; $i<=$x; $i++)
+        {
+            for($j=1; $j<=$y; $j++)
+            {
+                $color = imagecolorat($img, $i, $j);
+                if($color >= $this->colorA && $color <= $this->colorB)
+                {
+                    $color = array('R'=> ($color >> 16) & 0xFF, 'G'=> ($color >> 8) & 0xFF, 'B'=> $color & 0xFF);
+                    if($color['G'] >= $this->arA['G'] && $color['G'] <= $this->arB['G'] && $color['B'] >= $this->arA['B'] && $color['B'] <= $this->arB['B'])
+                    {
+                        if($i >= $zPoints[0] && $j >= $zPoints[1] && $i <= $zPoints[2] && $j <= $zPoints[3])
+                        {
+                            $score += 3;
+                            imagefill($img, $i, $j, 16711680);
+                        }
+                        elseif($i <= $xPoints[0] || $i >=$xPoints[5] || $j <= $yPoints[0] || $j >= $yPoints[5])
+                        {
+                            $score += 0.10;
+                            imagefill($img, $i, $j, 14540253);
+                        }
+                        elseif($i <= $xPoints[0] || $i >=$xPoints[4] || $j <= $yPoints[0] || $j >= $yPoints[4])
+                        {
+                            $score += 0.40;
+                            imagefill($img, $i, $j, 16514887);
+                        }
+                        else
+                        {
+                            $score += 1.50;
+                            imagefill($img, $i, $j, 512);
+                        }
+                    }
+                }
+            }
+        }
+        imagejpeg($img, $outputImage);
+ 
+        imagedestroy($img);
+ 
+        $score = sprintf('%01.2f', ($score * 100) / ($x * $y));
+        if($score > 100) $score = 100;
+        return $score;
+    }
+ 
+    private function _getImageResource($image, &$x, &$y)
+    {
+        $info = GetImageSize($image);
+ 
+        $x = $info[0];
+        $y = $info[1];
+ 
+        switch( $info[2] )
+        {
+            case IMAGETYPE_GIF:
+                return @ImageCreateFromGif($image);
+ 
+            case IMAGETYPE_JPEG:
+                return @ImageCreateFromJpeg($image);
+ 
+            case IMAGETYPE_PNG:
+                return @ImageCreateFromPng($image);
+ 
+            default:
+                return false;
+        }
+    }
+}