调整大小并裁剪图像

时间:2013-06-28 16:07:34

标签: php image

我想调整大小和裁剪图像。 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) ; 

3 个答案:

答案 0 :(得分: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

  }
}

答案 1 :(得分:0)

尝试使用croppie。 github网址为http://foliotek.github.io/Croppie/ 我用过它并且效果很好

答案 2 :(得分:-1)

您可以简单地使用PHP来获取图像并以HTML格式回显它们并为它们应用特定的宽度/高度。