Typora设置自动上传图片到图床

Typora设置自动上传图片到图床

方法一:使用php

打开设置界面:

自定义命令:

复制代码
php F:/WWW/php-library/TyporaUploadImage.php  ${filename}

php代码:

php 复制代码
# TyporaUploadImage.php
<?php
// Set the API endpoint URL
// $apiUrl = 'https://sm.ms/api/v2/upload';
$apiUrl = 'https://picui.cn/api/v1/upload';

// Set the API authorization header
// https://picui.cn/user/tokens bruce
$apiKey = '252|3n7rfRxagShT73kuJ8vNFrWpkd5XYsSH5uxxxx';  //这里输入你自己的apiKey
$headers = array(
    'Authorization:Bearer ' . $apiKey,
    'Accept: application/json',
    'Content-Type: multipart/form-data'
);

// Check if there are any image paths provided
if (count($argv) > 1) {
    echo "Upload Success:\n";
    for ($i = 1, $iMax = count($argv); $i < $iMax; $i++) {
        $imagePath = $argv[$i];
        $imageFileName = basename($imagePath);

        // Create the request data
        $data = array(
            'file' => new CURLFile($imagePath, mime_content_type($imagePath), $imageFileName)
        );

        // Initialize the cURL request
        $curl = curl_init($apiUrl);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

        // Send the cURL request and get the response
        $response = curl_exec($curl);

        // Check for any cURL errors
        if (curl_errno($curl)) {
            echo 'cURL error (' . $i . '): ' . curl_error($curl) . "\n";
            continue;
        }

        // Decode the JSON response
        $responseData = json_decode($response, true);

        // Check if the upload was successful
        if ($responseData['status']) {
            echo $responseData['data']['links']['url'] . "\n";
        } else {
            echo 'Error (' . $i . '): ' . $responseData['message'] . "\n";
        }

        // Close the cURL connection
        curl_close($curl);
    }
} else {
    echo "Usage: php script.php <image_path1> <image_path2> ...\n";
}
?>

做完这些以后就能自动上传图片了,上面这个方法需要本地有php应用程序。

方法二:使用powershell

如果没有php程序,就直接使用windows自带的powershell吧:

复制代码
powershell -File F:/WWW/php-library/TyporaUploadImage.ps1 ${filename}

代码内容:

powershell 复制代码
# TyporaUploadImage.ps1
# 设置 API 端点 URL 和 API 密钥
$apiUrl = "https://picui.cn/api/v1/upload"
$apiKey = "252|3n7rfRxagShT73kuJ8vNFrWpkd5XYsSH5uzhQxx"

# 设置 HTTP 头
$headers = @{
    "Authorization" = "Bearer $apiKey"
    "Accept" = "application/json"
}

# 检查是否提供了图片路径
if ($args.Count -eq 0) {
    Write-Host "Usage: script.ps1 <image_path1> <image_path2> ..."
    exit
}

Write-Host "Upload Success:"

foreach ($imagePath in $args) {
    # 检查文件是否存在
    if (-Not (Test-Path $imagePath)) {
        Write-Host "Error: File '$imagePath' does not exist."
        continue
    }

    # 获取文件名和 MIME 类型
    $imageFileName = [System.IO.Path]::GetFileName($imagePath)
    $mimeType = Get-Content -Path $imagePath -AsByteStream | 
        . { New-Object System.IO.MemoryStream($_) } |
        . { Add-Type -AssemblyName System.Drawing; New-Object System.Drawing.Bitmap($_) } |
        . { $_.RawFormat.ToString() }

    # 创建 POST 数据
    $boundary = [System.Guid]::NewGuid().ToString()
    $contentType = "multipart/form-data; boundary=$boundary"

    $formData = @"
--$boundary
Content-Disposition: form-data; name="file"; filename="$imageFileName"
Content-Type: $mimeType

$(Get-Content -Raw -Path $imagePath)
--$boundary--
"@

    # 发送 HTTP POST 请求
    try {
        $response = Invoke-RestMethod -Uri $apiUrl -Method Post -Headers $headers -ContentType $contentType -Body $formData
        if ($response.status -eq $true) {
            Write-Host $response.data.links.url
        } else {
            Write-Host "Error: " $response.message
        }
    } catch {
        Write-Host "Error uploading file '$imagePath': $_"
    }
}
相关推荐
2501_9160088922 分钟前
手机 iOS 系统全解析,生态优势、开发机制与跨平台应用上架实践指南
android·ios·智能手机·小程序·uni-app·iphone·webview
2501_915918413 小时前
App 使用 HTTPS 的工程化实战,从接入到真机排查的一线指南
android·ios·小程序·https·uni-app·iphone·webview
恋猫de小郭4 小时前
第一台 Andriod XR 设备发布,Jetpack Compose XR 有什么不同?对原生开发有何影响?
android·前端·flutter
allk554 小时前
List && Map在安卓中的优化
android·数据结构·性能优化·list·map
.豆鲨包5 小时前
【Android】从源码角度理解Handler机制
android
杨筱毅5 小时前
【Android】Handler/Looper机制相关的类图和流程图
android·java·流程图
Kapaseker6 小时前
酷炫的文字效果 — Compose 文本着色
android·kotlin
努力进修6 小时前
【JavaEE初阶】 多线程编程核心:解锁线程创建、方法与状态的创新实践密码
android·java·java-ee
生莫甲鲁浪戴7 小时前
Android Studio新手开发第二十八天
android·ide·android studio
zhaoyufei1337 小时前
Android触屏TP驱动事件上报以及多点触摸
android