=== Square Thumbnails – Square Product Images Without Cropping ===
Contributors: nicunarcisbodea
Donate link: https://www.paypal.com/paypalme/narcisbodea
Tags: square thumbnails, square images, product images, thumbnails, woocommerce
Requires at least: 3.5
Tested up to: 7.1
Stable tag: 3.0
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

Turn product photos of any shape into matching square thumbnails, whole and centered on a background you choose, for an even WooCommerce grid.

== Description ==

Product photos rarely come in one shape. Put a tall bottle next to a wide shoe and your shop grid turns uneven. Crop them to match and the bottle loses its cap and the shoe loses its toe.

**Square Thumbnails** makes square product images without cropping. It scales the whole photo to fit inside a square and fills the space around it with a background you choose, so every thumbnail has the same shape and nothing is cut off.

= How it makes square images =

When you upload an image, WordPress creates its usual thumbnails. Square Thumbnails then saves a square version of each size, with the whole image placed on the background. The file you uploaded is never overwritten.

For images already in your library, the plugin has its own regeneration tools, so you don't need a second plugin.

It works with JPEG, PNG and GIF, plus WebP, AVIF and BMP when your server's PHP GD library supports them. The settings page lists what your server can process.

= New in 3.0 =

Everything new is off until you turn it on. A site that updates and changes no setting keeps exactly the thumbnails it had.

* **Shape:** keep fitting the whole image, or crop to the center, around a focal point you pick, or by smart crop, which keeps the most detailed part of the image.
* **Trim and same size:** remove plain margins around the product, and make every product fill the same share of the square.
* **Background:** take the color from the edges of the image, or remove a plain background around the product.
* **Modern formats:** save the square thumbnails as WebP or AVIF where your server can, and see how much space that saved.
* **Background Batch:** regenerate your library on the server in short steps, with pause, cancel, an optional night window and undo of the last run.
* **Live Preview:** see a sample image, or one of yours, with the current settings before you regenerate anything.
* **Settings for one image:** in the Media Library, give an image its own shape, focal point or background, exclude it, or restore WordPress's own thumbnails for it.
* **Post Types:** make square only the images of the post types you choose, such as products.
* **Whole Images in Block Grids:** show whole product images in WooCommerce's block-based shop grids.
* **Server Check:** the formats and memory your server has, the largest image it can square and whether scheduled tasks run.
* **WP-CLI:** `wp square-thumbnails regenerate`, `status`, `undo` and `check`.

Not included: lazy loading, `<picture>` markup and any outside service. All the work happens on your own server.

= What you can set =

* **Shape:** fit the whole image (the default), crop to the center, crop around a focal point or smart crop.
* **Thumbnail background:** a custom color (white by default), the color of the image's top-left pixel, the most common color along its edges, or transparent.
* **Alignment:** left, center or right, and top, middle or bottom (center and middle by default).
* **Image sizes:** choose which sizes become square, with one-click selections for the WooCommerce or WordPress default sizes.
* **Small images:** set a minimum size, then leave smaller images as they are or process them anyway.
* **JPEG quality:** from 60 to 100 (90 by default). WebP and AVIF have their own quality.
* **Regeneration:** your whole library with a progress bar or in the background, one image from its details screen, or a selection from the Media Library.

= Which sites it suits =

* WooCommerce stores whose product images arrive in mixed shapes from suppliers, imports or data feeds
* Catalogs, portfolios and galleries built on a square grid
* Any WordPress site that needs square images. WooCommerce is optional.

== Installation ==

1. In **Plugins > Add New**, search for "Square Thumbnails", then install and activate it.
2. Open **Media > Square Thumbnails**, set the background and image sizes, and turn on **Force Square**. Settings save as you change them.
3. Use **Live Preview** on the same page to see a setting before you use it.
4. Upload images as usual. For images already in your library, use **Regenerate Thumbnails** or **Background Batch** on the same page.

== Frequently Asked Questions ==

= Does it crop my thumbnails? =

Not by default. The whole image is scaled to fit the square, and the space left over gets your background color. Under **Shape** you can choose to crop instead: to the center, around a focal point, or by smart crop.

WordPress crops one size itself: the 150×150 `thumbnail`. Turn on **Force Square** and regenerate: it also rebuilds sizes WordPress already crops square, like the 150 × 150 thumbnail, so they show the whole image. Without it, browsers can pick the cropped version for small product tiles.

= Why is my thumbnail background black? =

The default background is white. A black background usually means one of these:

