import React, { Component } from 'react' import UploadImage from './lib/uploadImage.component' import { post } from './util' const initialState = { 'image': null, 'res': null, 'loading': false, } export default class PhashApp extends Component { state = { ...initialState } upload(blob) { if (this.state.image) { URL.revokeObjectURL(this.state.image) } const url = URL.createObjectURL(blob) this.setState({ image: url, loading: true }) const fd = new FormData() fd.append('q', blob) post('/api/v1/match', fd) .then(res => { console.log(res) this.setState({ res, loading: false }) }) .catch(err => { console.log(err) this.setState({ loading: false }) }) } submit() { const { url } = this.state if (!url || url.indexOf('http') !== 0) return } render() { return (

Perceptual Hash Demo

{this.renderQuery()} {this.renderResults()}
) } renderQuery() { const { image } = this.state const style = {} if (image) { style.backgroundImage = 'url(' + image + ')' style.backgroundSize = 'cover' style.opacity = 1 } return (
{image &&
}
) } renderResults() { const { loading, res } = this.state if (!res) { return (
) } if (loading) { return (
Loading...
) } const { success, error, match, closest_match } = res if (!success) { return (
Error: {error}
) } if (!match) { return (
No match, image added to database
) } const { ext, phash, score, sha256 } = closest_match return (
Closest match: {sha256}{'.'}{ext}
Score: {score}
Phash: {phash.toString(16)}
) } }