123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- <?php
- namespace KIF;
- use KIF\Data\ResultWrapper;
- class Image {
-
-
- 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;
- }
-
-
- 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;
- }
- }
|