1: <?php
2:
3: /**
4: * Core class for handling "non-image" files
5: *
6: * Text type files can be displayed in place of an image in themes
7: *
8: * supports files of the following types:
9: * .txt
10: * .htm
11: * .html
12: * The contents of these files are "dumpped" into a SPAN sized to a 24x36 ratioed box based on your
13: * theme "image size" option. This has a class of "textobject" so it can be styled.
14: *
15: * What this plugin really is for is to serve as a model of how a plugin can be made to handle file types
16: * that zenphoto does not handle natively.
17: *
18: * Some key points to note:
19: * 1. The naming convention for these plugins is class-«handler class».php.
20: * 2. The statement setting the plugin_is_filter variable must be near the front of the file. This is important
21: * as it is the indicator to the Zenphoto plugin loader to load the script at the same point that other
22: * object modules are loaded.
23: * 3. These objects are extension to the zenphoto "Image" class. This means they have all the properties of
24: * an image plus whatever you add. Of course you will need to override some of the image class functions to
25: * implement the functionality of your new class.
26: * 4. There is one VERY IMPORTANT method that you must provide which is not part of the "Image" base class. That
27: * getContent() method. This method is called by template-functions.php in place of where it would normally put a URL
28: * to the image to show. This method must do everything needed to cause your image object to be viewable by the
29: * browser.
30: *
31: * So, briefly, the first four lines of code below are the standard plugin interface to Admin. There is one small
32: * wrinkle you might notice--the code for 'plugin_description' includes a test which sets the variable $disable.
33: * As you might expect, there were some changes needed to zenphoto in order to get this concept to work. $disable
34: * is set to true if the revision of zenphoto that is attempting to load this plugin is lower than the one where the
35: * implementation first appeared. The interface variable 'plugin_disable' is set to this value telling Admin not to
36: * allow enabling of the plugin if the release level is too low.
37: *
38: * The line that follows insures that the plugin will not load when it should be disabled--just in case.
39: *
40: * Then there is a call on addPlginType(«file extension», «Object Name»); This function registers the plugin as the
41: * handler for files with the specified extension. If the plugin can handle more than one file extension, make a call
42: * to the registration function for each extension that it handles.
43: *
44: * The rest is the object class for handling these files.
45: *
46: * The code of the object instantiation function is mostly required. Plugin "images" follow the lead of videos in that
47: * if there is a real image file with the same name save the suffix, it will be considered the thumb image of the object.
48: * This image is fetched by the call on checkObjectsThumb(). There is also code in the getThumb() method to deal with
49: * this property.
50: *
51: * Since text files have no natural height and width, we set them based on the image size option. This happens after the call
52: * PersistentObject(). The rest of the code there sets up the default title.
53: *
54: * getThumb() is responsible for generating the thumbnail image for the object. As above, if there is a similar named real
55: * image, it will be used. Otherwise [for this object implementation] we will use a thumbnail image provided with the plugin.
56: * The particular form of the file name used when there is no thumb stand-in image allows zenphoto to choose an image in the
57: * plugin folder.
58: *
59: * @author Stephen Billard (sbillard)
60: * @package plugins
61: *
62: */
63: class TextObject extends Image {
64:
65: protected $watermark = NULL;
66: protected $watermarkDefault = NULL;
67:
68: /**
69: * creates a textobject (image standin)
70: *
71: * @param object $album the owner album
72: * @param string $filename the filename of the text file
73: * @return TextObject
74: */
75: function __construct($album, $filename, $quiet = false) {
76:
77: $this->watermark = getOption('TextObject_watermark');
78: $this->watermarkDefault = getOption('textobject_watermark_default_images');
79:
80: $this->common_instantiate($album, $filename, $quiet);
81: }
82:
83: /**
84: * Handles class common instantiation
85: * @param $album
86: * @param $filename
87: */
88: function common_instantiate($album, $filename, $quiet = false) {
89: global $_zp_supported_images;
90: $msg = false;
91: if (!is_object($album) || !$album->exists) {
92: $msg = gettext('Invalid Textobject instantiation: Album does not exist');
93: } else if (!$this->classSetup($album, $filename) || !file_exists($this->localpath) || is_dir($this->localpath)) {
94: $msg = gettext('Invalid Textobject instantiation: file does not exist');
95: }
96: if ($msg) {
97: $this->exists = false;
98: if (!$quiet) {
99: trigger_error($msg, E_USER_ERROR);
100: }
101: return;
102: }
103: $this->sidecars = $_zp_supported_images;
104: $this->objectsThumb = checkObjectsThumb($this->localpath);
105: $this->updateDimensions();
106: $new = $this->instantiate('images', array('filename' => $filename, 'albumid' => $this->album->getID()), 'filename');
107: if ($new || $this->filemtime != $this->get('mtime')) {
108: if ($new)
109: $this->setTitle($this->displayname); $title = $this->displayname;
110: $this->updateMetaData();
111: $this->set('mtime', $this->filemtime);
112: $this->save();
113: if ($new)
114: zp_apply_filter('new_image', $this);
115: }
116: }
117:
118: /**
119: * Returns the image file name for the thumbnail image.
120: *
121: * @param string $path override path
122: *
123: * @return s
124: */
125: function getThumbImageFile($path = NULL) {
126: global $_zp_gallery;
127: if (is_null($path)) {
128: $path = SERVERPATH;
129: }
130: if (is_null($this->objectsThumb)) {
131: switch (getSuffix($this->filename)) {
132: default: // just in case we extend and are lazy...
133: $img = '/textDefault.png';
134: break;
135: }
136: $imgfile = $path . '/' . THEMEFOLDER . '/' . internalToFilesystem($_zp_gallery->getCurrentTheme()) . '/images/' . $img;
137: if (!file_exists($imgfile)) {
138: $imgfile = $path . "/" . ZENFOLDER . '/' . PLUGIN_FOLDER . '/class-textobject/' . $img;
139: }
140: } else {
141: $imgfile = ALBUM_FOLDER_SERVERPATH . internalToFilesystem($this->imagefolder) . '/' . $this->objectsThumb;
142: }
143: return $imgfile;
144: }
145:
146: /**
147: * returns a link to the thumbnail for the text file.
148: *
149: * @param string $type 'image' or 'album'
150: * @return string
151: */
152: function getThumb($type = 'image') {
153: $ts = getOption('thumb_size');
154: $sw = getOption('thumb_crop_width');
155: $sh = getOption('thumb_crop_height');
156: list($custom, $cw, $ch, $cx, $cy) = $this->getThumbCropping($ts, $sw, $sh);
157: $wmt = $this->watermark;
158: if (empty($wmt)) {
159: $wmt = getWatermarkParam($this, WATERMARK_THUMB);
160: }
161: if (is_null($this->objectsThumb)) {
162: $mtime = $cx = $cy = NULL;
163: $filename = makeSpecialImageName($this->getThumbImageFile());
164: if (!$this->watermarkDefault) {
165: $wmt = '!';
166: }
167: } else {
168: $filename = filesystemToInternal($this->objectsThumb);
169: $mtime = filemtime(ALBUM_FOLDER_SERVERPATH . '/' . internalToFilesystem($this->imagefolder) . '/' . $this->objectsThumb);
170: }
171: $args = getImageParameters(array($ts, $sw, $sh, $cw, $ch, $cx, $cy, NULL, true, true, true, $wmt, NULL, NULL), $this->album->name);
172: $cachefilename = getImageCacheFilename($alb = $this->album->name, $this->filename, $args);
173: return getImageURI($args, $alb, $filename, $mtime);
174: }
175:
176: function getBody($w = NULL, $h = NULL) {
177: TextObject_deprecated_functions::getBody();
178: $this->getContent($w, $h);
179: }
180:
181: /**
182: * Returns the content of the text file
183: *
184: * @param int $w optional width
185: * @param int $h optional height
186: * @return string
187: */
188: function getContent($w = NULL, $h = NULL) {
189: $this->updateDimensions();
190: if (is_null($w))
191: $w = $this->getWidth();
192: if (is_null($h))
193: $h = $this->getHeight();
194: switch (getSuffix($this->filename)) {
195: case 'txt':
196: case 'htm':
197: case 'html':
198: return '<span style="display:block;width:' . $w . 'px;height:' . $h . 'px;" class="textobject">' . @file_get_contents($this->localpath) . '</span>';
199: default: // just in case we extend and are lazy...
200: return '<img src="' . html_encode(pathurlencode($this->getThumb())) . '">';
201: }
202: }
203:
204: /**
205: * Get a custom sized version of this image based on the parameters.
206: *
207: * @param string $alt Alt text for the url
208: * @param int $size size
209: * @param int $width width
210: * @param int $height height
211: * @param int $cropw crop width
212: * @param int $croph crop height
213: * @param int $cropx crop x axis
214: * @param int $cropy crop y axis
215: * @param string $class Optional style class
216: * @param string $id Optional style id
217: * @param bool $thumbStandin set to true to treat as thumbnail
218: * @param bool $effects ignored
219: * @return string
220: */
221: function getCustomImage($size, $width, $height, $cropw, $croph, $cropx, $cropy, $thumbStandin = false, $effects = NULL) {
222: if ($thumbStandin) {
223: $wmt = $this->watermark;
224: if (empty($wmt)) {
225: $wmt = getWatermarkParam($this, WATERMARK_THUMB);
226: }
227: } else {
228: $wmt = NULL;
229: }
230: if ($thumbStandin & 1) {
231: $args = getImageParameters(array($size, $width, $height, $cropw, $croph, $cropx, $cropy, NULL, $thumbStandin, NULL, $thumbStandin, $wmt, NULL, $effects), $this->album->name);
232: if ($this->objectsThumb == NULL) {
233: $filename = makeSpecialImageName($this->getThumbImageFile());
234: if (!$this->watermarkDefault) {
235: $args[11] = '!';
236: }
237: $mtime = NULL;
238: } else {
239: $filename = filesystemToInternal($this->objectsThumb);
240: $mtime = filemtime(ALBUM_FOLDER_SERVERPATH . '/' . internalToFilesystem($this->imagefolder) . '/' . $this->objectsThumb);
241: }
242: return getImageURI($args, $this->album->name, $filename, $mtime);
243: } else {
244: return $this->getContent($width, $height);
245: }
246: }
247:
248: /**
249: * (non-PHPdoc)
250: * @see zp-core/Image::getSizedImage()
251: */
252: function getSizedImage($size) {
253: switch (getOption('image_use_side')) {
254: case 'width':
255: case 'longest':
256: $w = $size;
257: $h = floor(($size * 24) / 36);
258: break;
259: case 'height':
260: case 'shortest':
261: $h = $size;
262: $w = floor(($size * 36) / 24);
263: break;
264: }
265:
266: return $this->getContent($w, $h);
267: }
268:
269: /**
270: * (non-PHPdoc)
271: * @see zp-core/Image::updateDimensions()
272: */
273: function updateDimensions() {
274: $size = getOption('image_size');
275: switch (getOption('image_use_side')) {
276: case 'width':
277: case 'longest':
278: $this->set('width', getOption('image_size'));
279: $this->set('height', floor((getOption('image_size') * 24) / 36));
280: break;
281: case 'height':
282: case 'shortest':
283: $this->set('height', getOption('image_size'));
284: $this->set('width', floor((getOption('image_size') * 36) / 24));
285: break;
286: }
287: }
288:
289: }
290:
291: ?>