* **Color from Image** is selected and the top-left pixel of the image is dark or transparent.
* **Transparent** was selected for a GIF or BMP image before 3.0. Since 3.0 a GIF thumbnail keeps one transparent color and a BMP thumbnail gets your custom color, white by default. PNG, WebP and AVIF keep full transparency, and JPEG thumbnails are saved as PNG to keep it.
* The thumbnail was created before you changed the setting.

To fix it, open **Media > Square Thumbnails**, choose **Custom Color**, pick a color, then regenerate with **Force regeneration** ticked.

= Can I make only WooCommerce images square and leave blog images alone? =

Yes. By default every image size is processed, including the sizes themes use for blog posts. To limit it:

1. In **Media > Square Thumbnails**, under **Selective Size Processing**, click **Select WooCommerce Only**, then tick `thumbnail` as well. Browsers can use that size for small product tiles.
2. Turn on **Force Square**.
3. To fix images you already uploaded, make the same selection under **Regenerate Thumbnails** (it has its own list), tick **Force regeneration** and start.

The other sizes go back to normal WordPress thumbnails. The choice applies per image size. To choose by post type instead, for example products only, use **Post Types**. A single image can also be excluded from its own settings in the Media Library.

= WooCommerce thumbnails still look cropped or show the wrong image. What should I check? =

1. **Turn on Force Square, then regenerate** with **Force regeneration** ticked. Images uploaded before the plugin was active keep their old thumbnails until you do, and without this option cropped thumbnails that are already square are skipped.
2. **Clear your caches.** Square thumbnails are new files with `-sqt` in the name, so a page cache or the browser can keep showing the old ones.
3. **Check which size your theme or block displays.** The plugin makes registered image sizes square. A layout that shows the full-size image displays your original photo.

On the single product page, WooCommerce normally shows the photo in its own shape: whole, but not square.

= Does it work with block themes and the Product Collection block? =

Yes. Classic themes and the `[products]` shortcode use the WooCommerce thumbnail sizes, so they show the square versions.

WooCommerce's block-based shop page uses the `large` size instead and crops it with CSS. To show whole images there, turn on **Whole Images in Block Grids** under **WooCommerce Block Grids**. It changes only the style of those grids; your files stay as they are.

= Will image quality drop? =

Each square thumbnail is built from the full-size image, not from a smaller copy. You set the JPEG quality, from 60 to 100 (90 by default), and the WebP and AVIF quality under **Modern Formats**. If thumbnails look soft, raise the quality and regenerate, or upload a larger image.

= What happens to small images? =

An image with a width or height under 200 pixels is left as it is by default. Under **Small Image Handling** you can change that minimum or choose **Process anyway**.

Leave **Create Missing Sizes** off unless you need it. For sizes larger than the image, it saves one enlarged square copy and uses it for all of them. It looks soft and uses more memory. With **Force Square** off, only WooCommerce sizes get this copy.

= What if an image is too large for my server? =

When an image is too large to square in the memory your server gives PHP, it keeps the thumbnails WordPress made, and its details in the Media Library say why, instead of the upload stopping with an error. **Server Check** on the settings page shows the largest image your server can square.

= How do I regenerate thumbnails for images I already uploaded? =

New uploads are handled automatically. For existing images:

* **All images, in the background:** open **Media > Square Thumbnails**, go to **Background Batch**, choose the sizes under **Regenerate Thumbnails** and click **Start Batch**. It works through your library in short steps, can wait for a night window, and can be paused, cancelled or undone.
* **All images, while you watch:** go to **Regenerate Thumbnails**, choose the sizes, tick **Force regeneration** and click **Start Regeneration**.
* **One image:** open it in the Media Library and click **Regenerate Square Thumbnails**.
* **A selection:** in the Media Library list view, tick the images and choose **Regenerate Square Thumbnails** from Bulk actions.

After changing the background or alignment, use one of the first two. The bulk action skips non-WooCommerce thumbnails that are already square unless **Force Square** is on.

= Does the Background Batch need WordPress's scheduled tasks? =

It uses them when they run. On a site where they are turned off and no server cron runs them, the batch moves on only while its settings page is open. **Server Check** tells you which case your site is in.

= Can I undo a regeneration? =

A **Background Batch** or WP-CLI run can be undone with **Undo Last Run**, which gives each image back the thumbnails and record it had before the run. The copies it keeps for that take space in your uploads folder, and the page shows how much. They are deleted when the next run starts, 30 days after the run, or when you click **Delete undo copies now**. The regeneration with the progress bar can't be undone.

= Why do some square thumbnails have a code in their name? =

