Overview

Namespaces

  • FieldtypePDF
  • None

Classes

  • FieldtypePDF
  • InputfieldPDF
  • Overview
  • Namespace
  • Class
  • Deprecated
  1: <?php
  2: 
  3: /*
  4:  * The MIT License
  5:  *
  6:  * Copyright 2015 Richard Jedlička <jedlicka.r@gmail.com> (http://uiii.cz)
  7:  *
  8:  * Permission is hereby granted, free of charge, to any person obtaining a copy
  9:  * of this software and associated documentation files (the "Software"), to deal
 10:  * in the Software without restriction, including without limitation the rights
 11:  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 12:  * copies of the Software, and to permit persons to whom the Software is
 13:  * furnished to do so, subject to the following conditions:
 14:  *
 15:  * The above copyright notice and this permission notice shall be included in
 16:  * all copies or substantial portions of the Software.
 17:  *
 18:  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 19:  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 20:  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 21:  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 22:  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 23:  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 24:  * THE SOFTWARE.
 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:  * Represents a single PDF file item attached to a page, typically via a FieldtypePDF field.
 38:  */
 39: class PagePDF extends Pagefile
 40: {
 41:     public static $defaultImageExtension = 'jpg';
 42: 
 43:     protected $options = array();
 44: 
 45:     protected $images;
 46: 
 47:     /**
 48:      * Construct a new PagePDF
 49:      *
 50:      * @param PagePDFs $pagefiles Owning collection
 51:      * @param string $filename Full path and filename to this pagefile
 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:      * Convert PDF to image
 73:      * 
 74:      * This generates one image for each combinations of $page and $options['suffix'].
 75:      * If the file already exists it isn't regenerated until $options['forceNew'] is TRUE.
 76:      * 
 77:      * @param int $page Number of PDF's page (indexed from 0) to be converted to image
 78:      * @param type $options
 79:      * 
 80:      * Available options are:
 81:      * 
 82:      * - sufix (string[]) - Suffixes to be used in image's filename
 83:      * - forceNew (boolean) - Whether to overwrite the image if already exists
 84:      * 
 85:      * Accepts also converter options, see {@link FieldtypePDF\PDFConverter::setOptions}.
 86:      * 
 87:      * @return Pageimage
 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:         // e.g. myfile.pdf -> myfile-page2.jpg
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:      * Test whether $basename is image generated from this PDF
160:      * 
161:      * @param type $basename
162:      * @return boolean
163:      */
164:     public function isImageOfThis($basename)
165:     {
166:         $imageName = basename($basename);
167:         $originalName = basename($this->basename, "." . $this->ext());  // excludes extension
168: 
169:         $re = '/^' 
170:             . $originalName // myfile
171:             . '(?:-([-_a-zA-Z0-9]+))?' // -suffix1 or -suffix1-suffix2, etc.
172:             . '\.[^.]+' // .jpg
173:             . '$/';
174: 
175:         // if regex does not match or file is PDF, return false
176:         if(! preg_match($re, $imageName) || preg_match('/^.*pdf$/', $imageName)) {
177:             return false;
178:         }
179: 
180:         return true;
181:     }
182: 
183:     /**
184:      * Get all images generated from this PDF
185:      *
186:      * @return Pageimages
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:      * Remove all generated images.
204:      * 
205:      * @return void
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:      * Delete the physical file on disk associated with this PDF.
220:      * 
221:      * Unlinks also all generated images.
222:      * 
223:      * @return bool
224:      */
225:     public function unlink()
226:     {
227:         $this->removeImages();
228:         return parent::unlink();
229:     }
230: 
231:     /**
232:      * @deprecated since version 1.1.0, please use toImage() instead
233:      * @param int $width
234:      * @param int $height
235:      * @return Pageimage
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:      * @deprecated since version 1.1.0, please use isImageOfThis() instead
247:      * @param string $basename
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:      * @deprecated since version 1.1.0, please use removeImages() instead
268:      */
269:     public function removeThumbnails()
270:     {
271:         $this->removeImages();
272:     }
273: }
274: 
275: 
FieldtypePDF API documentation generated by ApiGen