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) ; 
4

3 回答 3

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

  }
}
于 2013-06-28T16:10:01.400 回答
0

尝试使用作物。github url 是http://foliotek.github.io/Croppie/ 我用过,效果很好

于 2016-11-27T07:44:41.803 回答
-1

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

于 2013-06-28T16:09:33.193 回答