如何预览500像素宽的图像.只有我该怎么办


How to preview image that wide 500px. only ? , How can i do?

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

当图像宽度不是500像素时,为什么图像仍然预览,我该怎么办?

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<?PHP // start script for preview image before upload // ?>
<script language="JavaScript">
    function showPreview_1(ele)
    {
            $('#imgAvatar_1').attr('src', ele.value); // for IE
            if (ele.files && ele.files[0]) {
                var reader = new FileReader();              
                reader.onload = function (e) {
                    $('#imgAvatar_1').attr('src', e.target.result);
                }
                reader.readAsDataURL(ele.files[0]);
            }
    }
</script>
<?PHP // end script for preview image before upload // ?>

Thumbnail image : 
<img id="imgAvatar_1" style=" max-height: 100px; max-width: 100px; border: 1px solid #ccc;" >
<input name="offer_image_1" type="file" id="offer_image_1" OnChange="showPreview_1(this)" />


<?PHP // start script for check wide image before upload // ?>
<script type="text/javascript">
var _URL = window.URL || window.webkitURL;
$("#offer_image_1").change(function(e) {
    var offer_image_1, img;
    if ((offer_image_1 = this.files[0])) {
        img = new Image();
        img.onload = function() {
            if (this.width != '500')
            {
            alert("not width 500 px.");
            $('input').val("");
            }
        };
        img.src = _URL.createObjectURL(offer_image_1);
    }
});
</script>
<?PHP // end script for check wide image before upload // ?>

放入

img.src = _URL.createObjectURL(offer_image_1);

其他部分的行

if (this.width != '500')
{
    alert("not width 500 px.");
    $('input').val("");
}

之所以显示它,是因为无论宽度是多少,都要设置图像源。希望这能有所帮助。