dongping1689 2017-08-01 12:03
浏览 45

将图像文件从php传递到javascript

I have a database in which there are images stored as LONGBLOB files. It seems I can't figure out how to "transport" those images to JS file so I could check their dimensions and compare them with the dimensions of the screen. Here is how I donwload the images:

function fillArrays(){
$idArray = array();
    $sql = "SELECT oglas_id,slika,prioriteta FROM deska WHERE deska.datumz <= CURRENT_DATE AND deska.datumk >= CURRENT_DATE;";
    $result = mysqli_query($GLOBALS['conn'],$sql);
    $resultCheck = mysqli_num_rows($result);
    if($resultCheck>0){
        while($row = mysqli_fetch_assoc($result)){
            array_push($GLOBALS['idArray'],$row['oglas_id']);
            $image = base64_encode($row['slika']);    // THIS        "slika" means "image" in my lang...
            array_push($GLOBALS['imgArray'],$image);        //AND THIS
            array_push($GLOBALS['prArray'],$row['prioriteta']);
        }
    }else{
        die("Oops there seems to be an error(arr)");
    }

}

I am preety sure everything in lines of SQL is correct. The problem begins when I try to get the images stored in array into javascript. I am trying to do it by json_encode.

<script>
        var idArray = <?php echo json_encode($idArray) ?>;
        var imgArray = <?php echo json_encode($imgArray) ?>;
        var prArray = <?php echo json_encode($prArray) ?>;
    </script>
    <script src="includes/script.js"></script>

Then we jump to my javascript file ( script.js ). Now when I try to output the dimensions of the image selected, it says undefined undefined (width*height). Here is how I try to obrain the info of the image selected. in my JS file:

var imgArray = window.imgArray;

var img = new Image();
img = imgArray[0];
img.width;
img.height;

I am aware the questions has been asked many times but I can't seem to wrap my head around it so I figured I would supply you guys with my concrete problem. Thanks to anyone who helps.

Since I dont think people fully understand:

Aim of the site:

  • Download images from database.
  • Resize them.(IMAGE CANNOT BE LARGER THAN HALF OF THE SCREEN SIZE)
  • Place them in the 2x2 table.
  • 写回答

2条回答 默认 最新

  • dpwdldgn43486 2017-08-01 12:23
    关注

    You need to wait for the loaded image.

    img.onload = function () {
      alert(this.width + ' ' + this.height);
    };
    

    Btw, if you need to resize the image, why don't you pass the screen resolution to PHP and then, there, you do all your checks? It's easier, because if you do this from JS, it depends on user browser.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动