2015-11-18 53 views
0

我試圖用GD重新採樣圖像。PHP GD不能正確重新採樣圖像(不能正確縮放)

基本上我試圖仿效的CSS的background-size: cover;

我的類需要的文件路徑名,那麼cover方法將覆蓋件的尺寸。

首先,這裏的代碼:

<?php 

class Image{ 

    public $filepath; 
    public $width; 
    public $height; 
    public $mime; 
    public $image; 
    public $landscape; 
    public $imageFunct; 
    public $compression; 

    public function __construct($fn){ 

     // The path to the /img directory 
     $ImgPath = realpath(dirname(dirname(dirname(__FILE__))))."/img"; 

     // Find the actual path, look in all relevant directories 
     $fp = $ImgPath."/$fn"; 
     if(!file_exists($fp)) $fp = $ImgPath."/backgrounds/$fn"; 
     if(!file_exists($fp)) throw new Exception("Image source file does not exist: $fp"); 

     $this->filepath = $fp; 
     $data = getimagesize($fp); 
     $this->width = $data[0]; 
     $this->height = $data[1]; 
     $this->landscape = $this->width > $this->height; 
     $this->mime = $data['mime']; 
     switch($this->mime){ 
      case("image/png"): 
       $this->image = imagecreatefrompng($this->filepath); 
       $this->imageFunct = 'imagepng'; 
       $this->compression = 9; 
       break; 
      case('image/jpeg'): 
      case('image/pjpeg'): 
      case('image/x-jps'): 
       $this->image = imagecreatefromjpeg($this->filepath); 
       $this->imageFunct = 'imagejpeg'; 
       $this->compression = 100; 
       break; 
      case('image/gif'): 
       $this->image = imagecreatefromgif($this->filepath); 
       $this->imageFunct = 'imagegif'; 
       break; 
      default: 
       throw new Exception("Invalid image type. Only excepts PNG, JPG, and GIF. You entered a {$this->mime} type image."); 
     } 
    } 

    /** 
    * scales the image to cover the dimensions provided 
    * @param type $width 
    * @param type $height 
    * @param type $output_mimetype (defaults to the original image's mimtype) 
    */ 
    public function cover($width, $height, $output_mimetype=''){ 

     $imgRatio = $this->height/$this->width; 
     $canvasRatio = $height/$width; 

     if ($canvasRatio > $imgRatio){ 
      $finalHeight = $height; 
      $scale = $finalHeight/$this->height; 
      $finalWidth = $this->width * $scale; 
     }else{ 
      $finalWidth = $width; 
      $scale = $finalWidth/$this->width; 
      $finalHeight = $this->height * $scale; 
     } 

     // Resize the image 
     $thumb = imagecreatetruecolor($finalWidth, $finalHeight); 
     imagecopyresampled($thumb, $this->image, 0, 0, 0, 0, $finalWidth, $finalHeight, $this->height, $this->height); 

     // Get output details 
     switch(strtoupper($output_mimetype)){ 
      case('JPG'): 
      case('JPEG'): 
       $mimetype = 'image/jpeg'; 
       $funct = 'imagejpeg'; 
       $compression = 100; 
       break; 
      case('PNG'): 
       $mimetype = 'image/png'; 
       $funct = 'imagepng'; 
       $compression = 9; 
       break; 
      case('GIF'): 
       $mimetype = 'image/gif'; 
       $funct = 'imagegif'; 
       $compression = null; 
       break; 
      default: 
       $mimetype = $this->mime; 
       $funct = $this->imageFunct; 
       $compression = $this->compression; 
     } 

     // Output and clear memory 
     header('Content-Type: '.$mimetype); 

     // Get and call the image creation 
     // function with the correct compression/quality 
     if(!empty($compression)) 
      $funct($thumb, null, $compression); 
     else 
      $funct($thumb, null); 

     imagedestroy($thumb); 
    } 

} 

這裏是原始的源圖像 Original Image

調用$img->cover(1669, 556)後,我得到的圖像以正確的比例,但不是拉伸的原始圖像,以適應寬度,它只是用黑色填充左側的空間。下面是輸出:

Output image

我怎樣才能獲得圖像拉伸以填充黑色空間...?

順便說一句,景觀圖像似乎工作得很好,相同的代碼。

回答

0

沒關係,我只是智障,這是所有..

imagecopyresampled($thumb, $this->image, 0, 0, 0, 0, $finalWidth, $finalHeight, $this->height, $this->height); 

也許不應該用高度寬度..