florinnichifiriuc/stable-diffusion-php-client
Composer 安装命令:
composer require florinnichifiriuc/stable-diffusion-php-client
包简介
A PHP client library for Stable Diffusion API
README 文档
README
A PHP client library for interacting with the Stable Diffusion API. This library allows you to integrate Stable Diffusion's text-to-image generation capabilities into your PHP applications, including Laravel projects.
Table of Contents
- Features
- Requirements
- Installation
- Configuration
- Usage
- Advanced Usage
- Error Handling
- Contributing
- License
Version 1.0.3
Features
- Interact with the Stable Diffusion API using PHP.
- Generate images from text prompts (
txt2img). - Generate images from existing images (
img2img). - Inpainting support with masking (
inpaint). - Retrieve available models and samplers.
- Asynchronous image generation (
txt2imgAsync). - Progress tracking during image generation.
- Fetch and update configurations.
Requirements
- PHP 7.4 or higher.
- Composer.
- Guzzle HTTP client (automatically installed via Composer).
- Locally running Stable Diffusion instance with API enabled.
Installation
1. Install via Composer
Run the following command in your project's root directory:
composer require florinnichifiriuc/stable-diffusion-php-client
Replace florinnichifiriuc/stable-diffusion-php-client with the actual package name as published on Packagist.
2. Set Up Stable Diffusion API
Ensure that your Stable Diffusion instance is running with the API enabled.
Steps:
-
Install Stable Diffusion with Automatic1111 Web UI if you haven't already.
-
Enable the API by starting the Web UI with the
--apiflag:python launch.py --api
- By default, the API runs at
http://127.0.0.1:7860.
- By default, the API runs at
Configuration
Environment Variables
In your project's .env file (or equivalent configuration), add the following line:
STABLE_DIFFUSION_API_URL=http://127.0.0.1:7860
Usage
Basic Example
<?php require 'vendor/autoload.php'; use Florinnichifiriuc\StableDiffusionPhpClient\StableDiffusionClient; $sdClient = new StableDiffusionClient(); $params = [ 'prompt' => 'A beautiful sunset over the mountains', 'steps' => 30, 'cfg_scale' => 7.5, ]; $images = $sdClient->txt2img($params); if ($images) { file_put_contents('generated_image.png', base64_decode($images[0])); }
Using Models
$sdClient->setModel('v1-5-pruned-emaonly');
Additional Methods
img2img()– Create images from existing images.inpaint()– Perform inpainting using masks.txt2imgAsync()– Perform asynchronous image generation.getProgress()– Fetch progress of image generation.getConfig()– Fetch current configuration.setConfig()– Set configurations like default resolution or sampler.fetchResultByTaskId()– Get the result of async image generation using task ID.
Laravel Integration
Controller Example
File: app/Http/Controllers/ImageGenerationController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Florinnichifiriuc\StableDiffusionPhpClient\StableDiffusionClient; use Illuminate\Support\Facades\Storage; class ImageGenerationController extends Controller { public function generateImage(Request $request) { $request->validate([ 'prompt' => 'required|string|max:1000', 'steps' => 'nullable|integer|min:1|max:150', 'cfg_scale' => 'nullable|numeric|min:1|max:30', ]); $params = [ 'prompt' => $request->input('prompt'), 'steps' => $request->input('steps', 20), 'cfg_scale' => $request->input('cfg_scale', 7.5), ]; $sdClient = new StableDiffusionClient(); try { $images = $sdClient->txt2img($params); if ($images) { $decodedImage = base64_decode($images[0]); // Save the image to storage (e.g., public disk) $filename = 'generated_' . time() . '.png'; Storage::disk('public')->put($filename, $decodedImage); return response()->json(['image_url' => Storage::url($filename)], 200); } else { return response()->json(['error' => 'Failed to generate image'], 500); } } catch (\Exception $e) { return response()->json(['error' => $e->getMessage()], 500); } } }
Available Methods
txt2img(array $params)– Generates images from text prompts.img2img(array $params, string $inputImage)– Generates images from an existing input image.inpaint(array $params, string $inputImage, string $maskImage)– Performs inpainting on an image using a mask.txt2imgAsync(array $params)– Asynchronously generates images and returns a task ID.fetchResultByTaskId(string $taskId)– Fetches the result of asynchronous image generation.getModels()– Retrieves the available models installed.setModel($modelTitle)– Sets the model for image generation.getSamplers()– Fetches available samplers.getUpscalers()– Fetches available upscalers.getProgress()– Retrieves progress for ongoing image generation.getConfig()– Fetches current configuration settings.setConfig(array $config)– Updates the configuration settings (e.g., default resolution, sampler).
Error Handling
The library throws exceptions when:
- The specified model does not exist (
InvalidArgumentException). - HTTP requests fail or the API returns an error.
Contributing
Contributions are welcome! If you encounter bugs or have feature requests, please open an issue or submit a pull request on GitHub.
Development Setup
-
Clone the Repository:
git clone https://github.com/florinnichifiriuc/stable-diffusion-php-client.git
-
Install Dependencies:
composer install
License
This project is licensed under the MIT License. See the LICENSE file for details.
florinnichifiriuc/stable-diffusion-php-client 适用场景与选型建议
florinnichifiriuc/stable-diffusion-php-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 46 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 10 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 florinnichifiriuc/stable-diffusion-php-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 florinnichifiriuc/stable-diffusion-php-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 46
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-10-01