2011-01-24 44 views
1

我有Ian Selby的这堂课。php thumbnailer课题

比方说,我有一个1024x768的图像。我想从该图像的中心裁剪230x53,以便出现230x53的缩略图图像。

但是,我总是得到230x230。

问题行:

$thumb->cropFromCenter(230, 153); 

有任何人遇到这种情况?如果是这样,你做了什么来解决它?

上下文:

$fileThumb = "./lib/galeria/thumb".$r["anexo"]; 
if (!file_exists($fileThumb)){ 
$thumb = new Thumbnail("lib/galeria/".$r["anexo"]); 
$thumb->cropFromCenter(230, 153); 
$thumb->show(100,$fileThumb); 
} 

,我使用的类版本:1.1 - 我知道,我们可以找到一个新的,但在写这篇文章的时候,店主网站当前离线小时。

非常感谢, MEM

回答

1

看来,至少在这个版本中,cropFromCenter,生成一个正方形。

所以,我最终添加了一个新的方法,非常类似于一些变化。

/** 
    * Crop a image from calculated center not in a square BUT 
     * on a given heigth and width. 
    * 
    * @param int $width 
    * @param int $height 
    */ 
    public function cropFromCenterNoSquare($width, $height) { 
     if($width > $this->currentDimensions['width']) $width = $this->currentDimensions['width']; 
     if($height > $this->currentDimensions['height']) $height = $this->currentDimensions['height']; 

     $cropX = intval(($this->currentDimensions['width'] - $width)/2); 
     $cropY = intval(($this->currentDimensions['height'] - $height)/2); 

     if(function_exists("ImageCreateTrueColor")) { 
      $this->workingImage = ImageCreateTrueColor($width,$height); 
     } 
     else { 
      $this->workingImage = ImageCreate($width,$height); 
     } 

     imagecopyresampled(
      $this->workingImage, 
      $this->oldImage, 
      0, 
      0, 
      $cropX, 
      $cropY, 
      $width, 
      $height, 
      $width, 
      $height 
     ); 

     $this->oldImage = $this->workingImage; 
     $this->newImage = $this->workingImage; 
     $this->currentDimensions['width'] = $width; 
     $this->currentDimensions['height'] = $height; 
    } 

问候, MEM