Help with script - arithmetic jpeg error


<?php

if ( isset ( $GLOBALS["HTTP_RAW_POST_DATA"] )) {

    $im = imagecreatefromstring ($GLOBALS["HTTP_RAW_POST_DATA"]);
    
    //header("Pragma: no-cache"); 
    header('Content-Type: image/jpeg');
    header("Content-Disposition: attachment; filename=".$_GET['name'].".jpg");
    $created = imagepng($im);
    imagedestroy($im);
    $image = imagecreatefrompng($created);
    imagedestroy($created); 
    imagejpeg($image, '', 100);
    
}  else echo 'An error occured.';

?>

this is the code im using. Converting a png to jpeg is the only workaround to what I’m doing atm which does the job. But, when I try opening the jpeg in Photoshop or any photo editing program it throws an error. It will open up in Preview and browsers though.

Any ideas what’s wrong there?

Thanks