Save crop image in php

I crop the image using imagecopy in php. but I want to keep it to my old way. what can i use for this? this is my method of cropping an image.

 $w=250; $h=300; $x=$_POST['x1']; $y=$_POST['y1']; $filename="upload/".$_SESSION['basename']; header('Content-type: image/jpg'); //header('Content-Disposition: attachment; filename='.$src); $image = imagecreatefromjpeg($filename); $crop = imagecreatetruecolor($w,$h); $new = imagecopy ($crop, $image, 0, 0, $x, $y, $w, $h); imagejpeg($crop); imagedestroy($filename); 

I used this method, but for me it did not work.

 $input = 'upload/'.$new; $output = 'upload/xxx.jpg'; file_put_contents($output, file_get_contents($input)); 
+4
source share
1 answer

Just indicate where you want to save the file

 imagejpeg($crop,$FILENAME); 

EDIT

Based on your comment below, the problem is not saving the file, but that you do not have the correct image resource.

 $image = imagecreatefromjpeg($filename); 

it should be

 $image = imagecreatefromjpeg($filename); if (!$image) exit("not a valid image"); 

You tell him to copy $ 250,300 $ x, $ y from an orignal image to a new image, but there was no check to make sure you have an image that is large enough to do this, or indeed that x and y exist or that they are less than 250 300

Also you are currently trying to use imagedestroy() file name. You can only imagedestroy image resource.

 imagedestroy($image); imagedestroy($crop); 
+3
source

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


All Articles