Skip to content

Checking your reading list…

Retry verification

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

Implementing Photo Capture and Recognition in a WeChat Mini Program with PHP Backend and MySQL Storage

Implementing Photo Capture and Recognition in a WeChat Mini Program with PHP Backend and MySQL Storage

This tutorial explains how to build a WeChat Mini Program that captures photos, sends them to a PHP backend for Baidu AI image recognition, and stores the results in a MySQL database, covering front‑end UI, JavaScript logic, and server‑side processing.

WeChat Mini Programs are popular for scenarios like e‑commerce and social networking; this guide walks through building a photo capture and recognition feature from scratch and sending the result to a PHP backend for MySQL storage.

1. Create the Mini Program Open the WeChat Developer Tools, start a new project, and add basic styling to app.wxss (background, font size, colors, etc.).

page {
  background-color: #f7f5f2;
  font-size: 16px;
  color: #333;
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

2. Design the Interface Add two buttons—one for taking a photo and one for uploading—in index.wxml.

<view class='camera-wrapper'>
  <camera id="camera" mode="takePhoto" device-position="back"></camera>
  <view class="btn-capture" catchtap="capturePhoto">拍照</view>
</view>
<view class="btn-upload" catchtap="uploadPhoto">上传</view>

The capture button triggers capturePhoto and the upload button triggers uploadPhoto.

3. Implement Capture and Upload In camera.js define the two functions. capturePhoto creates a camera context, calls takePhoto, and stores the temporary image path. uploadPhoto uses wx.uploadFile to send the image to a PHP script, optionally adding extra form data.

const api = 'http://localhost/api/upload.php';
Page({
  data: {},
  capturePhoto: function () {
    const ctx = wx.createCameraContext();
    ctx.takePhoto({
      quality: 'high',
      success: (res) => {
        this.setData({ src: res.tempImagePath });
      }
    });
  },
  uploadPhoto: function () {
    wx.uploadFile({
      url: api,
      filePath: this.data.src,
      name: 'file',
      formData: { user: 'test' },
      success: (resp) => { console.log(resp); }
    });
  }
});

4. PHP Backend Processing Create upload.php to receive the file, call Baidu AI for image recognition, and insert the image name and recognition result into a MySQL table.

<?php
header('Access-Control-Allow-Origin: *');
header("Content-type:text/html;charset=utf-8");
$file = $_FILES['file']['tmp_name'];
$url = "http://localhost:8081/?url=" . urlencode('data:image/jpeg;base64,' . base64_encode(file_get_contents($file)));
$content = json_decode(file_get_contents($url), true);
mysql_connect("localhost", "root", "123456");
mysql_select_db("test");
mysql_query("INSERT INTO test_tbl(name, value) values ('" . $_FILES['file']['name'] . "', '" . $content['result'] . "')");
mysql_close();
echo "上传成功<br/>";
echo "图片名称:" . $_FILES['file']['name'] . "<br/>";
echo "识别结果:" . $content['result'] . "<br/>";
?>

The script sends the image to Baidu AI, stores the result, and returns a success message.

5. Summary The article demonstrates a complete end‑to‑end solution for photo capture, image recognition, and data persistence in a WeChat Mini Program, while highlighting the need for security measures when exposing upload endpoints.

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

Mobile DevelopmentWeChat Mini-ProgramMySQLPHPPhoto Capture

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion