2

私はアップロードファイルを作成しましたが、コンテンツが画像であるかどうかを検証したい場合はスタックがあります。これは、拡張機能を検証する場合にのみ、偽のコンテンツと有効な拡張機能を操作できるためです。

javascript に getimagesize() php に似た関数はありますか?

4

1 に答える 1

2

最近のブラウザの中には、イメージをキャンバス コンテキストにロードして (これは実際にはページに表示する必要はありません)、そこから寸法を取得できるものがあります。

最初にサーバーにアップロードしなくても、画像を特定の色でスキャンできるように、構築した CMS で次のようなことを行いました。

$('section.content form input[name=image]').change(function(){
    file = this.files[0];

    if(file.type.match(/image.*/))
    {
        var img = document.createElement('img');
        // add this into an onload, as it takes time (albeit very little) to load the image into the DOM object
        img.onload = function(){
            canv = document.getElementById('product_image');
            if(!canv || !canv.getContext)
                return; // problem getting the canvas element

            // get the canvas context that we'll be using and the width of the canvas & image
            var ctx = canv.getContext('2d');
            var width = img.width;
            var height = img.height;
        };
        // put the file into the img object, triggering the img onload handler above
        var reader = new FileReader();
        reader.onload = function(e) {img.src = e.target.result};
        reader.readAsDataURL(file);
    }
});
于 2013-01-11T09:58:27.120 に答える