Malakiof
Malakiof

Reputation: 108

Php upload file then crop

i'm on a cordova project. i use Jquery and the Jcrop plugin and Php gd in my server side. When i upload user's avatar i try to crop the picture. The upload is going well, but nothing happens for the crop.

the php code:

    if(move_uploaded_file($file,$dirname."/avatar.jpeg")){
        $targ_w = $targ_h = 150;
        $src =   $dirname.'/avatar.jpeg';
        $img_r = imagecreatefromjpeg($src);
        $dst_r = ImageCreateTrueColor( $targ_w, $targ_h );
        imagecopyresampled($dst_r,$img_r,0,0,$_POST['avatar_x'],$_POST['avatar_y'], $targ_w,$targ_h,$_POST['avatar_w'],$_POST['avatar_h']);
    }else{...}

i have no error message

Upvotes: 0

Views: 62

Answers (1)

nullability
nullability

Reputation: 10677

Nothing happens because you're not doing anything with the result. See the documentation for imagecopyresampled:

imagecopyresampled() will take a rectangular area from src_image [...] and place it in a rectangular area of dst_image.

After running this function, the result will be in $dst_r, which is still an in-memory image resource. You now have to save it to a file. This can be done with the imagejpeg function, for example:

imagejpeg($dst_r, "$dirname/avatar_cropped.jpeg");

Upvotes: 2

Related Questions