Thumbnails made with a 3.0 setting, such as a crop or a modern format, carry a short code of their settings in the file name, for example `photo-300x300-sqt-1a2b3c.webp`. When those settings change, the new thumbnails get new names, so no cache keeps showing the old ones. Thumbnails made with the earlier settings keep the names they always had.

= What happens to thumbnails my posts link to? =

When a setting changes how square thumbnails look or their format, 3.0 writes new files and removes the ones they replace. A replaced thumbnail whose address is written in the content of a post, page or product stays on disk, so it keeps showing there as it was. Images placed by your theme, by WooCommerce or by anything else that follows the Media Library switch to the new files by themselves. Addresses that a page builder keeps outside the post content are not checked, so on such a site, after a **Background Batch**, look at those pages before you delete the undo copies.

= Can I go back to version 2.5? =

Yes. Images keep the thumbnails 3.0 made, because WordPress still points to them. A regeneration with 2.5 makes them again with 2.5's settings, so crops, trims and modern formats become fitted squares again.

= Are my original images changed? =

No. The file you upload is never overwritten. Square thumbnails are saved as separate files with `-sqt` in the name, next to the thumbnails WordPress made. **Apply to Original** doesn't overwrite your file either: it saves a square copy of the full-size image beside it.

= What happens if I deactivate or delete the plugin? =

Nothing is undone. Existing images keep their square thumbnails, because WordPress still points to those files, and new uploads get normal WordPress thumbnails.

To return existing images to normal, regenerate them while the plugin is inactive, with a regeneration tool or `wp media regenerate` in WP-CLI, or use **Restore WordPress Thumbnails** on each image while it is active. Your settings and the settings of single images stay in the database, even after you delete the plugin. Deleting it removes only its run data and undo copies.

== Screenshots ==

1. With the plugin: the whole image fits inside the square thumbnail in the Media Library.
2. Without the plugin: WordPress crops the same image to a square and cuts the text.
3. Shape: fit the whole image or crop it, trim margins and give every product the same size.
4. Live Preview: a sample image with the current settings, next to what WordPress makes.
5. Regeneration: the whole library with a progress bar, or a Background Batch run on the server with pause and cancel.
6. Settings: the formats your server supports and the apply options.
7. Settings: background, small image handling and JPEG quality.
8. Settings: which image sizes become square.
9. In the attachment details: the image's own settings and Regenerate Square Thumbnails.

== Changelog ==

= 3.0 =
* New, all off until you turn them on: crop to the center, around a focal point or by smart crop; trim margins and same-size products; color from the image edges and removal of plain backgrounds; WebP and AVIF square thumbnails with measured savings.
* New: Background Batch with pause, cancel, a night window and undo of the last run, and WP-CLI commands.
* New: Live Preview, settings for single images in the Media Library, Post Types, whole images in WooCommerce block grids and a Server Check.
* Fixed: GIF and BMP thumbnails with Transparent selected came out black. A GIF now keeps a transparent color, and a BMP gets the custom color, white by default.
* Fixed: an image too large for the server's memory no longer stops the upload with an error; it keeps WordPress's thumbnails and says why.
* Fixed: regenerating the library from the settings page no longer drops the link to the original upload and the camera data (title, caption, credit, copyright, keywords) of images larger than 2,560 pixels or turned by WordPress at upload.
* Clearer help for Force Square, Apply to Original and Create Missing Sizes.

= 2.5 =
* Security and reliability improvements.
* Compatibility with WordPress 7.1, current PHP and WooCommerce.
* Removed unused notices and sign-up forms.
* Added a dismissible recommendation on the settings page for WooCommerce stores.
* Added a one-time notice on the Dashboard after updating.

= 2.4.0 =
* Maintenance release.

= 2.3.8 =
* Fixed WooCommerce zoom gallery CSS issue when 'tooriginal' option is enabled
* Added additional validation for image dimensions to prevent PHP errors
* Added proper fallback dimensions for invalid source images
* Enhanced error handling for image processing

= 2.3.7 =
* Fixed PHP 8.2 compatibility issues
* Improved WooCommerce integration
* Fixed bug with transparent background handling
* Added: CSS fix for WooCommerce zoom gallery when tooriginal option is enabled (`.pswp__zoom-wrap img { height: auto !important; }`)
* Improved: Conditional loading of CSS only when needed

= 2.3.6 =
* Fixed: Critical PHP fatal error caused by imagecreatetruecolor() being called with invalid dimensions
* Improved: Added robust validation for image dimensions throughout the processing pipeline
* Fixed: Proper handling of zero or negative width/height values in thumbnail processing
* Enhanced: Better error logging for invalid image dimensions
* Added: Default fallback dimensions for invalid source images
* Improved: Protection against division by zero errors in image ratio calculations

