The solution provided by vlzvl works well. However, using this solution, you can also overwrite the image by changing the order of the code.
$image = imagecreatefromjpeg("image.jpg"); unlink("image.jpg"); imagejpeg($image,"image.jpg",50);
This allows you to compress a pre-existing image and save it in the same place with the same file name.
Daniel
source share