| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 | 
							- <?php
 
- namespace KIF;
 
- use KIF\Data\ResultWrapper;
 
- class Image {
 
- 	
 
- 	/**
 
- 	 * @desc 缩放图片
 
- 	 * @param string $src 图片源地址(全路径)
 
- 	 * @param int $dst_w 目标宽度
 
- 	 * @param int $dst_h 目标高度
 
- 	 * @param string $dst 目标地址(全路径) 如果指定,则把缩放后的图片直接写入到$dst指定的路径;否则则返回图片的二进制值
 
- 	 * @param boolean $isHold 是否锁定原图的高宽比。如果false(不锁定),则严格按照指定的$dst_w和$dst_h生成新的图片
 
- 	 * @param string $format 缩放后图片的格式。如果不指定,则使用原图的格式
 
- 	 * @return InternalResultTransfer
 
- 	 */
 
- 	static function resize($src, $dst_w, $dst_h, $dst = null, $isHold = false, $format = null) {
 
- 		if (empty($src)) {
 
- 			return ResultWrapper::fail("请指定原图");
 
- 		}
 
- 		if (!file_exists($src)) {
 
- 			return ResultWrapper::fail("{$src} 该图片文件不存在");
 
- 		}
 
- 		$objImagick = new \Imagick();
 
- 		$objImagick ->readImage($src);
 
- 		if ($isHold) {
 
- 			$src_h = $objImagick->getImageHeight();
 
- 			$src_w = $objImagick->getImageWidth();
 
- 			/// 源图片比目标图片要小
 
- 			if ($src_w < $dst_w && $src_h < $dst_h) {
 
- 				$hratio = $dst_h / $src_h;
 
- 				$wratio = $dst_w / $src_w;
 
- 				$ratio = $hratio < $wratio ? $hratio : $wratio;
 
- 				$dst_h = $src_h * $ratio;
 
- 				$dst_w = $src_w * $ratio;
 
- 				$isHold = false;
 
- 			}
 
- 		}
 
- 		$objImagick->resizeImage($dst_w, $dst_h, \Imagick::FILTER_UNDEFINED, 1, $isHold);
 
- 		if (is_null($format)) {
 
- 			$format = $objImagick->getImageFormat();
 
- 		}
 
- 		$objImagick->setImageFormat($format);
 
- 		if (is_null($dst)) {// 返回图像内容
 
- 			$data = $objImagick->getImageBlob();
 
- 			$ret = ResultWrapper::success($data);
 
- 		} else {
 
- 			$tmpWriteResult = $objImagick->writeImage($dst);
 
- 			if ($tmpWriteResult) {
 
- 				$ret = ResultWrapper::success(array(
 
- 					'w' => $objImagick->getImageWidth(),
 
-                 	'h' => $objImagick->getImageHeight(),
 
- 				));
 
- 			} else {
 
- 				$ret = ResultWrapper::fail("写入目标地址失败");
 
- 			}
 
- 		}
 
- 		$objImagick->destroy();
 
- 		return $ret;
 
- 	}
 
- 	
 
- 	/**
 
- 	 * @desc 图片格式转换
 
- 	 * @param string $source 源图地址
 
- 	 * @param string $destina 保存地址
 
- 	 * @param sring $format 保存格式 (JPG、JPEG、PNG)
 
- 	 * @return ResultWrapper
 
- 	 */
 
- 	static public function transformFormat($source, $destina = null, $format) {
 
- 		if (empty($source)) {
 
- 			return ResultWrapper::fail('请指定原图路径');
 
- 		}
 
- 		
 
- 		if (empty($format)) {
 
- 			return ResultWrapper::fail('请指定图片保存的格式');
 
- 		}
 
- 		
 
- 		$objImagick = new \Imagick();
 
- 		$objImagick->readimage($source);
 
- 		$objImagick->setformat($format);
 
- 		
 
- 		if ($destina === null) {
 
- 			$data = $objImagick->getImageBlob();
 
- 			$result = ResultWrapper::success($data);
 
- 		} else {
 
- 			if ($objImagick->writeImage($destina)) {
 
- 				$result = ResultWrapper::success();
 
- 			}
 
- 		}
 
- 		
 
- 		return $result;
 
- 	}
 
- }
 
 
  |