Esempio n. 1
0
 /**
  * 裁剪函数,两个参数表示需要的长宽,可以为百分比,表示目标大小为现在的百分之多少
  * @param number $width
  * @param number $height
  * @return GdImage
  */
 public function crop($width, $height)
 {
     $sourceWidth = $this->image->getWidth();
     $sourceHeight = $this->image->getHeight();
     if ($width < 1) {
         $width = $sourceWidth * $width;
     }
     if ($height < 1) {
         $height = $sourceHeight * $height;
     }
     $sourceHWRatio = $sourceHeight / $sourceWidth;
     $targetHWRatio = $height / $width;
     if ($sourceHWRatio > $targetHWRatio) {
         $ratio = 1.0 * $width / $sourceWidth;
     } else {
         $ratio = 1.0 * $height / $sourceHeight;
     }
     $tmp_w = (int) ($width / $ratio);
     $tmp_h = (int) ($height / $ratio);
     $tmp_x = (int) ($sourceWidth - $tmp_w) / 2;
     $tmp_y = (int) ($sourceHeight - $tmp_h) / 2;
     $targetImage = $this->image->initNew($width, $height);
     imagecopyresampled($targetImage->getImage(), $this->image->getImage(), 0, 0, $tmp_x, $tmp_y, $width, $height, $tmp_w, $tmp_h);
     return $targetImage;
 }