2010-12-14 155 views
3

好吧,基本上我希望所有圖像都是170x170px正方形。 因此,如果圖像不是一個正方形,我希望它被調整大小,然後在中間裁剪。調整大小然後裁剪PHP

我花了很多時間玩這個,我越來越無處..我已經得到它來裁剪更大的圖像等部分,但我特別需要圖像的大小,然後裁剪..

任何幫助將不勝感激。

// get image size of img 
$x = @getimagesize($img); 
// image width 
$sw = $x[0]; 
// image height 
$sh = $x[1]; 

if($sw > $sh) // Horizontal Rectangle? 
{ 
    $newwidth = ($sw/$sh)*170; 
    $newheight=170; 
    $x_pos = ($sw - $sh)/2; 
    $x_pos = ceil($x_pos); 
    $y_pos=0; 
} 

else if($sh > $sw) // Vertical Rectangle? 
{ 
    $newheight = ($sh/$sw)*170; 
    $newwidth=170; 
    $y_pos = ($sh - $sw)/2; 
    $y_pos = ceil($y_pos); 
    $x_pos=0; 
} 
else //Already Square 
{ 
    $newheight=170; 
    $newwidth=170; 
} 

$im = @ImageCreateFromJPEG ($img) or // Read JPEG Image 
$im = @ImageCreateFromPNG ($img) or // or PNG Image 
$im = @ImageCreateFromGIF ($img) or // or GIF Image 
$im = false; // If image is not JPEG, PNG, or GIF 

if (!$im) { 
    // We get errors from PHP's ImageCreate functions... 
    // So let's echo back the contents of the actual image. 
    readfile ($img); 
} else { 
    // Create the resized image destination 
    $thumb = @ImageCreateTrueColor (170, 170); 
    // Copy from image source, resize it, and paste to image destination 
    imagecopyresampled($thumb, $im, 0, 0, 180, $y_pos, 170, 170, $newwidth, 
    $newheight); 
} 
+0

請修復您的格式。 – metrobalderas 2010-12-14 19:43:22

+0

我提供了一個工作腳本,請檢查解決(見下文) – Teson 2010-12-15 23:18:30

回答

2

需要一些工作,但它應該給你足夠的開始。

function crop($filename, $width, $height) 
{ 
    // image resource, assuming it's PNG 
    $resource = imagecreatefrompng($filename); 
    // resource dimensions 
    $size = array(
     0 => imagesx($resource), 
     1 => imagesy($resource), 
    ); 
    // sides 
    $longer = (int)($size[0]/$width > $size[1]/$height); 
    $shorter = (int)(!$longer); 
    // ugly hack to avoid condition for imagecopyresampled() 
    $src = array(
     $longer => 0, 
     $shorter => ($size[$shorter]-$size[$longer])/2, 
    ); 
    // new image resource 
    $new = imagecreatetruecolor($width, $height); 
    // do the magic 
    imagecopyresampled($new, $resource, 
     0, 0, 
     $src[0], $src[1], 
     $width, $height, 
     $size[$longer], $size[$longer] 
    ); 

    // save it or something else :) 
} 

編輯:試圖解釋 「醜陋的黑客攻擊」 上面。在問題

兩個參數$src_x$src_y,從manual採取:

imagecopyresampled()將採取矩形區域從 寬度src_w和高度src_h和src_image在位置(從src_x,src_y),並將其放置在 中的dst_image的矩形區域的寬度dst_w和高度dst_h在 位置(dst_x,dst_y)。

含義如果$filename的寬度較長,src_x必須是0,並且如果高度較長,src_y必須0。轉換成代碼,它看起來像這樣:

$src = ($size[$shorter]-$size[$longer])/2; 

if ($longer === 1) 
{ 
    imagecopyresampled($new, $resource, 
     0, 0, 
     $src, 0, 
     $width, $height, 
     $size[$longer], $size[$longer] 
    ); 
} 
else 
{ 
    imagecopyresampled($new, $resource, 
     0, 0, 
     0, $src, 
     $width, $height, 
     $size[$longer], $size[$longer] 
    ); 
} 
+0

你可能會評論額外的黑客你已經利用。此外,給我最麻煩的是imagecopyresampled()函數的所有不同方面...謝謝 – 2010-12-14 21:05:22

+0

查看更新的答案。 「醜陋的黑客」很可能是一個糟糕的選擇。事情是我有一個圖像處理類,並顯示一個作物的例子,我不得不調整一下邏輯。因此,醜陋的黑客:) – 2010-12-14 21:26:44

0

是否使用ImageMagic?如果沒有,你應該。 http://php.net/manual/en/book.imagick.php

+0

OP tag gd ...你可以通過一些例子說服他嗎? – ajreal 2010-12-14 20:14:34

+0

不是..?這是原則可以達到我想要的.. id喜歡堅持我所知道的..謝謝 – 2010-12-14 21:06:13

+0

我想我不知道你在用什麼。我可以舉一些例子,其實很簡單。但是,使用ImageMagick意味着您需要將它安裝在承載應用程序的系統中。這通常是在* nix系統中的情況,而不是在Windows上。 – 2010-12-15 15:03:16

4

好的,這是一個工作的;

<? 
$img = 'leaf.jpg'; 
// get image size of img 
$x = @getimagesize($img); 

// image dimensions 
$sw = $x[0]; 
$sh = $x[1]; 

//dest size 
$dSize = 170; 

//find smallerst part and get needed scale and offset 
$yOff = 0; 
$xOff = 0; 
if($sw < $sh) { 
    $scale = $dSize/$sw; 
    $yOff = $sh/2 - $dSize/$scale/2; 
} else { 
    $scale = $dSize/$sh; 
    $xOff = $sw/2 - $dSize/$scale/2; 
} 

$im = @ImageCreateFromJPEG ($img) or // Read JPEG Image 
$im = @ImageCreateFromPNG ($img) or // or PNG Image 
$im = @ImageCreateFromGIF ($img) or // or GIF Image 
$im = false; // If image is not JPEG, PNG, or GIF 

if (!$im) { 
    // We get errors from PHP's ImageCreate functions... 
    // So let's echo back the contents of the actual image. 
    readfile ($img); 
} else { 
    // Create the resized image destination 
    $thumb = @ImageCreateTrueColor ($dSize,$dSize); 
    // Copy from image source, resize it, and paste to image destination 
    imagecopyresampled($thumb, $im, 
    0, 0, 
    $xOff,$yOff, 
    $dSize, $dSize, 
    $dSize/$scale ,$dSize/$scale); 
} 
header('content-type:image/jpeg'); 
imagejpeg($thumb); 
//imagejpeg($im);