コード例 #1
0
 /**
  * Crop image
  *
  * @param  string   $path               image file
  * @param  int      $width              crop width
  * @param  int      $height             crop height
  * @param  bool	    $x                  crop left offset
  * @param  bool	    $y                  crop top offset
  * @param  string   $destformat         image destination format
  * @return string|false
  * @author Dmitry (dio) Levashov
  * @author Alexey Sukhotin
  **/
 protected function imgCrop($path, $width, $height, $x, $y, $destformat = null)
 {
     if (($s = @getimagesize($path)) == false) {
         return false;
     }
     $result = false;
     switch ($this->imgLib) {
         case 'imagick':
             try {
                 $img = new imagick($path);
             } catch (Exception $e) {
                 return false;
             }
             $img->cropImage($width, $height, $x, $y);
             $result = $img->writeImage($path);
             return $result ? $path : false;
             break;
         case 'gd':
             if ($s['mime'] == 'image/jpeg') {
                 $img = imagecreatefromjpeg($path);
             } elseif ($s['mime'] == 'image/png') {
                 $img = imagecreatefrompng($path);
             } elseif ($s['mime'] == 'image/gif') {
                 $img = imagecreatefromgif($path);
             } elseif ($s['mime'] == 'image/xbm') {
                 $img = imagecreatefromxbm($path);
             }
             if ($img && false != ($tmp = imagecreatetruecolor($width, $height))) {
                 if (!imagecopy($tmp, $img, 0, 0, $x, $y, $width, $height)) {
                     return false;
                 }
                 if ($destformat == 'jpg' || $destformat == null && $s['mime'] == 'image/jpeg') {
                     $result = imagejpeg($tmp, $path, 100);
                 } else {
                     if ($destformat == 'gif' || $destformat == null && $s['mime'] == 'image/gif') {
                         $result = imagegif($tmp, $path, 7);
                     } else {
                         $result = imagepng($tmp, $path, 7);
                     }
                 }
                 imagedestroy($img);
                 imagedestroy($tmp);
                 return $result ? $path : false;
             }
             break;
     }
     return false;
 }
コード例 #2
0
	/**
	 * Crop image
	 *
	 * @param  string   $path               image file
	 * @param  int      $width              crop width
	 * @param  int      $height             crop height
	 * @param  bool	    $x                  crop left offset
	 * @param  bool	    $y                  crop top offset
	 * @param  string   $destformat         image destination format
	 * @return string|false
	 * @author Dmitry (dio) Levashov
	 * @author Alexey Sukhotin
	 **/
  	protected function imgCrop($path, $width, $height, $x, $y, $destformat = null) {
		if (($s = @getimagesize($path)) == false) {
			return false;
		}

		$result = false;
		
		switch ($this->imgLib) {
			case 'imagick':
				
				try {
					$img = new imagick($path);
				} catch (Exception $e) {

					return false;
				}

				$img->cropImage($width, $height, $x, $y);

				$result = $img->writeImage($path);

				return $result ? $path : false;

				break;

			case 'gd':
				if ($s['mime'] == 'image/jpeg') {
					$img = imagecreatefromjpeg($path);
				} elseif ($s['mime'] == 'image/png') {
					$img = imagecreatefrompng($path);
				} elseif ($s['mime'] == 'image/gif') {
					$img = imagecreatefromgif($path);
				} elseif ($s['mime'] == 'image/xbm') {
					$img = imagecreatefromxbm($path);
				}

				if ($img &&  false != ($tmp = imagecreatetruecolor($width, $height))) {
					
					$bgcolor = $this->options['tmbBgColor'];

					if ($bgcolor == 'transparent') {
						list($r, $g, $b) = array(0, 0, 255);
					} else {
						list($r, $g, $b) = sscanf($bgcolor, "#%02x%02x%02x");
					}

					$bgcolor1 = imagecolorallocate($tmp, $r, $g, $b);

					if ($bgcolor == 'transparent') {
						$bgcolor1 = imagecolortransparent($tmp, $bgcolor1);
					}

					imagefill($tmp, 0, 0, $bgcolor1);

					$size_w = $width;
					$size_h = $height;

					if ($s[0] < $width || $s[1] < $height) {
						$size_w = $s[0];
						$size_h = $s[1];
					}

					if (!imagecopy($tmp, $img, 0, 0, $x, $y, $size_w, $size_h)) {
						return false;
					}
					
					if ($destformat == 'jpg'  || ($destformat == null && $s['mime'] == 'image/jpeg')) {
						$result = imagejpeg($tmp, $path, 100);
					} else if ($destformat == 'gif' || ($destformat == null && $s['mime'] == 'image/gif')) {
						$result = imagegif($tmp, $path, 7);
					} else {
						$result = imagepng($tmp, $path, 7);
					}

					imagedestroy($img);
					imagedestroy($tmp);

					return $result ? $path : false;

				}
				break;
		}

		return false;
	}
