2011-06-27 641 views
4

我想創建一個非常非常基本的上傳,調整大小和裁剪PHP腳本。 對於這個功能將是相同的(最後我檢查)Twitter的方法上傳頭像圖片的方法。用PHP上傳,調整圖像大小和裁剪圖像的中心

我想要腳本獲取任意大小的圖像,將最短邊調整爲116px,然後裁剪頂部和底部(或左右側(如果是橫向),以獲得116px的正方形116px。

我不希望客戶端調整大小或任何東西的臃腫的PHP腳本,只是一個簡單的PHP調整大小和裁剪。這是如何完成的?

+0

http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php – Frankie

+1

你做了什麼,這麼遠嗎?一些腳本? – ariefbayu

+0

我還沒有開始這個確切的項目,但我試圖做一些過去的項目,並無法弄清楚。我希望有人知道一些基本的代碼讓我開始。 – Adam

回答

2

如果你想要一個例子,從我的上傳工作,調整和作物類完成這一切加上其他一些很酷的東西 - 你可以使用它,如果需要或只取位的是你喜歡:

http://www.mjdigital.co.uk/blog/php-upload-and-resize-image-class/

我不認爲這是太臃腫! - 你可以做這個事情(未測試):

 
if((isset($_FILES['file']['error']))&&($_FILES['file']['error']==0)){ // if a file has been posted then upload it 
    include('INCLUDE_CLASS_FILE_HERE.php'); 
    $myImage = new _image; 
    // upload image 
    $myImage->uploadTo = 'uploads/'; // SET UPLOAD FOLDER HERE 
    $myImage->returnType = 'array'; // RETURN ARRAY OF IMAGE DETAILS 
    $img = $myImage->upload($_FILES['file']); 
    if($img) { 
     $myImage->newWidth = 116; 
     $myImage->newHeight = 116; 
     $i = $myImage->resize(); // resizes to 116px keeping aspect ratio 
     // get new image height 
     $imgWidth = $i['width']; 
     // get new image width 
     $imgHeight = $i['height']; 
     if($i) { 
      // work out where to crop it 
      $cropX = ($imgWidth>116) ? (($imgWidth-116)/2) : 0; 
      $cropY = ($imgHeight>116) ? (($imgHeight-116)/2) : 0; 
      $cropped = $myImage->crop(116,116,$cropX,$cropY); 
      if($cropped) { echo 'It Worked (I think!)'; print_r($cropped); 
      } else { echo 'Crop failed'; } 
     } else { echo 'Resize failed'; } 
    } else { echo 'Upload failed'; } 
+0

哇,這很簡單,比我發現的更簡單! – Adam

+0

謝謝 - 儘管我在原始文章中意識到我遺漏了裁剪方法的最後兩個參數 - D'oh!我已經修復了它現在所以希望它應該沒問題 - 以防萬一你需要裁剪方法應該使用:$ myImage->裁剪($ cropToWidth,$ cropToHeight,$ cropFromX,$ cropFromY); – mj7

4

有一個簡單易用,開源庫稱爲PHP Image Magician。它使用GD,但將其使用簡化爲3行。依據使用的

實施例:

$magicianObj = new imageLib('racecar.jpg'); 
$magicianObj -> resizeImage(100, 200, 'crop'); 
$magicianObj -> saveImage('racecar_small.png'); 
+0

迄今爲止最好的。很多非常容易使用的功能。謝謝!! – MazarD

0

我在此簡單的功能,這是非常容易使用,它可以讓你調整大小,裁剪和居中的圖像到特定的寬度和高度,它可以suppert JPG文件,PNG和GIF。隨意複製並粘貼到您的代碼:

function resize_imagejpg($file, $w, $h, $finaldst) { 

    list($width, $height) = getimagesize($file); 
    $src = imagecreatefromjpeg($file); 
    $ir = $width/$height; 
    $fir = $w/$h; 
    if($ir >= $fir){ 
     $newheight = $h; 
     $newwidth = $w * ($width/$height); 
    } 
    else { 
     $newheight = $w/($width/$height); 
     $newwidth = $w; 
    } 
    $xcor = 0 - ($newwidth - $w)/2; 
    $ycor = 0 - ($newheight - $h)/2; 


    $dst = imagecreatetruecolor($w, $h); 
    imagecopyresampled($dst, $src, $xcor, $ycor, 0, 0, $newwidth, $newheight, 
    $width, $height); 
    imagejpeg($dst, $finaldst); 
    imagedestroy($dst); 
    return $file; 


} 






function resize_imagegif($file, $w, $h, $finaldst) { 

    list($width, $height) = getimagesize($file); 
    $src = imagecreatefromgif($file); 
    $ir = $width/$height; 
    $fir = $w/$h; 
    if($ir >= $fir){ 
     $newheight = $h; 
     $newwidth = $w * ($width/$height); 
    } 
    else { 
     $newheight = $w/($width/$height); 
     $newwidth = $w; 
    } 
    $xcor = 0 - ($newwidth - $w)/2; 
    $ycor = 0 - ($newheight - $h)/2; 


    $dst = imagecreatetruecolor($w, $h); 
    $background = imagecolorallocatealpha($dst, 0, 0, 0, 127); 
    imagecolortransparent($dst, $background); 
    imagealphablending($dst, false); 
    imagesavealpha($dst, true); 
    imagecopyresampled($dst, $src, $xcor, $ycor, 0, 0, $newwidth, $newheight, 
    $width, $height); 
    imagegif($dst, $finaldst); 
    imagedestroy($dst); 
    return $file; 


} 



function resize_imagepng($file, $w, $h, $finaldst) { 

    list($width, $height) = getimagesize($file); 
    $src = imagecreatefrompng($file); 
    $ir = $width/$height; 
    $fir = $w/$h; 
    if($ir >= $fir){ 
     $newheight = $h; 
     $newwidth = $w * ($width/$height); 
    } 
    else { 
     $newheight = $w/($width/$height); 
    $newwidth = $w; 
    } 
    $xcor = 0 - ($newwidth - $w)/2; 
    $ycor = 0 - ($newheight - $h)/2; 


    $dst = imagecreatetruecolor($w, $h); 
    $background = imagecolorallocate($dst, 0, 0, 0); 
    imagecolortransparent($dst, $background); 
    imagealphablending($dst, false); 
    imagesavealpha($dst, true); 

    imagecopyresampled($dst, $src, $xcor, $ycor, 0, 0, $newwidth, 
    $newheight,$width, $height); 

    imagepng($dst, $finaldst); 
    imagedestroy($dst); 
    return $file; 


} 








function ImageResize($file, $w, $h, $finaldst) { 
     $getsize = getimagesize($file); 
     $image_type = $getsize[2]; 

     if($image_type == IMAGETYPE_JPEG) { 

     resize_imagejpg($file, $w, $h, $finaldst); 
     } elseif($image_type == IMAGETYPE_GIF) { 

     resize_imagegif($file, $w, $h, $finaldst); 
     } elseif($image_type == IMAGETYPE_PNG) { 

     resize_imagepng($file, $w, $h, $finaldst); 
     } 





} 

所有你需要做的使用它的是調用它像這樣:

ImageResize(image, width, height, destination); 

例如

ImageResize("uploads/face.png", 100, 150, "images/user332profilepic.png");