簡體   English   中英

使用 PHP 上傳、調整大小和裁剪圖像中心

[英]Upload, resize, and crop center of image with PHP

我想創建一個非常基本的上傳、調整大小和裁剪 PHP 腳本。 此功能將與 Twitter 用於上傳頭像圖片的方法相同(最后我檢查過)。

我希望腳本拍攝任何尺寸的圖像,將最短邊的大小調整為 116px,然后裁剪頂部和底部(如果是橫向,則裁剪掉左右邊)以獲得 116px x 116px 的正方形。

我不想要一個臃腫的 PHP 腳本與客戶端調整大小或任何東西,只是一個簡單的 PHP 調整大小和裁剪。 這是怎么做到的?

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

基礎用法示例:

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

GD 庫是一個很好的起點。

http://www.php.net/manual/en/book.image.php

如果你想從我上傳的示例中工作,調整大小和裁剪 class 完成所有這些以及其他一些很酷的東西 - 如果需要,你可以全部使用它,或者只是取出你喜歡的部分:

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'; }

我制作了這個簡單的 function,它非常易於使用,它允許您將圖像調整大小、裁剪和居中到特定的寬度和高度,它可以支持 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");

暫無
暫無

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

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