コード例 #3
0
 /**
  * Crop image
  *
  * @param  string   $path               image file
  * @param  int      $width              crop width
  * @param  int      $height             crop height
  * @param  bool	    $x                  crop left offset
  * @param  bool	    $y                  crop top offset
  * @param  string   $destformat         image destination format
  * @return string|false
  * @author Dmitry (dio) Levashov
  * @author Alexey Sukhotin
  **/
 protected function imgCrop($path, $width, $height, $x, $y, $destformat = null)
 {
     if (($s = @getimagesize($path)) == false) {
         return false;
     }
     $result = false;
     switch ($this->imgLib) {
         case 'imagick':
             try {
                 $img = new imagick($path);
             } catch (Exception $e) {
                 return false;
             }
             $img->cropImage($width, $height, $x, $y);
             $result = $img->writeImage($path);
             return $result ? $path : false;
             break;
         case 'gd':
             $img = self::gdImageCreate($path, $s['mime']);
             if ($img && false != ($tmp = imagecreatetruecolor($width, $height))) {
                 self::gdImageBackground($tmp, $this->options['tmbBgColor']);
                 $size_w = $width;
                 $size_h = $height;
                 if ($s[0] < $width || $s[1] < $height) {
                     $size_w = $s[0];
                     $size_h = $s[1];
                 }
                 if (!imagecopy($tmp, $img, 0, 0, $x, $y, $size_w, $size_h)) {
                     return false;
                 }
                 $result = self::gdImage($tmp, $path, $destformat, $s['mime']);
                 imagedestroy($img);
                 imagedestroy($tmp);
                 return $result ? $path : false;
             }
             break;
     }
     return false;
 }
コード例 #4
0
 private function resizeCropImg($pFolder, $pImg, $pW, $pH)
 {
     $im = new imagick();
     $im->readImage($pFolder . $pImg);
     $image = new stdClass();
     $image->dimensions = $im->getImageGeometry();
     $image->w = $image->dimensions['width'];
     $image->h = $image->dimensions['height'];
     $image->ratio = $image->w / $image->h;
     if ($image->w / $pW < $image->h / $pH) {
         $h = ceil($pH * $image->w / $pW);
         $y = ($image->h - $pH * $image->w / $pW) / 2;
         $im->cropImage($image->w, $h, 0, $y);
     } else {
         $w = ceil($pW * $image->h / $pH);
         $x = ($image->w - $pW * $image->h / $pH) / 2;
         $im->cropImage($w, $image->h, $x, 0);
     }
     $im->ThumbnailImage($pW, $pH, true);
     if ($img->type === "PNG") {
         $im->setImageCompressionQuality(55);
         $im->setImageFormat('png');
     } elseif ($img->type === "JPG" || $img->type === "JPEG") {
         $im->setCompressionQuality(100);
         $im->setImageFormat("jpg");
     }
     $im->writeImage($this->ad->url->folder . '/assets/' . $pImg);
     $im->destroy();
 }