/** * Resize a public image based on it's URL * * @param string $url The image to resize * * @return File An object representing the newly resized image. */ public function resize($url) { $url = '/' . ltrim($url, '/'); if (!file_exists($this->_cachePath) || !is_writeable($this->_cachePath)) { throw new \RuntimeException('Cache directory does not exist or is not writeable.'); } // dont cache an already cached file if (substr($url, 0, strlen($this->_cacheDir . '/')) === $this->_cacheDir . '/') { throw new \RuntimeException('Files inside the cache cannot be cached again.'); } // parse the parameters. Matches URLs like // - /resize/files/test_800xAUTO-b151b7.jpg // - /resize/files/another_image-white_400x300-72acf5a.gif if (!preg_match("/(.*)_(.+)\\-([a-f0-9]{6})(\\.[a-zA-Z]+)\$/u", $url, $matches)) { throw new Exception\BadParameters('Not a valid resize path.'); } list($match, $path, $paramString, $hash, $ext) = $matches; $this->_validateHash($path, $paramString, $hash, $ext); $params = $this->_parseParams($paramString); // lets generate an image! $original = new File($this->_publicDirectory . $path . $ext); // ensure original exists if (!file_exists($original) || !is_file($original)) { throw new Exception\NotFound('Neither the original file nor the default-image exist.'); } // make sure the target dir exists and we can write to it. $saved = new File($this->_cachePath . $url); $savedRaw = new File($saved->getRealPath()); $fs = new Filesystem(); $fs->mkdir($savedRaw->getPath(), 0777); if ($params['width'] === self::DIMENSION_AUTO || $params['height'] === self::DIMENSION_AUTO) { $mode = \Imagine\Image\ImageInterface::THUMBNAIL_INSET; } else { $mode = \Imagine\Image\ImageInterface::THUMBNAIL_OUTBOUND; } $box = new \Imagine\Image\Box($params['width'], $params['height']); $image = $this->_imagine->open($original->getPathname())->thumbnail($box, $mode)->save($saved->getRealPath(), array('quality' => $this->_defaultQuality)); $fs->chmod($saved->getRealPath(), 0777); return $saved; }