简体   繁体   中英

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));

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);

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