2013-06-28 195 views
0

我想調整大小和裁剪圖像。 250x250的調整大小和裁剪圖像

我可以調整它的大小:

$newfilename = "image.jpg"; 
    if(isset($_POST['submit'])){ 
     if (isset ($_FILES['new_image'])){ 
      $imagename = $newfilename; 
      $source = $_FILES['new_image']['tmp_name']; 
      $target = "img/".$imagename; 
      move_uploaded_file($source, $target); 

      $imagepath = $imagename; 
      $save = "img/" . $imagepath; 
      $file = "img/" . $imagepath; 

      list($width, $height) = getimagesize($file) ; 


      $modwidth = 250; 

      $diff = $width/$modwidth; 

      $modheight = $height/$diff; 
      $tn = imagecreatetruecolor($modwidth, $modheight) ; 
      $image = imagecreatefromjpeg($file) ; 
      imagecopyresized($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 

      imagejpeg($tn, $save, 100) ; 

     } 
    } 

但我不知道該怎麼調整後裁剪。 它裁剪圖像。如果我有像10000x10000這是錯誤的決定

imagecopyresized($tn, $image, 0, 0, 0, 0, 250, 250, 250, 250) ; 

回答

-1

您可以簡單地使用PHP來獲取圖像和HTML呼應他們和特定寬度/高度適用於他們。

+0

這並不是他所要求的,說實話,這是一個非常懶惰和浪費的方式來做到這一點。 –

+0

比例是多少? – Dobrozhelatel

0

使用imagecreatetruecolor創建指定裁剪尺寸的新圖像,然後使用imagecopy將其從舊版複製到新版畫布。

實施例(未測試):

$newfilename = "image.jpg"; 
if(isset($_POST['submit'])){ 
    if (isset ($_FILES['new_image'])){ 
     $imagename = $newfilename; 
     $source = $_FILES['new_image']['tmp_name']; 
     $target = "img/".$imagename; 
     move_uploaded_file($source, $target); 

     $imagepath = $imagename; 
     $save = "img/" . $imagepath; 
     $file = "img/" . $imagepath; 

     list($width, $height) = getimagesize($file) ; 


     $modwidth = 250; 

     $diff = $width/$modwidth; 

     $modheight = $height/$diff; 
     $tn = imagecreatetruecolor($modwidth, $modheight) ; 
     $cropped = imagecreatetruecolor(250, 250); 
     $cropLeft = 0; // start from left 
     $cropTop = 0; // start from top 

     $image = imagecreatefromjpeg($file) ; 
     imagecopyresized($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 
     imagecopy($cropped, $image, 0, 0, $cropLeft, $cropTop, $modwidth, $modheight); 

     imagejpeg($cropped, $save, 100); // save cropped image 
     // imagejpeg($tn, $save, 100) ; // save resized image 

    } 
} 
+0

它創建http://clip2net.com/s/5j0dZt – Dobrozhelatel

+0

設置'$ cropped = imagecreatetruecolor($ modwidth,$ modheight);' –

+0

它只會裁剪原始圖像 – Dobrozhelatel