Documentation menu

Image to Image

Upscale photos with Swin2SR or redraw them from a prompt with Stable Diffusion, locally from PHP. CLI and PHP examples, strength and options.

Enlarge and clean up photos, or redraw an image following a prompt, with a local image-to-image model.

Setup#

Terminal
vendor/bin/loves-ai setup image-to-image
vendor/bin/loves-ai pull caidas/swin2SR-classical-sr-x2-64

Supported models#

Two kinds of models work, and the runner tells them apart by the model's own files:

  • Upscaling models, e.g. caidas/swin2SR-classical-sr-x2-64 (2× larger) or caidas/swin2SR-realworld-sr-x4-64-bsrgan-psnr (4×). They enlarge a photo and clean it up, and take no prompt. Use them to make images bigger than a plain resize can.
  • Diffusers image-to-image pipelines, e.g. stabilityai/sd-turbo or timbrooks/instruct-pix2pix. They redraw the image following a prompt, keeping more or less of the original depending on --strength.

For plain resizing to a smaller size, PHP's own GD or Imagick extension is faster and needs no model.

From the command line#

Upscale a photo 2×:

Terminal
vendor/bin/loves-ai image-to-image caidas/swin2SR-classical-sr-x2-64 photo.jpg --output=photo-2x.png
Output
🔎 Making it bigger and better with caidas/swin2SR-classical-sr-x2-64… How about a hot chocolate while you wait? ☕
If you wish to see all logs, re-run the command with the "--debug" option.
🎉 Image saved to /var/www/my-app/photo-2x.png

Redraw a photo as a watercolor painting:

Terminal
vendor/bin/loves-ai pull stabilityai/sd-turbo
vendor/bin/loves-ai image-to-image stabilityai/sd-turbo photo.jpg --prompt="a watercolor painting" --strength=0.6 --steps=2 --guidance=0

Options#

Option Meaning
--output=PATH Image file to write (default: a timestamped .png in output_dir)
--prompt=TEXT What the result should look like; needed by diffusers models, refused by upscaling models
--negative-prompt=TEXT What the result should not contain (diffusers models)
--strength=N How much of the original to change, 0 to 1; higher changes more (diffusers models)
--steps=N Inference steps (default: the pipeline's own)
--guidance=SCALE Guidance scale; turbo models use 0 (default: the pipeline's own)
--seed=N Random seed, for reproducible images
--device=DEVICE cpu, cuda, mps… (default: the best available)
--log-file=PATH Append the runner's output to this file
--debug Show the runner's output while working

Defaults come from config/image-to-image.php (output_dir, log_file).

From PHP#

PHP
use PhpLovesAi\Runner\ImageToImage;

// Finds the runner and the pulled model in the project's .local directory by itself.
$imageToImage = new ImageToImage();

$bigger = $imageToImage->transform(
    model: 'caidas/swin2SR-classical-sr-x2-64',
    imagePath: storage_path('app/photo.jpg'),
    outputPath: storage_path('app/photo-2x.png'),
);

$painting = $imageToImage->transform(
    model: 'stabilityai/sd-turbo',
    imagePath: storage_path('app/photo.jpg'),
    outputPath: storage_path('app/painting.png'),
    prompt: 'a watercolor painting',
    strength: 0.6,
    steps: 2,
    guidanceScale: 0.0,
);

Errors#

  • ImageNotFoundException when the image does not exist
  • BinaryNotInstalledException when setup image-to-image has not been run
  • ModelNotFoundException when the model was not pulled yet
  • UnsupportedModelException when the model does not produce images
  • RunFailedException, with the runner's error output, when the run fails, e.g. when a prompt is missing or given to a model that takes none

Performance#

Upscaling works on the whole image at once, so memory use grows with the picture: a large photo can need several GB. Enlarge in a queue job, and shrink very large photos first, to keep web requests safe.