-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththumb-o-matic.php
164 lines (140 loc) · 4.82 KB
/
thumb-o-matic.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
<?php
/*
Plugin Name: thumb-o-matic
Version: 0.1
Plugin URI: http://svn.wp-plugins.org/thumb-o-matic/
Description: thumb-o-matic is a very simple thumbnail generator.
Author: Mark Rosenstand
Author URI: http://borkware.net/~mark/
*/
/*
Copyright (C) 2005 Mark Rosenstand <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
// Options.
add_option('images_path', get_settings('fileupload_realpath'),
'Where to look for images.');
add_option('images_url', get_settings('fileupload_url'),
'Where to link images.');
add_option('cache_thumbs', 'true',
'Whether to cache thumbnails.');
add_option('thumb_cache_dir', '/thumbs',
'Where to store cached thumbnails, relative to upload directory.');
add_option('thumb_size', '160',
'Default thumbnail size.');
// Filter all posts.
add_filter('the_content', 'tom_parser', 4);
function tom_parser($post) {
// Parse <thumb /> tags with tom_tag_parser().
return preg_replace_callback('|<thumb .* />|',
'tom_tag_parser', $post);
}
function tom_tag_parser($thumb_tags) {
$output = '';
foreach ($thumb_tags as $thumb_tag) {
preg_match('|src="(.*?)"|', $thumb_tag, $matches);
$filename = str_replace(get_settings('siteurl'), '', $matches[1]);
// Generate a thumbnail.
tom_create_thumbnail($filename);
$image_url = get_option('images_url').'/'.$filename;
$thumb_url = get_option('images_url').get_option('thumb_cache_dir').'/'.$filename;
// Replace the fictional thumb tag.
$img_tag = str_replace('<thumb ', '<img ', $thumb_tag);
// Show the thumbnail instead of the original.
$img_tag = str_replace($filename, $thumb_url, $img_tag);
// Use our own CSS class.
$img_tag = str_replace(' />', ' class="thumb-o-matic" />', $img_tag);
$output .= "<a href=\"$image_url\">$img_tag</a>";
}
return $output;
}
function tom_create_thumbnail($filename) {
// Get options.
$image_path = get_option('images_path');
$thumb_path = $image_path.get_option('thumb_cache_dir');
$image = $image_path.'/'.$filename;
$thumbnail = $thumb_path.'/'.$filename;
$thumb_size = get_option('thumb_size');
// Check if the source image exists.
if (file_exists($image)) {
// Only generate a thumbnail if we don't cache.
if (get_option('cache_thumbs') == 'false'
// ... or there isn't already one ...
|| !file_exists("$thumbnail")
// ... or there is one, but the original has been modified since generation.
|| file_exists("$thumbnail") && filemtime($image) > filemtime($thumbnail)) {
// Get geometry from source image.
$src_geom = getimagesize($image);
// Check image type and initialize an image object
// with the corresponding gd function.
switch ($src_geom[2]) {
case 1:
function_exists('imagecreatefromgif')
? $image_obj = @imagecreatefromgif($image)
: print "Your version of gdlib doesn't support GIF images.";
break;
case 2:
(function_exists('imagecreatefromjpeg'))
? $image_obj = @imagecreatefromjpeg($image)
: print "Your version of gdlib doesn't support JPEG images.";
break;
case 3:
(function_exists('imagecreatefrompng'))
? $image_obj = @imagecreatefrompng($image)
: print "Your version of gdlib doesn't support PNG images.";
break;
default:
print "Unsupported image type.";
return 0;
}
// Keep image proportions.
$thumb_scale = $src_geom[0] / $thumb_size;
$dst_geom = array($thumb_size, $src_geom[1] / $thumb_scale);
// Initialize an image object for the thumbnail.
$thumb_obj = @imagecreatetruecolor($dst_geom[0], $dst_geom[1]);
// Preserve alpha transparency in the thumbnail.
imagealphablending($thumb_obj, false);
imagesavealpha($thumb_obj, true);
// Resample the image.
imagecopyresampled($thumb_obj, $image_obj, 0, 0, 0, 0,
$dst_geom[0], $dst_geom[1], $src_geom[0], $src_geom[1]);
// Flush thumbnail to file.
imagepng($thumb_obj, $thumbnail);
// Free memory.
imagedestroy($image_obj);
imagedestroy($thumb_obj);
return 1;
}
else {
// We don't cache thumbnails or it didn't need to be (re)created.
// However, we didn't try, so we didn't fail.
return 1;
}
}
else {
// File doesn't exist.
print "File doesn't exist: $image";
return 0;
}
}
// Add a CSS class to use.
add_action('wp_head', 'tom_header');
function tom_header() {
print '
<style type="text/css">
#content img.thumb-o-matic {
border: 0px;
clear: both;
float: right;
margin: 5px 0 5px 15px;
}
</style>';
}
?>