コード例 #1
0
 public function adaptiveResizeQuadrant($width, $height, $item = 'c')
 {
     if (!(($width = intval($width)) > 0 && ($height = intval($height)) > 0)) {
         throw new ImageUtilityException('ImageGdUtility 錯誤!', '新尺寸錯誤,width:' . $width . ',height:' . $height, '尺寸寬高一定要大於 0!');
     }
     if ($width == $this->dimension['width'] && $height == $this->dimension['height']) {
         return $this;
     }
     $newDimension['width'] = $this->options['resizeUp'] === false && $width > $this->dimension['width'] ? $this->dimension['width'] : $width;
     $newDimension['height'] = $this->options['resizeUp'] === false && $height > $this->dimension['height'] ? $this->dimension['height'] : $height;
     if (!ImageUtility::verifyDimension($newDimension = $this->calcImageSizeStrict($this->dimension, $newDimension))) {
         throw new ImageUtilityException('ImageGdUtility 錯誤!', '新尺寸錯誤!', '尺寸寬高一定要大於 0!');
     }
     $this->resize($newDimension['width'], $newDimension['height']);
     $newDimension['width'] = $this->options['resizeUp'] === false && $width > $this->dimension['width'] ? $this->dimension['width'] : $width;
     $newDimension['height'] = $this->options['resizeUp'] === false && $height > $this->dimension['height'] ? $this->dimension['height'] : $height;
     if (!ImageUtility::verifyDimension($newDimension)) {
         throw new ImageUtilityException('ImageGdUtility 錯誤!', '新尺寸錯誤!', '尺寸寬高一定要大於 0!');
     }
     $newImage = function_exists('imagecreatetruecolor') ? imagecreatetruecolor($newDimension['width'], $newDimension['height']) : imagecreate($newDimension['width'], $newDimension['height']);
     $newImage = $this->_preserveAlpha($newImage);
     $cropX = $cropY = 0;
     if ($this->dimension['width'] > $newDimension['width']) {
         switch ($item) {
             case 'l':
             case 'L':
                 $cropX = 0;
                 break;
             case 'r':
             case 'R':
                 $cropX = intval($this->dimension['width'] - $newDimension['width']);
                 break;
             case 'c':
             case 'C':
             default:
                 $cropX = intval(($this->dimension['width'] - $newDimension['width']) / 2);
                 break;
         }
     } else {
         if ($this->dimension['height'] > $newDimension['height']) {
             switch ($item) {
                 case 't':
                 case 'T':
                     $cropY = 0;
                     break;
                 case 'b':
                 case 'B':
                     $cropY = intval($this->dimension['height'] - $newDimension['height']);
                     break;
                 case 'c':
                 case 'C':
                 default:
                     $cropY = intval(($this->dimension['height'] - $newDimension['height']) / 2);
                     break;
             }
         }
     }
     return $this->_copyReSampled($newImage, $this->image, 0, 0, $cropX, $cropY, $newDimension['width'], $newDimension['height'], $newDimension['width'], $newDimension['height']);
 }
コード例 #2
0
 public function adaptiveResizeQuadrant($width, $height, $item = 'c')
 {
     if (!(($width = intval($width)) > 0 && ($height = intval($height)) > 0)) {
         throw new ImageUtilityException('ImageImagickUtility 錯誤!', '新尺寸錯誤,width:' . $width . ',height:' . $height, '尺寸寬高一定要大於 0!');
     }
     if ($width == $this->dimension['width'] && $height == $this->dimension['height']) {
         return $this;
     }
     $newDimension['width'] = $this->options['resizeUp'] === false && $width > $this->dimension['width'] ? $this->dimension['width'] : $width;
     $newDimension['height'] = $this->options['resizeUp'] === false && $height > $this->dimension['height'] ? $this->dimension['height'] : $height;
     if (!ImageUtility::verifyDimension($newDimension = $this->calcImageSizeStrict($this->dimension, $newDimension))) {
         throw new ImageUtilityException('ImageImagickUtility 錯誤!', '新尺寸錯誤!', '尺寸寬高一定要大於 0!');
     }
     $this->resize($newDimension['width'], $newDimension['height']);
     $newDimension['width'] = $this->options['resizeUp'] === false && $width > $this->dimension['width'] ? $this->dimension['width'] : $width;
     $newDimension['height'] = $this->options['resizeUp'] === false && $height > $this->dimension['height'] ? $this->dimension['height'] : $height;
     if (!ImageUtility::verifyDimension($newDimension)) {
         throw new ImageUtilityException('ImageImagickUtility 錯誤!', '新尺寸錯誤!', '尺寸寬高一定要大於 0!');
     }
     $cropX = $cropY = 0;
     if ($this->dimension['width'] > $newDimension['width']) {
         switch ($item) {
             case 'l':
             case 'L':
                 $cropX = 0;
                 break;
             case 'r':
             case 'R':
                 $cropX = intval($this->dimension['width'] - $newDimension['width']);
                 break;
             case 'c':
             case 'C':
             default:
                 $cropX = intval(($this->dimension['width'] - $newDimension['width']) / 2);
                 break;
         }
     } else {
         if ($this->dimension['height'] > $newDimension['height']) {
             switch ($item) {
                 case 't':
                 case 'T':
                     $cropY = 0;
                     break;
                 case 'b':
                 case 'B':
                     $cropY = intval($this->dimension['height'] - $newDimension['height']);
                     break;
                 case 'c':
                 case 'C':
                 default:
                     $cropY = intval(($this->dimension['height'] - $newDimension['height']) / 2);
                     break;
             }
         }
     }
     $workingImage = $this->_machiningImageCrop($cropX, $cropY, $newDimension['width'], $newDimension['height']);
     return $this->_updateImage($workingImage);
 }