= 2.3.5 =
* Fix to delete all thumbnails when an image is deleted from media. The same for the PNG set created if the background is set to transparent.

= 2.3.4 =
* There were some problems in regenerating and are all fixed.

= 2.3.3 =
* Fixing functionality.

= 2.3.0 =
* New Feature: Media Library integration for regenerating thumbnails
* Added: Regenerate button in attachment details screen
* Added: Bulk action "Regenerate Square Thumbnails" in Media Library
* Enhanced: Better user feedback with admin notices
* Improved: AJAX handling for single image regeneration
* New Feature: Bulk thumbnail regeneration with selective size processing
* Added: Progress tracking for regeneration with visual progress bar
* Added: Option to force regenerate existing thumbnails or only create missing ones
* Added: Batch processing for better performance (5 images at a time)
* Added: Separate quick selectors for regeneration section
* Enhanced: AJAX handling for smooth operation without page refresh
* Improved: Memory management during bulk operations
* Fixed: Image processing for individual thumbnail sizes
* Fixed: Selective size processing now works correctly
* New Feature: JPEG quality control setting (60-100%, default 90%)
* New Feature: Selective size processing - choose which image sizes to make square
* Added: Quick selector buttons for WooCommerce and WordPress default sizes
* Added: Visual highlighting for WooCommerce-specific image sizes
* Enhanced: Settings interface with better organization and controls
* Improved: Performance by only processing selected image sizes

= 2.2.0 =
* New Feature: Comprehensive small image handling system (configurable minimum size threshold of 50-1000 pixels, three action options for images below it, prevents WooCommerce product display failures)
* Added: "Small Image Handling" settings section (Minimum Image Size, default 200px, and Small Image Action)
* Fixed: Critical bug where products with small thumbnails wouldn't display in WooCommerce
* Improved: Image processing logic for edge cases
* Enhanced: Admin interface with clearer options
* Added: Detailed logging for small image processing
* Compatibility: Better integration with WooCommerce product galleries

= 2.1.1 =
* Critical Fix: Users without manage_options capability (e.g., Authors, Contributors, Editors) can now properly access their profile page (/wp-admin/profile.php) and other admin areas
* Fix: Moved permission check from display_admin_page() to showPage() method to prevent global admin access blocking
* Security: Settings page remains properly protected - only administrators can access Square Thumbnails options
* Addresses reported issue: "Plugin is blocking access to /wp-admin/profile.php and /wp-admin/ in general to users without manage_options capability"

= 2.1.0 =
* New: Added transparent background option for square thumbnails
* New: Support for modern image formats including WebP and AVIF
* New: Added BMP format support with improved handling
* New: Format compatibility checker in settings page showing supported formats
* New: Elegant new settings page design with improved UI/UX
* New: Background type selector (Custom Color, Color from Image, Transparent)
* New: AJAX settings save with visual feedback
* New: Auto-hide/show options based on background type selection
* Improved: Enhanced error handling for unsupported formats
* Improved: Better memory management for large images
* Improved: Transparency support for PNG, WebP, and AVIF formats
* Improved: Added proper nonce security for settings save
* Improved: Sanitization of all input values
* Fixed: Settings not saving properly in some configurations
* Fixed: Color picker display issues

= 2.0.2 =
* Removed donation link in admin.

= 1.1.2 =
* Fixed all errors displayed in plugin check

= 1.1.1 =
* Fixed vulnerability reported by Wordfence: https://www.wordfence.com/threat-intel/vulnerabilities/id/31cc30c7-262d-4582-8976-fc8095bdca5f?source=api-prod

= 1.0.4 =
* Major update for the entire code and fixes for all bugs.
* New option to choose if the original image should also be put in a square
* New option to create all existing sizes, even if the original image is smaller than some of them. By default, WordPress doesn't create thumbnails larger than the original image.

= 1.0.3 =
* Fixed the bug reported by @chuchilade and by @peer_012. The bug was caused by hex to rgb color conversion.

= 1.0.2 =
* Fix for eregi deprecation. Thanks to mjassen (https://wordpress.org/support/users/mjjojo/) for reporting this error and for helping to fix.

= 1.0.1 =
* Horizontal align of the image in the frame
* Vertical align of the image in the frame
* Set background color of the frame or check to automatically read the color from the image, point (0,0)

= 1.0 =
* Initial release.

== Upgrade Notice ==

= 3.0 =
New: crops, trimming, WebP and AVIF thumbnails and a background batch with undo. Everything new is off until you turn it on, and your existing thumbnails and settings stay as they are.
