1: <?php
2: /**
3: * Klutz Image Class
4: *
5: * @author Marcus I. Ryan <marcus@riboflavin.net>
6: * @package Klutz
7: */
8: class Klutz_Image
9: {
10: /**
11: * The name of the file the image is stored in (if it's stored locally)
12: *
13: * @var string
14: */
15: var $file = null;
16:
17: /**
18: * The image data itself (binary)
19: *
20: * @var string
21: */
22: var $data = null;
23:
24: /**
25: * The height of the image in pixels
26: *
27: * @var integer
28: */
29: var $height = null;
30:
31: /**
32: * The width of the image in pixels
33: *
34: * @var integer
35: */
36: var $width = null;
37:
38: /**
39: * The mime type of the image
40: *
41: * @var string
42: */
43: var $type = null;
44:
45: /**
46: * The attributes to use in an <img> tag to define the size
47: * (e.g. height="120" width="400")
48: *
49: * @var string
50: */
51: var $size = null;
52:
53: /**
54: * The last modified time of the file (only used if file is local).
55: *
56: * @var integer
57: */
58: var $lastmodified = 0;
59:
60: /**
61: * Constructor - Based on the information passed, loads an image,
62: * determines the size and type, etc., and stores the information in
63: * the various public properties. Any optional parameters not passed
64: * in are calculated to the best of our ability.
65: *
66: * @param string $image Either raw image data or a filename
67: * @param string $type Image MIME type (e.g. image/jpeg)
68: * @param integer $height Height of the image in pixels
69: * @param integer $width Width of the image in pixels
70: */
71: function Klutz_Image($image, $type = null, $height = null, $width = null)
72: {
73: $argc = 1;
74: if (!is_null($height)) {
75: $argc++;
76: $this->height = $height;
77: }
78: if (!is_null($width)) {
79: $argc++;
80: $this->width = $width;
81: }
82: if (!is_null($type)) {
83: $argc++;
84: $this->type = $type;
85: }
86:
87: $image_info = @getimagesize($image);
88:
89: // if $image_info is false, then $image doesn't point to a file name
90: if ($image_info === false) {
91: $this->data = $image;
92:
93: // If we need to use getimagesize and we were passed data
94: // write it to a tempfile so getimagesize will work.
95: if ($argc < 4) {
96: $tmpfile = Horde::getTempFile('klutz');
97: $fp = fopen($tmpfile, 'wb+');
98: fwrite($fp, $image);
99: fclose($fp);
100: $image_info = @getimagesize($tmpfile);
101:
102: // if $image_info is false, it's an invalid image...
103: if ($image_info === false) {
104: return null;
105: }
106: }
107: } else {
108: $this->file = $image;
109: $this->lastmodified = filemtime($this->file);
110: $this->data = file_get_contents($image);
111: }
112:
113: if (is_null($this->height)) {
114: $this->height = $image_info[KLUTZ_FLD_HEIGHT];
115: }
116: if (is_null($this->width)) {
117: $this->width = $image_info[KLUTZ_FLD_WIDTH];
118: }
119: if (is_null($this->type)) {
120: global $klutz;
121: $this->type = $klutz->image_types[$image_info[KLUTZ_FLD_TYPE]];
122: }
123:
124: $this->size = '';
125: if (!empty($this->height)) {
126: $this->size = ' height="' . $this->height . '"';
127: }
128: if (!empty($this->width)) {
129: $this->size .= ' width="' . $this->width . '"';
130: }
131: }
132:
133: }
134: