1: <?php
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: namespace FieldtypePDF;
28:
29: use DirectoryIterator;
30: use Exception;
31: use Pagefile;
32: use Pagefiles;
33: use Pageimage;
34: use Pageimages;
35:
36: 37: 38:
39: class PagePDF extends Pagefile
40: {
41: public static $defaultImageExtension = 'jpg';
42:
43: protected $options = array();
44:
45: protected $images;
46:
47: 48: 49: 50: 51: 52:
53: public function __construct(PagePDFs $pagefiles, $filename)
54: {
55: parent::__construct($pagefiles, $filename);
56:
57: $field = $pagefiles->getField();
58: foreach($field->getArray() as $key => $value) {
59: if (preg_match('/^converter(.+)$/', $key, $matches)) {
60: $this->options[lcfirst($matches[1])] = $value;
61: }
62: }
63:
64: if ($field->get('imageExtension')) {
65: $this->options['extension'] = $field->get('imageExtension');
66: }
67:
68: $this->images = new Pageimages($this->pagefiles->getPage());
69: }
70:
71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88:
89: public function ___toImage($page = 0, $options = array())
90: {
91: if (is_array($page)) {
92: $options = $page;
93: $page = 0;
94: }
95:
96: $defaultOptions = array(
97: 'extension' => self::$defaultImageExtension,
98: 'suffix' => array(),
99: 'forceNew' => false
100: );
101:
102: if ($page > 0) {
103: $defaultOptions['suffix'][] = 'page' . $page;
104: }
105:
106: $options = array_replace($defaultOptions, $this->options, $options);
107:
108: $suffixStr = '';
109: if(!empty($options['suffix'])) {
110: $suffix = is_array($options['suffix']) ? $options['suffix'] : array($options['suffix']);
111: sort($suffix);
112: foreach($suffix as $key => $s) {
113: $s = strtolower($this->wire('sanitizer')->fieldName($s));
114: if(empty($s)) unset($suffix[$key]);
115: else $suffix[$key] = $s;
116: }
117: if(count($suffix)) $suffixStr = '-' . implode('-', $suffix);
118: }
119:
120:
121: $basename = sprintf('%s%s.%s',
122: basename($this->basename(), "." . $this->ext()),
123: $suffixStr,
124: $options['extension']
125: );
126:
127: $filename = $this->pagefiles->path() . $basename;
128: $exists = file_exists($filename);
129:
130: if(! $exists || $options['forceNew']) {
131: if($exists && $options['forceNew']) {
132: $image = new Pageimage($this->images, $filename);
133: $image->unlink();
134: }
135:
136: try {
137: $converter = new PDFConverter($this->filename, $options);
138: $converter->toImage($page, $filename);
139:
140: if($this->config->chmodFile) {
141: chmod($filename, octdec($this->config->chmodFile));
142: }
143: } catch(Exception $e) {
144: if ($this->pagefiles->getPage()->template === 'admin') {
145: $this->error($e->getMessage());
146: $this->error("PDF to image conversion failed for $filename");
147: } else {
148: throw $e;
149: }
150: }
151: }
152:
153: $image = new Pageimage($this->images, $filename);
154: $this->images->add($image);
155: return $image;
156: }
157:
158: 159: 160: 161: 162: 163:
164: public function isImageOfThis($basename)
165: {
166: $imageName = basename($basename);
167: $originalName = basename($this->basename, "." . $this->ext());
168:
169: $re = '/^'
170: . $originalName
171: . '(?:-([-_a-zA-Z0-9]+))?'
172: . '\.[^.]+'
173: . '$/';
174:
175:
176: if(! preg_match($re, $imageName) || preg_match('/^.*pdf$/', $imageName)) {
177: return false;
178: }
179:
180: return true;
181: }
182:
183: 184: 185: 186: 187:
188: public function getImages()
189: {
190: $images = new Pageimages($this->pagefiles->page);
191: $dir = new DirectoryIterator($this->pagefiles->path);
192:
193: foreach($dir as $file) {
194: if($file->isDir() || $file->isDot()) continue;
195: if(! $this->isImageOfThis($file->getFilename())) continue;
196: $images->add($file->getFilename());
197: }
198:
199: return $images;
200: }
201:
202: 203: 204: 205: 206:
207: public function removeImages()
208: {
209: $images = $this->getImages();
210:
211: foreach($images as $image) {
212: $image->unlink();
213: }
214:
215: return $this;
216: }
217:
218: 219: 220: 221: 222: 223: 224:
225: public function unlink()
226: {
227: $this->removeImages();
228: return parent::unlink();
229: }
230:
231: 232: 233: 234: 235: 236:
237: public function thumbnail($width, $height = 0)
238: {
239: $height = $height ?: $width;
240:
241: $image = $this->toImage();
242: return $image->size($width, $height);
243: }
244:
245: 246: 247: 248:
249: public function isThumbnail($basename)
250: {
251: $images = $this->getImages();
252:
253: if ($images->count() == 0) {
254: $images->add($this->toImage());
255: }
256:
257: foreach($images as $image) {
258: if($image->basename === $basename || $image->isVariation($basename)) {
259: return true;
260: }
261: }
262:
263: return false;
264: }
265:
266: 267: 268:
269: public function removeThumbnails()
270: {
271: $this->removeImages();
272: }
273: }
274:
275: