-
Notifications
You must be signed in to change notification settings - Fork 0
/
photology.plugin.php
227 lines (198 loc) · 6.31 KB
/
photology.plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
class Photology extends Plugin
{
public function filter_plugin_config( $actions, $plugin_id )
{
if ( $plugin_id == $this->plugin_id() ) {
$actions[]= _t('Configure');
}
return $actions;
}
public function action_plugin_ui( $plugin_id, $action )
{
if ( $plugin_id == $this->plugin_id() ) {
switch ( $action ) {
case _t('Configure') :
$ui = new FormUI( strtolower( get_class( $this ) ) );
$max_dimension = $ui->append( 'text', 'max_dimension', 'photology__maxdim', _t( 'Maximum size of thumbnail (length and width)' ) );
$max_dimension->add_validator( array ( $this, 'validate_numeric' ) );
$ui->append( 'submit', 'save', _t('Save') );
$ui->on_success( array( $this, 'update_config' ) );
$ui->out();
break;
}
}
}
public function validate_numeric( $value )
{
if( !is_numeric( $value ) ) {
return array( _t( 'This field must be a number.', 'photology' ) );
}
return array();
}
/**
* Give the user a session message to confirm options were saved.
**/
public function update_config( $ui )
{
Session::notice( _t( 'Maximum Thumbnail Dimension set.', 'photology' ) );
$ui->save();
}
/**
* function action_post_insert_after
* Invokes our thumbnail generating function when a new post is saved
* @param Post the post being added
**/
public function action_post_insert_after( $post )
{
$this->save_thumbnail( $post );
}
/**
* function action_post_update_after
* invokes our thumbnail generating function when a post is updated
* @param Post the post being updated
**/
public function action_post_update_after( $post )
{
$this->save_thumbnail( $post );
}
/**
* function save_thumbnail
* Determines whether a thumbnail needs to be created for this post, and adds it to the postinfo for this post
* @param Post the post for which the thumb should be generated
**/
public function save_thumbnail( $post )
{
// set up a temporary variable to capture the image tag(s)
$matches= array();
if ( preg_match( '/<img [^>]+>/', $post->content, $matches) ) {
// we got one! Now tease out the src element
$html= new HTMLTokenizer( $matches[0] );
$tokens= $html->parse();
foreach ($tokens as $node ) {
if ( 'img' == $node['name'] ) {
$elements= $node['attrs'];
}
}
}
if ( ! isset( $elements['src'] ) ) {
// no src= found, so don't try to do anything else
return;
}
$thumb= $post->info->photology_thumb;
if ( ! isset( $thumb ) ) {
// no thumbnail exists for this post yet, so make one
$post->info->photology_thumb= $this->make_thumbnail( $elements['src'] );
$post->info->photology_md5= md5_file( $this->get_image_file( $elements['src'] ) );
$post->info->commit();
} else {
// a thumbnail exists; we should check whether we need to update it
if (true) { // ( md5_file( $this->get_image_file( $elements['src'] ) ) != $post->info->photology_md5 ) {
// the image has a different MD5 sum than the
// one we previously calculated for it, so
// generate a new thumbnail
$post->info->photology_thumb= $this->make_thumbnail( $elements['src'] );
$post->info->photology_md5= md5_file( $this->get_image_file( $elements['src'] ) );
$post->info->commit();
}
}
}
/**
* function get_image_file
* Given a URL to an image, obtain the filesystem path to the image
* @param String the image URL
* @return String the filesystem path to the image
**/
public function get_image_file( $image )
{
return substr_replace( $image, Site::get_dir( 'user' ), 0, strlen( Site::get_url( 'user' ) ) );
}
/**
* function get_image_url
* Given the filesystem path to the image to an image, obtain a URL
* @param String the filesystem path to the image
* @return String the image URL
**/
public function get_image_url( $image )
{
return substr_replace( $image, Site::get_url( 'user' ), 0, strlen( Site::get_dir( 'user' ) ) );
}
/**
* function make_thumbnail
* Create a thumbnail from an image URL
* @param String The image defined in the <img> tag
* @return String the URL of the generated thumbnail
**/
public function make_thumbnail( $image )
{
// Get maximum size from stored options, or use a default of 100.
$max_dimension= Options::get( 'photology__maxdim', 100 );
// Get the image from the filesystem
$img= $this->get_image_file( $image );
// Does derivative directory not exist?
$thumbdir = dirname( $img ) . '/' . HabariSilo::DERIV_DIR . '';
if( ! is_dir( $thumbdir ) ) {
// Create the derivative directory
if( ! mkdir( $thumbdir, 0755 ) ){
// Couldn't make derivative directory
return false;
}
}
// Get information about the image
list( $src_width, $src_height, $type, $attr )= getimagesize( $img );
// Load the image based on filetype
switch( $type ) {
case IMAGETYPE_JPEG:
$src_img = imagecreatefromjpeg( $img );
break;
case IMAGETYPE_PNG:
$src_img = imagecreatefrompng( $img );
break;
case IMAGETYPE_GIF:
$src_img = imagecreatefromgif( $img );
break;
default:
return false;
}
// Did the image fail to load?
if ( !$src_img ) {
return false;
}
// Calculate the output size based on the original's aspect ratio
if ( $src_width > $src_height )
{
$thumb_w = $max_dimension;
$thumb_h = $src_height * $max_dimension / $src_width;
}
else { // it's either portrait, or square
$thumb_h = $max_dimension;
$thumb_w = $src_width * $max_dimension / $src_height;
}
// Create the output image and copy to source to it
$dst_img = ImageCreateTrueColor( $thumb_w, $thumb_h );
imagecopyresampled( $dst_img, $src_img, 0,0,0,0, $thumb_w, $thumb_h, $src_width, $src_height );
// Define the thumbnail filename
$dst_filename = $thumbdir . '/' . basename( $img ) . ".photology_tb.jpg";
// Save the thumbnail as a JPEG
imagejpeg( $dst_img, $dst_filename );
// Clean up memory
imagedestroy( $dst_img );
imagedestroy( $src_img );
// Get back a URL - probably should just store the filename...
$dst_url= $this->get_image_url( $dst_filename );
return $dst_url;
}
/**
* post_filter_content_excerpt_out
* filters the post's excerpt to display only the thumbnail
**/
public function filter_post_content_excerpt_out ( $excerpt, $post )
{
if (isset( $post->info->photology_thumb ) ) {
return '<img src="' . $post->info->photology_thumb . '">';
} else {
return $excerpt;
}
}
}
?>