Crop image in PHP creates an empty result

I'm just trying to crop a JPEG image (without scaling) using PHP. Here is my function as well as the input.

function cropPicture($imageLoc, $width, $height, $x1, $y1) {
    $newImage = imagecreatetruecolor($width, $height);

    $source = imagecreatefromjpeg($imageLoc);
    imagecopyresampled($newImage,$source,0,0,$x1,$y1,$width,$height,$width,$height);
    imagejpeg($newImage,$imageLoc,90);
}

When I call it as follows - cropPicture('image.jpg', 300, 300, 0, 0)- the function is executed correctly, but I still have a black image of 300x300 px in size (in other words, an empty canvas). Am I passing the wrong arguments?

The image exists and is being recorded.

+3
source share
2 answers

sobedai: , cropPicture(), . . false, ().

function cropPicture($imageLoc, $width, $height, $x1, $y1) {
  $newImage = imagecreatetruecolor($width, $height);
  if ( !$newImage ) {
    throw new Exception('imagecreatetruecolor failed');
  }

  $source = imagecreatefromjpeg($imageLoc);
  if ( !$source ) {
    throw new Exception('imagecreatefromjpeg');
  }

  $rc = imagecopyresampled($newImage,$source,0,0,$x1,$y1,$width,$height,$width,$height);
  if ( !$rc ) {
    throw new Exception('imagecopyresampled');
  }

  $rc = imagejpeg($newImage,$imageLoc,90);
  if ( !$rc ) {
    throw new Exception('imagejpeg');
  }
}

edit: http://docs.php.net/error_get_last. script ...

+3

:

imagecopyresampled imagejpeg

, , , .

, .

+2

Source: https://habr.com/ru/post/1738826/


All Articles