1

我正在使用 jcrop 来裁剪图像。
但生成的裁剪图像无法保存,我的意思是当我右键单击并保存图像时,它会保存 .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;
}
4

1 回答 1

1
  • 在您的 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_zise,你可以从php.netheader("Content-length:" . $f_size);使用这个函数getSizeFile

请测试一下...

于 2013-02-23T22:04:34.070 回答