Version Description
Download this release
Release Info
Developer | bfintal |
Plugin | OTF Regenerate Thumbnails |
Version | 0.1 |
Comparing to | |
See all releases |
Version 0.1
- otf_regen_thumbs.php +152 -0
- readme.txt +71 -0
otf_regen_thumbs.php
ADDED
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?php
|
2 |
+
/*
|
3 |
+
Plugin Name: OTF Regenerate Thumbnails
|
4 |
+
Plugin URI: http://github.com
|
5 |
+
Description: Automatically regenerates your thumbnails on the fly (OTF) after changing the thumbnail sizes or switching themes.
|
6 |
+
Author: Benjamin Intal - Gambit Technologies Inc
|
7 |
+
Version: 0.1
|
8 |
+
Author URI: http://gambit.ph
|
9 |
+
*/
|
10 |
+
|
11 |
+
/**
|
12 |
+
* Simple but effectively resizes images on the fly. Doesn't upsize, just downsizes like how WordPress likes it.
|
13 |
+
* If the image already exists, it's served. If not, the image is resized to the specified size, saved for
|
14 |
+
* future use, then served.
|
15 |
+
*
|
16 |
+
* @author Benjamin Intal - Gambit Technologies Inc
|
17 |
+
* @see https://wordpress.stackexchange.com/questions/53344/how-to-generate-thumbnails-when-needed-only/124790#124790
|
18 |
+
* @see http://codex.wordpress.org/Function_Reference/get_intermediate_image_sizes
|
19 |
+
*/
|
20 |
+
if ( ! function_exists( 'gambit_otf_regen_thumbs_media_downsize' ) ) {
|
21 |
+
|
22 |
+
add_filter( 'image_downsize', 'gambit_otf_regen_thumbs_media_downsize', 10, 3 );
|
23 |
+
|
24 |
+
/**
|
25 |
+
* The downsizer. This only does something if the existing image size doesn't exist yet.
|
26 |
+
*
|
27 |
+
* @param $out boolean false
|
28 |
+
* @param $id int Attachment ID
|
29 |
+
* @param $size mixed The size name, or an array containing the width & height
|
30 |
+
* @return mixed False if the custom downsize failed, or an array of the image if successful
|
31 |
+
*/
|
32 |
+
function gambit_otf_regen_thumbs_media_downsize( $out, $id, $size ) {
|
33 |
+
|
34 |
+
// Gather all the different image sizes of WP (thumbnail, medium, large) and,
|
35 |
+
// all the theme/plugin-introduced sizes.
|
36 |
+
global $_gambit_otf_regen_thumbs_all_image_sizes;
|
37 |
+
if ( ! isset( $_gambit_otf_regen_thumbs_all_image_sizes ) ) {
|
38 |
+
global $_wp_additional_image_sizes;
|
39 |
+
|
40 |
+
$_gambit_otf_regen_thumbs_all_image_sizes = array();
|
41 |
+
$interimSizes = get_intermediate_image_sizes();
|
42 |
+
|
43 |
+
foreach ( $interimSizes as $sizeName ) {
|
44 |
+
if ( in_array( $sizeName, array( 'thumbnail', 'medium', 'large' ) ) ) {
|
45 |
+
|
46 |
+
$_gambit_otf_regen_thumbs_all_image_sizes[ $sizeName ]['width'] = get_option( $sizeName . '_size_w' );
|
47 |
+
$_gambit_otf_regen_thumbs_all_image_sizes[ $sizeName ]['height'] = get_option( $sizeName . '_size_h' );
|
48 |
+
$_gambit_otf_regen_thumbs_all_image_sizes[ $sizeName ]['crop'] = (bool) get_option( $sizeName . '_crop' );
|
49 |
+
|
50 |
+
} elseif ( isset( $_wp_additional_image_sizes[ $sizeName ] ) ) {
|
51 |
+
|
52 |
+
$_gambit_otf_regen_thumbs_all_image_sizes[ $sizeName ] = $_wp_additional_image_sizes[ $sizeName ];
|
53 |
+
}
|
54 |
+
}
|
55 |
+
}
|
56 |
+
|
57 |
+
// This now contains all the data that we have for all the image sizes
|
58 |
+
$allSizes = $_gambit_otf_regen_thumbs_all_image_sizes;
|
59 |
+
|
60 |
+
// If image size exists let WP serve it like normally
|
61 |
+
$imagedata = wp_get_attachment_metadata( $id );
|
62 |
+
|
63 |
+
// Image attachment doesn't exist
|
64 |
+
if ( ! is_array( $imagedata ) ) {
|
65 |
+
return false;
|
66 |
+
}
|
67 |
+
|
68 |
+
// If the size given is a string / a name of a size
|
69 |
+
if ( is_string( $size ) ) {
|
70 |
+
|
71 |
+
// If the size has already been previously created, use it
|
72 |
+
if ( isset( $imagedata['sizes'][ $size ] ) && ! empty( $allSizes[ $size ] ) ) {
|
73 |
+
|
74 |
+
// But only if the size remained the same
|
75 |
+
if ( $allSizes[ $size ]['width'] == $imagedata['sizes'][ $size ]['width']
|
76 |
+
&& $allSizes[ $size ]['height'] == $imagedata['sizes'][ $size ]['height'] ) {
|
77 |
+
return false;
|
78 |
+
}
|
79 |
+
|
80 |
+
// Or if the size is different and we found out before that the size really was different
|
81 |
+
if ( ! empty( $imagedata['sizes'][ $size ][ 'width_query' ] )
|
82 |
+
&& ! empty( $imagedata['sizes'][ $size ]['height_query'] ) ) {
|
83 |
+
if ( $imagedata['sizes'][ $size ]['width_query'] == $allSizes[ $size ]['width']
|
84 |
+
&& $imagedata['sizes'][ $size ]['height_query'] == $allSizes[ $size ]['height'] ) {
|
85 |
+
return false;
|
86 |
+
}
|
87 |
+
}
|
88 |
+
|
89 |
+
}
|
90 |
+
|
91 |
+
// Resize the image
|
92 |
+
$resized = image_make_intermediate_size(
|
93 |
+
get_attached_file( $id ),
|
94 |
+
$allSizes[ $size ]['width'],
|
95 |
+
$allSizes[ $size ]['height'],
|
96 |
+
$allSizes[ $size ]['crop']
|
97 |
+
);
|
98 |
+
|
99 |
+
// Resize somehow failed
|
100 |
+
if ( ! $resized ) {
|
101 |
+
return false;
|
102 |
+
}
|
103 |
+
|
104 |
+
// Save the new size in WP
|
105 |
+
$imagedata['sizes'][ $size ] = $resized;
|
106 |
+
|
107 |
+
// Save some additional info so that we'll know next time whether we've resized this before
|
108 |
+
$imagedata['sizes'][ $size ]['width_query'] = $allSizes[ $size ]['width'];
|
109 |
+
$imagedata['sizes'][ $size ]['height_query'] = $allSizes[ $size ]['height'];
|
110 |
+
|
111 |
+
wp_update_attachment_metadata( $id, $imagedata );
|
112 |
+
|
113 |
+
// Serve the resized image
|
114 |
+
$att_url = wp_get_attachment_url( $id );
|
115 |
+
return array( dirname( $att_url ) . '/' . $resized['file'], $resized['width'], $resized['height'], true );
|
116 |
+
|
117 |
+
|
118 |
+
// If the size given is a custom array size
|
119 |
+
} else if ( is_array( $size ) ) {
|
120 |
+
$imagePath = get_attached_file( $id );
|
121 |
+
|
122 |
+
// This would be the path of our resized image if the dimensions existed
|
123 |
+
$imageExt = pathinfo( $imagePath, PATHINFO_EXTENSION );
|
124 |
+
$imagePath = preg_replace( '/^(.*)\.' . $imageExt . '$/', sprintf( '$1-%sx%s.%s', $size[0], $size[1], $imageExt ) , $imagePath );
|
125 |
+
|
126 |
+
$att_url = wp_get_attachment_url( $id );
|
127 |
+
|
128 |
+
// If it already exists, serve it
|
129 |
+
if ( file_exists( $imagePath ) ) {
|
130 |
+
return array( dirname( $att_url ) . '/' . basename( $imagePath ), $size[0], $size[1], true );
|
131 |
+
}
|
132 |
+
|
133 |
+
// If not, resize the image...
|
134 |
+
$resized = image_make_intermediate_size(
|
135 |
+
get_attached_file( $id ),
|
136 |
+
$size[0],
|
137 |
+
$size[1],
|
138 |
+
true
|
139 |
+
);
|
140 |
+
|
141 |
+
// Resize somehow failed
|
142 |
+
if ( ! $resized ) {
|
143 |
+
return false;
|
144 |
+
}
|
145 |
+
|
146 |
+
// Then serve it
|
147 |
+
return array( dirname( $att_url ) . '/' . $resized['file'], $resized['width'], $resized['height'], true );
|
148 |
+
}
|
149 |
+
|
150 |
+
return false;
|
151 |
+
}
|
152 |
+
}
|
readme.txt
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
=== Plugin Name ===
|
2 |
+
Contributors: bfintal
|
3 |
+
Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=D2MK28E7BDLHC
|
4 |
+
Tags: thumbnail, thumbnails, resize, regenerate, automatic, featured image, feature image, on the fly, otf
|
5 |
+
Requires at least: 3.8
|
6 |
+
Tested up to: 4.1
|
7 |
+
Stable tag: 0.1
|
8 |
+
License: GPLv2 or later
|
9 |
+
License URI: http://www.gnu.org/licenses/gpl-2.0.html
|
10 |
+
|
11 |
+
Automatically regenerates your thumbnails on the fly when thumbnail sizes change.
|
12 |
+
|
13 |
+
== Description ==
|
14 |
+
|
15 |
+
This plugin behaves similarly to [Regenerate Thumbnails](https://wordpress.org/plugins/regenerate-thumbnails/) except that images are resized automatically / on the fly, when they are used. Once created, they won't be processed again.
|
16 |
+
|
17 |
+
Your thumbnails will now resize when:
|
18 |
+
* Image Sizes in **Settings > Media** is modified,
|
19 |
+
* Switching themes & plugins with different thumbnail / featured image sizes
|
20 |
+
|
21 |
+
No settings pages, just install and activate and things should work right away.
|
22 |
+
|
23 |
+
Report bugs and help out in the code from the [Github repository](https://github.com/gambitph/WP-OTF-Regenerate-Thumbnails)
|
24 |
+
|
25 |
+
= What are you talking about? =
|
26 |
+
|
27 |
+
Test it out. In your normal WordPress website set up, create a gallery using the **Add Media** button while editing a post or page and use thumbnails. Afterwards, check out your gallery.
|
28 |
+
|
29 |
+
Notice the size of your thumbnails, most likely they're **150 x 150**. Do you see it? Great.
|
30 |
+
|
31 |
+
Now head over to **Settings > Media** and change your thumbnail size to something cooler, something rectangular, let's try **400 x 200**. Save it.
|
32 |
+
|
33 |
+
Go back to your gallery that you previously created and refresh your browser. Most likely you *won't* be seeing **400 x 200** thumbnails there.
|
34 |
+
|
35 |
+
OTF Regenerate Thumbnails fixes this for you.
|
36 |
+
|
37 |
+
= Features =
|
38 |
+
|
39 |
+
* Creates Resizes thumbnails on the fly
|
40 |
+
* Handles Image Size settings changes in **Settings > Media**
|
41 |
+
* Handles thumbnail / image size changes introduced by switching themes & plugins
|
42 |
+
* Works automatically, no setup needed
|
43 |
+
* All calls to `the_post_thumbnail` and other thumbnail functions are handled automatically
|
44 |
+
* Handles image sizes created from `add_image_size`,
|
45 |
+
* Handles [2-item array sizes](http://codex.wordpress.org/Function_Reference/the_post_thumbnail)
|
46 |
+
|
47 |
+
== Installation ==
|
48 |
+
|
49 |
+
1. Head over to Plugins > Add New in the admin
|
50 |
+
2. Search for "OFT Regenerate Thumbnails"
|
51 |
+
3. Install & activate the plugin
|
52 |
+
|
53 |
+
== Screenshots ==
|
54 |
+
|
55 |
+
1. This is my Settings > Media screen, the thumbnails specified as 80 x 80
|
56 |
+
2. This is what my gallery looks like. Yup, it's 80 x 80
|
57 |
+
3. I just changed my Settings > Media thumbnails to 400 x 200
|
58 |
+
4. But, my gallery thumbnails still look like it's all 80 x 80 :(
|
59 |
+
5. After turning on the OTF Regenerate Thumbnails, my gallery now shows 400 x 200 thumbnails :)
|
60 |
+
|
61 |
+
== Frequently Asked Questions ==
|
62 |
+
|
63 |
+
* [OTF Regenerate Thumbnails GitHub Repository](https://github.com/gambitph/WP-OTF-Regenerate-Thumbnails)
|
64 |
+
* [Issue Tracker](https://github.com/gambitph/WP-OTF-Regenerate-Thumbnails/issues)
|
65 |
+
|
66 |
+
== Upgrade Notice ==
|
67 |
+
|
68 |
+
== Changelog ==
|
69 |
+
|
70 |
+
= 1.0 =
|
71 |
+
First release
|