我想從200 * 130大小的中心裁剪圖像要裁剪的圖像的大小可能會有所不同,如果圖像較小,我們不會裁剪它我知道如何在這部分我可以檢查高度和圖像,但種類從圖像的中間被裁剪的東西 因爲我不知道如何保持中心作爲作物點和比向外作物它從中心裁剪圖像PHP
10
A
回答
31
GD從4.3.6版本開始捆綁所有的PHP安裝,所以很有可能你有它。
這裏有您需要採取的步驟......
- 創建使用的GD
imagecreatefrom*()
功能之一的圖像資源。您使用的一個取決於你處理 - 圖像的類型使用
imagesx()
和imagesy()
- 確定你的作物座標使用以下算法和作物使用
imagecopy()
查找作物座標確定圖像尺寸
$width = imagesx($img);
$height = imagesy($img);
$centreX = round($width/2);
$centreY = round($height/2);
$cropWidth = 200;
$cropHeight = 130;
$cropWidthHalf = round($cropWidth/2); // could hard-code this but I'm keeping it flexible
$cropHeightHalf = round($cropHeight/2);
$x1 = max(0, $centreX - $cropWidthHalf);
$y1 = max(0, $centreY - $cropHeightHalf);
$x2 = min($width, $centreX + $cropWidthHalf);
$y2 = min($height, $centreY + $cropHeightHalf);
隨意使用我的圖像處理類,它應該做出一些方面要容易得多 - https://gist.github.com/880506
$im = new ImageManipulator('/path/to/image');
$centreX = round($im->getWidth()/2);
$centreY = round($im->getHeight()/2);
$x1 = $centreX - 100;
$y1 = $centreY - 65;
$x2 = $centreX + 100;
$y2 = $centreY + 65;
$im->crop($x1, $y1, $x2, $y2); // takes care of out of boundary conditions automatically
$im->save('/path/to/cropped/image');
0
這可能會幫助你。
function cropCentered($img, $w, $h)
{
$cx = $img->getWidth()/2;
$cy = $img->getHeight()/2;
$x = $cx - $w/2;
$y = $cy - $h/2;
if ($x < 0) $x = 0;
if ($y < 0) $y = 0;
return $img->crop($x, $y, $w, $h);
}
我假設你正在使用GD庫。 $ img是GD圖像,$ w和$ h分別是寬度和高度,您希望您的新圖像具有。在你的情況下,$ w = 200,$ h = 130.
1
jeez,你爲什麼要這麼做呢?只需簡單設置x和y位置的量來裁剪/ 2
$imageSize = getimagesize('thumbnail.png');
$croppedImage = imagecrop(imagecreatefrompng('thumbnail.png'), ['x' => 0, 'y' => ($imageSize[1]-$imageSize[0]*(9/16))/2, 'width' => $imageSize[0], 'height' => $imageSize[0]*(9/16)]);
通知我如何用我的$ IMAGESIZE [0] *(9/16),這也是我在y裁剪經量方向,我從原始圖像高度減去找到裁剪量,然後除以2.如果你想要做同樣的寬度,只需按照相同的步驟。
相關問題
- 1. PHP Imagick - 中心裁剪後的圖像
- 2. 裁剪圖像的中心
- 3. 圖像裁剪PHP
- 4. PHP - 裁剪圖像
- 5. 在PHP中裁剪圖像
- 6. 從UIImageView中裁剪圖像
- 7. 從URL中裁剪圖像
- 8. 來自中心的C#裁剪圖像
- 9. 裁剪圖像不是中心
- 10. 中心圖像和裁剪它
- 11. 使用php裁剪圖像
- 12. PHP圖像裁剪問題
- 13. php縮放圖像裁剪
- 14. 從現有圖像中裁剪圖像
- 15. WinRT中的裁剪/裁剪圖像
- 16. CSS3中心裁剪圖片
- 17. 裁剪圖像
- 18. 裁剪圖像
- 19. 裁剪圖像
- 20. 在PHP中裁剪圖像imagecopyresampled - 壓扁而不是裁剪
- 21. 從頂部裁剪圖像
- 22. PHP GD - 如何修改我的Thumbnail Creator以從中心裁剪人像圖像?
- 23. PHP圖像調整大小 - 從中心平方/裁剪縮略圖?
- 24. 從裁剪圖像創建新圖像
- 25. 用Libgdx從圖像裁剪圖像
- 26. 無法剪裁/裁剪圖像
- 27. 核心圖形 - 如何從UIImage中裁剪非透明像素?
- 28. 圖像裁剪AVCaptureSession圖像
- 29. 用PHP上傳,調整圖像大小和裁剪圖像的中心
- 30. 笨裁剪圖像
帽子關閉,這是驚人的,並出色地工作。THanks – June
在第一個算法中,哪些變量對應於imagecopy()的正確參數? –
@ChrisHarrison請參閱https://gist.github.com/philBrown/880506#file-imagemanipulator-php-L185 – Phil