# Create optimized copy

The individual Manage Image Optimization form has a separate copy action using
its existing maximum width, maximum height and quality fields. The media modal
embeds that same form. Bulk actions continue to optimize in place.

`create_optimized_copy()` wraps `make_optimized_image()`: convert first, then
insert an independent WebP attachment and generate its registered sizes. It
copies alt, caption and description, adds the actual output width to the title,
and records `_ksf_image_copy_source_id` and `_ksf_image_copy_source` on the copy.
The latter contains the actual input filename, dimensions, bytes and fallback
reason, allowing savings to be measured against the selected source.

For active in-place replacements, source selection reads the journal's retained
original file and metadata, including its WordPress scaled-file relationship.
It never substitutes the current optimized WebP when that original is missing.
The source attachment, its optimization diagnostics and its replacement record
are not written by copying. No database link replacement or schema change is
needed. Copies are excluded from automatic upload optimization.

The copy uses the existing format/edit restrictions, helper limits, connection
errors and minimum 5% savings rule. A helper failure occurs before insertion;
later failures remove the new attachment and its recorded sizes. This is normal
request error cleanup; terminating PHP or the host forcibly is not a filesystem
transaction.

The result shows the new attachment, dimensions, bytes, savings and edit link.
“Use this copy” fetches and selects its new model in the existing media frame;
the editor's normal insertion callback then inserts it. The standalone result
links to that attachment in the Media Library. Provenance links are shown on
the copy's optimization page and in both attachment editors. The bottom of the
optimization page lists the source's existing editable copies, newest first, with
current dimensions, file sizes and the same edit/selection links. Trashed copies
are excluded.

## Validation

`ImageOptimizerCopyTest` covers independent copies at two widths, source and
existing-content preservation, retained originals and scaled fallback, generated
WebP sizes, copied fields including slashes, failed helper/insert/metadata writes,
missing originals, validation, automatic-upload exclusion and the admin result.
The modal JavaScript suite uses the shipped bundle and real WordPress controllers
to check new-model selection/insertion in Gutenberg, Classic Editor and the grid,
fetch retries, and preservation of the clicked action when disabling buttons.

Run PHP tests with the project's disposable database configuration for each engine;
run `npm run build` and `npm test` for the shipped assets and JavaScript checks.

For local browser acceptance with a configured, running Kind Image Tool:

```bash
node tests/Live/copy-browser.cjs
```

This wp-dev DDEV harness creates a temporary administrator, source image and three
draft pages, creates two copies, saves one through Gutenberg's media picker,
checks Classic Editor and grid selection, and confirms source and existing-page
snapshots. It also observes a real helper request for a copy of an optimized
source, verifying that it contains the retained original bytes. Fixtures and
session are deleted in `finally`; helper configuration is never changed.

If the helper is a disposable container named `ksf-copy-*` on `ddev_default`,
`KSF_COPY_HELPER_CONTAINER=<name>` additionally tests helper unavailability by
disconnecting and reconnecting only that container. The harness expects its
network alias to be `kind-image-tool-nginx-1`, matching this local site's URL.
This checks that a failed browser copy leaves no new attachment or files.

## Cropping follow-up

Feasible without changing the helper: use `wp_get_image_editor()` / `crop()`
(or `wp_crop_image()`) to save a separate temporary crop, then optimize it and
register the output as the new attachment. A PNG intermediate avoids another
lossy JPEG compression step. This needs explicit crop coordinates, local image
editor memory limits and temporary-file cleanup. The normal WordPress attachment
edit UI creates backup metadata, which the current optimizer deliberately rejects;
that path cannot simply be chained into Optimize.

Crop support in Kind Image Tool would avoid local decoding and intermediate files,
but requires a helper API change. Keep either implementation separate from this
proportional-resize action.
