soo_thumb_atts
Summary:
Update thumbnail height and width values after the Txp 4.2.0 upgrade
(If it won’t install try the compressed version)
(Code preview below)
Full plugin help text
Overview
Thumbnail height and width attributes were introduced in Textpattern version 4.2.0. However, these attributes are not automatically added to thumbnails already in the database when the upgrade takes place.
This plugin finds images with a thumbnail but no thumbnail height or width in the database, and allows you to update these with the correct values.
It is intended as a one-use plugin, but there might be future use for it as a means of correcting data for images that have been batch-replaced without going through the Textpattern admin panel. (It could easily be modified to examine all images, not just thumbnails.)
The plugin requires Textpattern version 4.2.0 or later.
Usage
Install and activate the plugin, then find its control panel under the Extensions tab. Click “Update” and you’re done; you will probably want to uninstall the plugin.
Version History
0.2.1 (9/3/2009)
Code cleaning — thanks again to Ruud for the suggestions.
0.2 (9/3/2009)
Thanks to Ruud for the suggestions. This version only operates on thumbnails with h and w set to 0 in the database, and does not attempt to list more than 50 images (although it will still attempt to update all the images at once).
0.1 (9/3/2009)
Initial release:
- Update thumbnail height and width values in the database
Code preview
1 : if ( @txpinterface == 'admin' ) {
2 : add_privs('soo_thumb_atts','1,2');
3 :
4 : register_tab('extensions', 'soo_thumb_atts',
5 : soo_thumb_atts_gTxt('soo_thumb_atts'));
6 : register_callback('soo_thumb_atts', 'soo_thumb_atts');
7 : }
8 :
9 : function soo_thumb_atts( $event, $step, $message = '' ) {
10 : if( $step and in_array($step, array(
11 : 'update',
12 : )) ) {
13 : $func = 'soo_thumb_atts_' . $step;
14 : $message = $func();
15 : }
16 : soo_thumb_atts_ui($message);
17 : }
18 :
19 : function soo_thumb_atts_ui( $message ) {
20 :
21 : global $prefs;
22 : $version_ok = $prefs['version'] >= 4.2;
23 : $version_color = $version_ok ? '#093' : '#f30';
24 : $version_text = "<strong style=\"color:$version_color;\">" .
25 : $prefs['version'] . '</strong>';
26 : $cols = 3;
27 : $limit = 50;
28 : $tda = ' style="text-align:center;"';
29 :
30 : pagetop(soo_thumb_atts_gTxt('soo_thumb_atts'), $message);
31 :
32 : echo n .
33 : startTable('list') .
34 : tr( n .
35 : tdcs(hed(soo_thumb_atts_gTxt('header'), 1), $cols)
36 : ) .
37 : tr( n .
38 : tdcs(soo_thumb_atts_gTxt('version_notice') . br .
39 : soo_thumb_atts_gTxt('version_check') . $version_text . '.'
40 : , $cols)
41 : )
42 : ;
43 :
44 : if ( ! $version_ok ) {
45 : echo endTable();
46 : return;
47 : }
48 :
49 : $broken_thumbs = soo_thumb_atts_query('select', $limit);
50 : $count = count($broken_thumbs);
51 :
52 : if ( $count == 0 ) {
53 : echo
54 : tr(
55 : tdcs(strong(soo_thumb_atts_gTxt('up_to_date_begin')) .
56 : soo_thumb_atts_gTxt('up_to_date_end'), $cols)
57 : ) .
58 : endTable()
59 : ;
60 : return;
61 : }
62 :
63 : echo
64 : tr(
65 : tdcs( ( $count == $limit ? soo_thumb_atts_gTxt('limit_begin') : '' )
66 : . $count . soo_thumb_atts_gTxt('count_end') .
67 : ( $count == $limit ? soo_thumb_atts_gTxt('limit_end')
68 : . $count . '.' : '' )
69 : , $cols
70 : )
71 : ) .
72 : tr(
73 : tdcs('<form method="post" name="soo_thumb_atts_form">' . n .
74 : sInput('update') . n .
75 : eInput('soo_thumb_atts') . n .
76 : fInput('submit', 'soo_thumb_atts_update',
77 : soo_thumb_atts_gTxt('update'), 'publish')
78 : , $cols)
79 : ) .
80 : tr(
81 : hCell('File', '', $tda) . hCell('Actual w', '', $tda) . hCell('Actual h', '', $tda)
82 : );
83 :
84 : foreach ( $broken_thumbs as $t ) {
85 : extract($t);
86 : echo tr( n .
87 : tda("${id}t$ext", $tda) .
88 : tda($w, $tda) .
89 : tda($h, $tda)
90 : );
91 : }
92 : echo endTable();
93 : }
94 :
95 : function soo_thumb_atts_update( ) {
96 : return soo_thumb_atts_query('update');
97 : }
98 :
99 : function soo_thumb_atts_query( $action = 'select', $limit = null ) {
100 : global $img_dir, $prefs;
101 : $path = $prefs['path_to_site'] . "/$img_dir";
102 : $where = 'thumbnail = 1 and thumb_h = 0 and thumb_w = 0' .
103 : ( $limit ? " limit $limit" : '' );
104 : $rs = safe_rows_start('id, ext', 'txp_image', $where);
105 : $count = 0;
106 :
107 : while ( $a = nextRow($rs) ) {
108 : extract($a);
109 : $file = "$path/${id}t$ext";
110 : if ( file_exists($file) ) {
111 : list($w, $h) = getimagesize($file);
112 : switch ( $action ) {
113 : case 'select':
114 : $out[$id] = $a;
115 : $out[$id]['w'] = $w;
116 : $out[$id]['h'] = $h;
117 : break;
118 : case 'update':
119 : $ok = safe_update(
120 : 'txp_image',
121 : "thumb_w = $w, thumb_h = $h",
122 : "id = $id"
123 : );
124 : if ( $ok ) $count++;
125 : }
126 : }
127 : }
128 : if ( $action == 'select' and isset($out) )
129 : return $out;
130 : if ( $action == 'update' )
131 : return $count . soo_thumb_atts_gTxt('images_updated');
132 : }
133 :
134 : function soo_thumb_atts_gTxt ( $what, $atts = array() ) {
135 : $lang = array(
136 : 'soo_thumb_atts' => 'Thumbnail Atts',
137 : 'header' => 'Update Thumbnail H/W Attributes',
138 : 'version_check' => 'You appear to be running Txp version ',
139 : 'version_notice' => 'This plugin requires Txp version 4.2.0 or greater.',
140 : 'limit_begin' => 'There are at least ',
141 : 'count_end' => ' thumbnails to update.',
142 : 'limit_end' => ' Showing the first ',
143 : 'up_to_date_begin' => 'Nothing to update: ',
144 : 'up_to_date_end' => 'all thumbnails have correct h/w attributes.',
145 : 'update' => 'Update',
146 : 'images_updated' => ' thumbnails updated successfully.',
147 : );
148 : return strtr($lang[$what], $atts);
149 :
150 : }
Posted 2009-09-03 (last modified 2017-03-14)