0

このスクリプトには奇妙なバグがあり、どこに行けばよいかわかりません。

ユーザーが画像をアップロードすると、それをトリミングするオプションが与えられます。トリミングしたら、PHP は画像のサイズを変更して保存する必要があります。

このスクリプトは、IE8、IE7、Chrome、Safari、Firefox などで問題なく動作します。IE9 では失敗します。単に画像のサイズを変更しません。500x300 の画像をアップロードしてトリミングしても、エラー メッセージは表示されませんが、500x300 の画像になります。

「if(imagejpeg($dst_r,$src,$jpeg_quality)){」チェックはtrueを返し、IE9でもリダイレクトしていますが、実際には画像を編集していません。

すべての投稿変数が通過します。print_r($_POST); を追加すると、すべての寸法が問題なく通過します。画像は FTP にアップロードされていますが、IE9 でのみサイズ変更されていません。

どんな助けでも大歓迎です。

<?php

    //assign src
    $src = $uploadStoragePath.$_SESSION['USER']['user_id'].'/'.$_SESSION['crop_me'];

    if ($_SERVER['REQUEST_METHOD'] == 'POST'){

        //set width/height
        $targ_w = $targ_h = 125;

        //set jpeg quality
        $jpeg_quality = 90;

        //open the jpeg
        $img_r = imagecreatefromjpeg($src);
        $dst_r = ImageCreateTrueColor( $targ_w, $targ_h );

        //crop the jpeg
        imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],$targ_w,$targ_h,$_POST['w'],$_POST['h']);

        //header('Content-type: image/jpeg');

        //save the file
        if(imagejpeg($dst_r,$src,$jpeg_quality)){
            $_SESSION['GORB']['message'] = "Profile Picture Updated!";
            $_SESSION['GORB']['tone'] = "happy";
            header('Location: '.$siteConfigMainUrl.'db/?view=edit_profile_picture');
        }else{
            echo 'Image failed';
        }   
    }
?>
<html>
    <head>
        <script src="<?php echo $siteConfigUrl; ?>display/js/jquery.min.js"></script>
        <script src="<?php echo $siteConfigUrl; ?>display/js/jquery.Jcrop.min.js"></script>
        <link rel="stylesheet" href="css/jquery.Jcrop.css" type="text/css" />

        <script language="Javascript">

            function updateCoords(c)
            {
                $('#x').val(c.x);
                $('#y').val(c.y);
                $('#w').val(c.w);
                $('#h').val(c.h);
            };

            function checkCoords()
            {
                if (parseInt($('#w').val())) return true;
                alert('Please select a crop region then press submit.');
                return false;
            };

        </script>
        <script type="text/javascript">

            jQuery(function($){

            // create variables (in this scope) to hold the API and image size
            var jcrop_api, boundx, boundy;

                $('#target').Jcrop({
                    onChange: updatePreview,
                    onSelect: updatePreview,
                    aspectRatio: 1,
                    onSelect: updateCoords
                },function(){
                    // use the API to get the real image size
                    var bounds = this.getBounds();
                    boundx = bounds[0];
                    boundy = bounds[1];
                    // store the API in the jcrop_api variable
                    jcrop_api = this;
                });

                function updatePreview(c){

                    if (parseInt(c.w) > 0){
                        var rx = 125 / c.w;
                        var ry = 125 / c.h;

                        $('#preview').css({
                            width: Math.round(rx * boundx) + 'px',
                            height: Math.round(ry * boundy) + 'px',
                            marginLeft: '-' + Math.round(rx * c.x) + 'px',
                            marginTop: '-' + Math.round(ry * c.y) + 'px'
                        });
                    }
                };
            });
        </script>
    </head>
    <body>
        <div class="article">   
            <img src="<?php echo $uploadStoragePath.$_SESSION['USER']['user_id'].'/'.$_SESSION['crop_me'];?>" id="target" />
            <br/>
            <h1>Preview:</h1>
            <div style="width:125px;height:125px;overflow:hidden;">
                <img src="<?php echo $uploadStoragePath.$_SESSION['USER']['user_id'].'/'.$_SESSION['crop_me'];?>" id="preview" alt="Preview" />
            </div>

            <form action="<?php echo $siteConfigUrl.'?view=crop_profile_picture'; ?>" method="POST" onsubmit="return checkCoords();">
                <input type="hidden" id="x" name="x" />
                <input type="hidden" id="y" name="y" />
                <input type="hidden" id="w" name="w" />
                <input type="hidden" id="h" name="h" />
                <input type="submit" value="Crop Image" />
            </form>
        </div>
    </body>
</html>
4

1 に答える 1

0

最後の日、私は同じ問題を抱えていました。これを使って解決します。これがうまくいくことを願っています:

    if ($_SERVER['REQUEST_METHOD'] == 'POST')
     {
$targ_w = $_POST['w'];$targ_h = $_POST['h'];
$jpeg_quality = 100;
$image = $_POST['image'];
$dir = $_POST['rel'];
$imagen = $dir."/".$image;
$src = $imagen;
$img_r = imagecreatefromjpeg($src);
$dst_r = ImageCreateTrueColor( $targ_w, $targ_h );

imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
$targ_w,$targ_h,$_POST['w'],$_POST['h']);

//header('Content-type: image/jpeg');
imagejpeg($dst_r, $dir.'/crop_'.$image.'', $jpeg_quality);

exit;
    }
于 2012-10-19T17:34:32.120 に答える