2010-10-01 155 views
1

我想要一個功能,當我上傳一張照片時,它應該裁剪圖像,而不管中心圖像的比例如何,以確保裁切在圖像內部。PHP圖像調整大小和裁剪功能

alt text

上面的圖片是2592×1944

我要裁剪的159 * 129

alt text

的圖像,這是使用一個插件,當我得到什麼cakephp(Miles Johnsons上傳插件)

有人可以幫我找到一個圖像裁剪功能來做到這一點或者幫助我完成相同的算法。

+2

您的意思是'resize'而不是'crop'嗎? – 2010-10-01 05:07:33

+0

調整大小,裁剪或調整大小和作物? – stillstanding 2010-10-01 05:13:45

+0

我不知道如何以正確的方式做到這一點。我希望最終的圖像是圖像的最大部分,但我需要的大小。所以我認爲它可能會調整大小和作物或其他組合。我不確定。 – 2010-10-01 08:14:03

回答

1

我用這個:http://shiftingpixel.com/2008/03/03/smart-image-resizer/創造一切在這裏找到了圖片的縮略圖:http://www.patriciashin.com/painting.php

+1

對不起,我沒有添加更多,我的筆記本電腦電池死亡。無論如何,這個腳本是非常可定製的。他們的文檔非常好。我想我甚至可以調整腳本以更好地滿足我的需求。 Cakephp可以很容易地將其轉換爲第三方腳本,但是您可能必須將此腳本演示爲最有效的類。讓我知道你的想法,我願意幫忙。 – gregghz 2010-10-01 06:06:49

+0

看起來很有希望。我打算構建一個插件,將其作爲開源發佈。你會感興趣嗎 ?因爲那裏的腳本我沒有發現他們很多做適當的調整和裁剪。 – 2010-10-01 08:18:41

+0

當然我會很感興趣。只需與我聯繫即可進行協調。我並不總是有很多時間,但我會盡我所能幫助。 – gregghz 2010-10-01 14:59:53

1

我必須說,我還沒有徹底測試這段代碼,我修改了它爲個人使用,這將有助於你的問題。

替換插件/上載/供應商的功能作物/ uploader.php

近線368

用下面的函數

public function crop(array $options = array(), $explicit = false) { 
    if ($this->_data[$this->_current]['group'] != 'image' || !$this->_enabled) { 
     return false; 
    } 

    $options = $options + array('location' => self::LOC_CENTER, 'quality' => 100, 'width' => null, 'height' => null, 'append' => null, 'prepend' => null); 
    $width = $this->_data[$this->_current]['width']; 
    $height = $this->_data[$this->_current]['height']; 
    $src_x = 0; 
    $src_y = 0; 
    $dest_w = $width; 
    $dest_h = $height; 
    $location = $options['location']; 

    if (is_numeric($options['width']) && is_numeric($options['height'])) { 
     $newWidth = $options['width']; 
     $newHeight = $options['height']; 

     if ($width/$newWidth > $height/$newHeight) { 
      $dest_h = $options['height']; 
      $dest_w = round($width/($height/$newHeight)); 
     } else { 
      $dest_w = $options['width']; 
      $dest_h = round($height/($width/$newWidth)); 
     } 
    } else { 
     if ($width > $height) { 
      $newWidth = $height; 
      $newHeight = $height; 
     } else { 
      $newWidth = $width; 
      $newHeight = $width; 
     } 

     $dest_h = $newHeight; 
     $dest_w = $newWidth; 
    } 

    $src_x = 0; 
    $src_y = 0; 
    if ($dest_w > $newWidth) { 
      $src_x = ceil((($dest_w - $newWidth)/2) * ($height/$newHeight)); 
    } 

    if ($dest_h > $newHeight) { 
      $src_y = ceil((($dest_h - $newHeight)/2) * ($width/$newWidth)); 
    } 

    $append = '_cropped_' . $newWidth . 'x' . $newHeight; 

    if ($options['append'] !== false && empty($options['append'])) { 
     $options['append'] = $append; 
    } 

    $transform = array(
     'width' => $newWidth, 
     'height' => $newHeight, 
     'source_x' => $src_x, 
     'source_y' => $src_y, 
     'source_w' => $width, 
     'source_h' => $height, 
     'dest_w' => $dest_w, 
     'dest_h' => $dest_h, 
     'target' => $this->setDestination($this->_data[$this->_current]['name'], true, $options, false), 
     'quality' => $options['quality'] 
    ); 
    if ($this->transform($transform)) { 

     return $this->_returnData($transform, $append, $explicit); 
    } 

    return false; 
} 

此致。