Nytram
Nytram

Reputation: 343

Save Crop image in php

I crop image using imagecopy in php. but I want save it to my old path. what can I used for it ? this is my method how i crop the 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 it was not work for me.

$input = 'upload/'.$new;
    $output = 'upload/xxx.jpg';
    file_put_contents($output, file_get_contents($input));

Upvotes: 3

Views: 1653

Answers (1)

Anigel
Anigel

Reputation: 3437

Just specify where you want it to save the file

imagejpeg($crop,$FILENAME);

EDIT

Based on your comment below, the problem is not saving the file , it is that you do not have a valid image resource to begin with.

$image = imagecreatefromjpeg($filename);

should be

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

You are telling it to copy $x,$y to 250,300 from the orignal image into the new image, but there has been no check to ensure that you have an image that is big enough to do that, or indeed that x and y exist or that they are less than 250,300

Also you care currently trying to imagedestroy() a filename. You can only imagedestroy an image resource.

imagedestroy($image);
imagedestroy($crop);

Upvotes: 3

Related Questions