簡體   English   中英

使用PHP(GD)進行圖像裁剪

[英]Image cropping with PHP (GD)

我有下面的圖像,我想裁剪,所以所有黑色區域被刪除,拇指返回。 拇指必須是130px(寬度)乘155px(高度)。

拇指

如何使用PHP的GD庫裁剪它? (Imagick庫不是一個選項)。

如果我的問題可以改進,請告訴我。

編輯

我使用了@Martijn建議的imagecopyresized()函數,代碼如下

imagecopyresized(
    imagecreatetruecolor(130,155) , 
    imagecreatefromjpeg($src_image) , 
    0, 
    0, 
    0, 
    0, 
    130,  
    155,  
    260 , 
    310   
)

我得到的是這個結果

的Thumb2

我究竟做錯了什么?

對於庫來說這很難做到,因為黑色的大小可能不同,並且您可能想要的圖像部分並不總是相同。

我建議使用jCrop (是的,網站非常小),它允許您選擇圖像的一部分。 如果可以手工完成,這是一種非常簡單的方法。 我在我的公司CMS'中使用它,我們的客戶從不需要解釋是如何工作的,非常自然 :)


如果這不是一個選項,您可以嘗試imagecopyresampled()

imagecopyresampled(
    $dst_image , // the thumb you want to place it on
    $src_image , // the image to crop it from
    0, // place left of thumb
    0, // place top of thumb
    0, // start from left of input image
    0, // start from top of input image
    130,  // destination width
    155,  // destination height
    $src_w , // the width of the image without the black
    $src_h   // the height of the image without the black
)

如果黑色畫布上的圖像沒有固定大小,則可以編寫一個函數來查找偏移量。 你可以通過拍攝第一行像素來找到第一個黑色像素,並檢查之后是否存在(比方說)10分黑色像素。
這可能是敏感的,您可以增加偵察區域來測試黑色是否也在下面的行中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM