Get pixel color from an image

后端 未结 2 1489
北恋
北恋 2020-12-04 15:47

I have an image on a browser.

I want to get the top left pixel of the image color (at coordinates: 0,0), no matter whether the image is rotated or not.

How

2条回答
  •  再見小時候
    2020-12-04 16:25

    For an image on a browser you can't use PHP unless you can transfer the image to a server first.

    n the browser, if you can draw the image in a canvas you could use the getImageData() method:

    var myImg = new Image();
    myImg.src = 'image.jpg';
    var context = document.getElementById('canvas').getContext('2d');
    context.drawImage(myImg, 0, 0);
    var data = context.getImageData(x, y, 1, 1).data;
    

    You'd have to allow for any rotation - presumably you know what rotation has been applied.

提交回复
热议问题