如何预览宽500px的图像。只要 ? , 我能怎么做?

时间:2013-11-26 06:17:39

标签: php jquery

............................................... ...........................................

当图像宽度不是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 // ?>

1 个答案:

答案 0 :(得分:0)

img.src = _URL.createObjectURL(offer_image_1);

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

正在显示,因为无论宽度如何,您都在设置图像源。 希望这会有所帮助。