为什么Image Header不允许我保存图像


Why Image Header not letting me save image?

我正在使用jcrop制作图像裁剪
但是生成的croped图像是不可保存的,我的意思是,当我右键单击并保存图像时,它会保存.php文件
这是使用的代码:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$targ_iw = $_REQUEST['iwidth'];
$targ_ih = $_REQUEST['iheight'];
$source = $_REQUEST['tname'];
if(empty($targ_iw)){
    $targ_iw = $_POST['w'];
    $targ_ih = $_POST['h'];
    }
    $jpeg_quality = 90;
    $src = 'http://www.imageopti.com/crop/files/'.$source;
    $img_r = imagecreatefromjpeg($src);
    $dst_r = ImageCreateTrueColor( $targ_iw, $targ_ih );
    imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
    $targ_iw,$targ_ih,$_POST['w'],$_POST['h']);
    header('Content-type: image/jpeg');
    imagejpeg($dst_r, null, $jpeg_quality);
    exit;
}
  • 在HTML代码中,添加

    <input type="hidden" name="create" value="true" />
    
  • 添加我的功能

    function new_my_resampled($source, $iwidth, $iheight, $w, $h, $x, $y)
    {
        $filename   = "http://www.imageopti.com/crop/files/" . $source;
        $targ_iw = $iwidth;
        $targ_ih = $iheight;
        if(empty($targ_iw))
        {
            $targ_iw = $w;
            $targ_ih = $h;
        }
        $image = imagecreatetruecolor($targ_iw,$targ_ih);
        $o_img = imagecreatefromjpeg($filename);
        imagecopyresampled($image,$o_img,0,0,$x,$y,$targ_iw,$targ_ih,$w,$h);
        imagejpeg($image, null, 100);
    }
    
  • 在php文件中添加

    if($_POST['create'] == 'true')
    {
        /*
         * Write here your conditions
         */
        $filename = "http://www.imageopti.com/crop/files/" . $_POST['tname'];
        $f_name   = trim(basename($filename));
        //$f_size   = getSizeFile($filename);
        header("(anti-spam-content-type:) image/jpeg");
        header('Cache-control: max-age=31536000');
        header('Expires: ' . gmdate('D, d M Y H:i:s', (time() + 31536000)) . ' GMT');
        header('Content-transfer-encoding: binary');
        header('ETag: "' . time() . '"');
        header("Content-Disposition: attachment; filename=" . $f_name);
        header("Content-type: image/jpeg");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header("Content-Description: File Transfer");  
        //header("Content-length:" . $f_size);
        header("Cache-control: private"); 
        new_my_resampled($_POST['tname'], $_POST['iwidth'], $_POST['iheight'], $_POST['w'], $_POST['h'],$_POST['x'],$_POST['y']);
    }
    
  • !!!如果您想使用$f_ziseheader("Content-length:" . $f_size);,可以使用php.net中的函数getSizeFile

请测试一下。。。