2009-09-03 208 views
0

我想剪裁圖像。有沒有可能這樣做。如果是的話,crop()函數將如何?使用php裁剪圖像

$imgURL始終是.jpg圖片。

$image = file_get_contents($imgURL); 
$maxWidth = 100; 
$height = 68; 

$image = crop($image, $maxWidth, $height); 

file_put_contents("media/imgname.jpg", $image); 

function crop($image, $maxWidth, $height){ 
    ...how do I crop the image? 
} 

回答

2

看看gd library這通常是大多數PHP安裝的一部分。

通常情況下,你最好:

  1. 導入使用的imagecreatefromTYPE功能之一
  2. 使用imagecreate($寬度,高度$),以使空白緩衝圖像
  3. 使用imagecopy的圖像()將想要的部分傳輸到緩衝區
  4. 使用圖像類型函數之一將緩衝區寫出到文件中。
0

如果您的服務器上沒有安裝GD,您可以使用的另一種選擇是Image Magick。

0

查找下面的代碼進行圖像裁切使用GD庫:

<?php 
function createThumb($upfile, $dstfile, $max_width, $max_height){ 

    $size = getimagesize($upfile); 

    $width = $size[0]; 

    $height = $size[1]; 



    $x_ratio = $max_width/$width; 

    $y_ratio = $max_height/$height; 

    if(($width <= $max_width) && ($height <= $max_height)) { 

      $tn_width = $width; 

      $tn_height = $height; 

    } elseif (($x_ratio * $height) < $max_height) { 

      $tn_height = ceil($x_ratio * $height); 

      $tn_width = $max_width; 

    } else { 

      $tn_width = ceil($y_ratio * $width); 

      $tn_height = $max_height; 

    } 



    if($size['mime'] == "image/jpeg"){ 

      $src = ImageCreateFromJpeg($upfile); 

      $dst = ImageCreateTrueColor($tn_width, $tn_height); 

      imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height); 

      imageinterlace($dst, true); 

      ImageJpeg($dst, $dstfile, 100); 

    } else if ($size['mime'] == "image/png"){ 

      $src = ImageCreateFrompng($upfile); 

      $dst = ImageCreateTrueColor($tn_width, $tn_height); 

      imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height); 

      Imagepng($dst, $dstfile); 

    } else { 

      $src = ImageCreateFromGif($upfile); 

      $dst = ImageCreateTrueColor($tn_width, $tn_height); 

      imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height); 

      imagegif($dst, $dstfile); 

    } 

} 

//usage 

if(isset($_FILES['upload_Image']['name']) && $_FILES['upload_Image']['name']!=='') { 
    $ext = substr($_FILES['upload_Image']['name'], strpos($_FILES['upload_Image']['name'],'.'), strlen($_FILES['upload_Image']['name'])-1); 

    $imgNormal = time().$ext; 

    $normalDestination = "Photos/Orignal/" . $imgNormal; 

    $httpRootLarge = "Photos/Large/" . $imgNormal; 

    $httpRootSmall = "Photos/Small/" . $imgNormal; 

    $httpRootThumb = "Photos/Thumb/" . $imgNormal; 

    move_uploaded_file($_FILES['upload_Image']['tmp_name'], $normalDestination); 



    createThumb($normalDestination,$httpRootLarge,680,604); #For 604x604 Image 

    createThumb($normalDestination,$httpRootSmall,500,300); #For 500x300 Image 

    createThumb($normalDestination,$httpRootThumb,130,100); #For 130x100 Image 

} 

?> 
0

只是試着從Jquery做JCROP插件

一個最好的插件用於裁剪圖像

deepliquid.com----download JCROP

function cropfun(){   
      $('#previews').Jcrop({ 
       aspectRatio: 3, 
       minSize:[300,100] , 
       boxWidth: 450, boxHeight: 400, 
       bgFade:true, 
       bgOpacity: .5, 
       setSelect: [ 60, 70, 600, 330 ], 
       onSelect: updateCoords 
      }); 

} 

function updateCoords(c) 
     { 
      $('#x').val(c.x); 
      $('#y').val(c.y); 
      $('#w').val(c.w); 
      $('#h').val(c.h); 
     }; 

     function checkCoords() 
     { 

      if (parseInt($('#w').val())) return true; 
      alert('Select where you want to Crop.'); 
      return false; 
     }; 

在主體部分

 <img src="uploads/<?php echo $image; ?>" id="previews" name="previews" onclick="cropfun();" /> 
    <input type="file" name="image22" id="image22" style="visibility:hidd"  > 
    <input type="hidden" id="hh" name="hh" value="" /> 
    <input type="hidden" id="hhh" name="hhh" value="<?php echo $image; ?>" /> 
     <input type="hidden" id="x" name="x" /> 
     <input type="hidden" id="y" name="y" /> 
     <input type="hidden" id="w" name="w" /> 
     <input type="hidden" id="h" name="h" /> 
     <input type="submit" name="submit" id="sub" value="submit" /> 
    </form> 
+0

他要了一個PHP不是一個JavaScript,尤其是不適用於jQuery解決方案。 – CodeBrauer 2016-05-19 13:48:33