使用php和jquery裁剪图像

时间:2015-11-24 14:59:47

标签: php jquery image

此代码用于将裁剪的图像保存在同一位置。

  1. 在此特定点的代码裁剪图像中,datax是画布的x点
  2. 作物开始的最高位置和
  3. datay是画布顶部位置的y点和
  4. datawidth是画布的宽度
  5. dataheight是画布的高度
  6. img_scr文件路径
  7. 画布用于裁剪图像
  8. 以下是上述代码:

      $targ_w = 396; // set the width of the new image
      $targ_h =400; //set the height og the new image
    
      $src = $_POST['img_src']; //get the image source
      $img_r = imagecreatefromjpeg($src); //open the image
      $dst_r = imagecreatetruecolor( $targ_w,$targ_h); //create a new image
    
     imagecopyresampled($dst_r,$img_r,0,0,$_POST['datax'],$_POST['datay'],$targ_w,$targ_h,$_POST['datawidth'],$_POST['dataheight']); // create the new image with the specified width and height from jcrop
    
      unlink($src); //delete the old image
      imagejpeg($dst_r,$src,100); // save the new image
      imagedestroy($img_r);
    

    Before crop

    After Crop image is shrinking

1 个答案:

答案 0 :(得分:0)

请在下面的脚本中尝试裁剪图像。

<?php
if (isset($_FILES["file"])) {
    $tmpFile = $_FILES["file"]["tmp_name"];
    $fileName = 'file name';

    list($width, $height) = getimagesize($tmpFile);
    // check if the file is really an image
    if ($width == null && $height == null) {
        header("Location: index.php");
        return;
    }
    // resize if necessary
    if ($width >= 400 && $height >= 400) {
        $image = new Imagick($tmpFile);
        $image->thumbnailImage(400, 400);
        $image->writeImage($fileName);
    }
    else {
        move_uploaded_file($tmpFile, $fileName);
    }
}

这会有所帮助......