Skip to content
This repository
tree: 6ba6d64c74
Fetching contributors…

Cannot retrieve contributors at this time

file 312 lines (273 sloc) 8.327 kb
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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
<?php

/**
* ProcessWire Pageimage
*
* Represents a single image item attached to a page, typically via a FieldtypeImage field.
*
* ProcessWire 2.x
* Copyright (C) 2010 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://www.processwire.com
* http://www.ryancramer.com
*
*
* @property int $width Width of image, in pixels
* @property int $height Height of image, in pixels
* @property Pageimage $original Reference to original $image, if this is a resized version.
*
*/

class Pageimage extends Pagefile {

/**
* Reference to the collection of Pageimages that this Pageimage belongs to
*
*/
protected $pageimages;

/**
* Reference to the original image this variation was created from
*
* Applicable only if this image is a variation (resized version). It will be null in all other instances.
*
*/
protected $original = null;

/**
* Cached result of the getVariations() method
*
* Don't reference this directly, because it won't be loaded unless requested, instead use the getVariations() method
*
*/
private $variations = null;

/**
* Cached result of the getImageInfo() method
*
* Don't reference this directly, because it won't be loaded unless requested, instead use the getImageInfo() method
*
*/
private $imageInfo = array(
'width' => 0,
'height' => 0,
);

/**
* Construct a new Pagefile
*
* @param Pagefiles $pagefiles
* @param string $filename Full path and filename to this pagefile
*
*/
public function __construct(Pagefiles $pagefiles, $filename) {

if(!$pagefiles instanceof Pageimages) throw new WireException("Pageimage::__construct requires instance of Pageimages");
$this->pageimages = $pagefiles;
parent::__construct($pagefiles, $filename);
}

/**
* When a Pageimage is cloned, we reset it's width and height to force them to reload in the clone
*
*/
public function __clone() {
$this->imageInfo['width'] = 0;
$this->imageInfo['height'] = 0;
}

/**
* Return the web accessible URL to this Pagefile
*
*/
public function url() {
return $this->pagefiles->url . $this->basename;
}

/**
* Returns the disk path to the Pagefile
*
*/
public function filename() {
return $this->pagefiles->path . $this->basename;
}

/**
* Returns the basename of this Pagefile
*
*/
public function basename() {
return parent::get('basename');
}

/**
* Get a property from this Pageimage
*
*/
public function get($key) {
if($key == 'width') return $this->width();
if($key == 'height') return $this->height();
if($key == 'original') return $this->getOriginal();
return parent::get($key);
}

/**
* Gets the image information with PHP's getimagesize function and caches the result
*
*/
public function getImageInfo($reset = false) {

if($reset) $checkImage = true;
else if($this->imageInfo['width']) $checkImage = false;
else $checkImage = true;

if($checkImage && ($info = @getimagesize($this->filename))) {
$this->imageInfo['width'] = $info[0];
$this->imageInfo['height'] = $info[1];
}

return $this->imageInfo;
}

/**
* Return a Pageimage object sized/cropped to the specified dimensions.
*
* @param int $width
* @param int $height
* @param array $options Array of options to override default behavior (quality=90, upscaling=true, cropping=true)
* @return Pageimage
*
*/
public function size($width, $height, array $options = array()) {

$defaultOptions = array(
'upscaling' => true,
'cropping' => true,
'quality' => 90
);

$width = (int) $width;
$height = (int) $height;

$basename = basename($this->basename(), "." . $this->ext()); // i.e. myfile
$basename .= '.' . $width . 'x' . $height . "." . $this->ext(); // i.e. myfile.100x100.jpg
$filename = $this->pagefiles->path() . $basename;

if(!is_file($filename)) {
if(@copy($this->filename(), $filename)) {
$configOptions = wire('config')->imageSizerOptions;
if(!is_array($configOptions)) $configOptions = array();
$options = array_merge($defaultOptions, $configOptions, $options);
$sizer = new ImageSizer($filename);
$sizer->setOptions($options);
$sizer->resize($width, $height);
if($this->config->chmodFile) chmod($filename, octdec($this->config->chmodFile));
}
}

$pageimage = clone $this;
$pageimage->setFilename($filename);
$pageimage->setOriginal($this);

return $pageimage;
}

/**
* Multipurpose: return the width of the Pageimage OR return an image sized with a given width (and proportional height)
*
* If given a width, it'll return a new Pageimage object sized to that width.
* If not given a width, it'll return the width of this Pageimage
*
* @param int $n Optional width
* @param array $options Optional options (see size function)
* @return int|Pageimage
*
*/
public function width($n = 0, array $options = array()) {
if($n) return $this->size($n, 0, $options);
$info = $this->getImageInfo();
return $info['width'];
}

/**
* Multipurpose: return the height of the Pageimage OR return an image sized with a given height (and proportional width)
*
* If given a height, it'll return a new Pageimage object sized to that height.
* If not given a height, it'll return the height of this Pageimage
*
* @param int $n Optional height
* @param array $options Optional options (see size function)
* @return int|Pageimage
*
*/
public function height($n = 0, array $options = array()) {
if($n) return $this->size(0, $n, $options);
$info = $this->getImageInfo();
return $info['height'];
}

/**
* Get all size variations of this Pageimage as a Pageimages array of Pageimage objects.
*
* This is useful after a delete of an image (for example). This method can be used to track down all the child files that also need to be deleted.
*
* @return Pageimages
*
*/
public function getVariations() {

if(!is_null($this->variations)) return $this->variations;

$variations = new Pageimages($this->pagefiles->page);
$dir = new DirectoryIterator($this->pagefiles->path);
$basename = basename($this->basename, "." . $this->ext());

foreach($dir as $file) {
if($file->isDir() || $file->isDot()) continue;
if(!preg_match('/^' . $basename . '\.\d+x\d+\.' . $this->ext() . '$/', $file->getFilename())) continue;
$pageimage = clone $this;
$pageimage->setFilename($file->getPathname());
$pageimage->setOriginal($this);
$variations->add($pageimage);
}

$this->variations = $variations;
return $variations;
}

/**
* Delete all the alternate sizes associated with this Pageimage
*
* @return this
*
*/
public function removeVariations() {

$variations = $this->getVariations();

foreach($variations as $variation) {
if(is_file($variation->filename)) unlink($variation->filename);
}

$this->variations = null;
return $this;
}

/**
* Identify this Pageimage as a variation, by setting the Pageimage it was resized from
*
* @param Pageimage $image
* @return this
*
*/
public function setOriginal(Pageimage $image) {
$this->original = $image;
return $this;
}

/**
* If this is a variation, return the original, otherwise return null
*
* @return Pageimage|null
*
*/
public function getOriginal() {
if($this->original) return $this->original;
if(!preg_match('/^(.+\.)\d+x\d+\.' . $this->ext() . '$/', $this->basename(), $matches)) return null;
$basename = $matches[1] . $this->ext();
$this->original = $this->pagefiles->get($basename);
return $this->original;
}

/**
* Delete the physical file(s) associated with this Pagefile
*
*/
public function unlink() {
parent::unlink();
$this->removeVariations();
return $this;
}

/**
* Copy this Pageimage and any of it's variations to another path
*
* @param string $path
* @return bool True if successful
*
*/
public function copyToPath($path) {
if(parent::copyToPath($path)) {
foreach($this->getVariations() as $variation) {
if(is_file($variation->filename)) {
copy($variation->filename, $path . $variation->basename);
if($this->config->chmodFile) chmod($path . $variation->basename, octdec($this->config->chmodFile));
}
}
return true;
}
return false;
}
}
Something went wrong with that request. Please try again.