简体   繁体   中英

PHP image crop making black images

I've got a problem with this code. The code is a PHP page that receives information on where to crop the image. The sent information seems to be alright, it's coordinates (x1, y1, x2, y2), but the PHP code just makes a black image as a result. Although, it is the right scale, at least.

I am quite fresh when it comes to php, so sorry if this is something basic, but i just can't find an answer :/

image-cropping php:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $targ_w = $_POST['w'];
    $targ_h = $_POST['h'];
    $jpeg_quality = 90;

    $src = $_POST['img_file'];
    $img_r = imagecreatefromjpeg($src);
    $dst_r = ImageCreateTrueColor( $targ_w, $targ_h );

    imagecopyresampled($dst_r,$img_r,0,0,$_POST['x1'],$_POST['y1'],
    $targ_w,$targ_h,$_POST['w'],$_POST['h']);

    header('Content-type: image/jpeg');
    imagejpeg($dst_r,$src,$jpeg_quality);

    exit;
}
elseif($_SERVER['REQUEST_METHOD'] == 'GET'){
    $src_g = './demo2/pics/' . $_GET['id'] . 'v' . $_GET['v'] . '.jpg';
    if(!file_exists($src_g)){
        die();
    }
}
?>

Like I said in my comment you must set enctype to multipart/form-data in order for your form to support file uploads. Here is an example of a working form:

<form method="POST" enctype="multipart/form-data">
  <input type="file" name="file" id="file" />
  <input type="text" name="x1" value="0" />
  <input type="text" name="y1" value="0" />
  <input type="submit" value="Upload" />
</form>

Then you will check if the $_FILES array are set and then modify the image and output it to the browser:

// enable error reporting
error_reporting(E_ALL);
ini_set('display_errors', true);

if($_SERVER['REQUEST_METHOD'] == 'POST' && 
   isset($_FILES['file']) && 
   $_FILES['file']['error'] == 0) {

   // create an image resource from the temporary file
   $src = imagecreatefromjpeg($_FILES['file']['tmp_name']);
   // get dimensions of the source image
   $src_w = imagesx($src);
   $src_h = imagesy($src);

   // get offset to copy from 
   $src_x = intval($_POST['x1']);
   $src_y = intval($_POST['y1']);
   $dst_w = $src_w - $src_x;
   $dst_h = $src_h - $src_y;

   // create destination image 
   $dst = imagecreatetruecolor($dst_w, $dst_h);

   // copy the original image based on offset to destination
   // notice that we subtract the offset from the source width and hight
   // so we use `$dst_w` && `$dst_h`
   imagecopyresampled($dst, 
                      $src, 0, 0, 
                      $src_x, $src_y, 
                      $dst_w, $dst_h, 
                      $dst_w, $dst_h);
   // destroy resource
   imagedestroy($src);

   // output the image to the browser
   header('Content-type: image/jpeg');
   imagejpeg($dst);
   imagedestroy($dst);
   exit;
}

Notice that this is just a quick example and you should check for errors and so on. Like I also stated in my comment always enable error_reporting which often will give you information about what is going wrong. One other thing to keep in mind is that the code above assumes that the upload file is really a .jpg file which also is something that you probably would need to verify first.

Like you asked in your comment of course you can send a file location from the form. Then you will have to modify the code a bit:

if(isset($_POST['file_path']) && file_exists($_POST['file_path'])) {
   $src = $_POST['img_path'];
   $img_r = imagecreatefromjpeg($src);

Reference

POST method uploads in PHP

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM