2011-06-27 365 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");