/** * Get params from path * * @param string $path * * @return array|bool */ protected static function parsePath($path) { $methods = implode('|', Creator::$methods); if (preg_match('{^(([0-9]{1,4})-([0-9]{1,4})-(' . $methods . ')(?:-q([0-9]{1,2}|100))?(?:-([a-f0-9]{3}|[a-f0-9]{6}))?(?:-([a-z]+))?)/(.+)}', $path, $m)) { $params = $m[7]; $params = str_split($params); return array('dir_name' => $m[1], 'width' => (int) $m[2], 'height' => (int) $m[3], 'method' => $m[4], 'quality' => $m[5] !== '' ? ImageHelper::processQuality($m[5]) : Creator::$defaultQuality, 'bg_color' => ImageHelper::processColor($m[6]), 'silhouette' => in_array('s', $params), 'disable_alpha' => in_array('a', $params), 'place_upper' => in_array('u', $params), 'no_top_offset' => in_array('n', $params), 'no_bottom_offset' => in_array('b', $params), 'disable_copy' => in_array('c', $params), 'skip_small' => in_array('t', $params), 'image_url' => trim($m[8])); } else { return false; } }
/** * Resize - base method * * @param string $method * @param int $width * @param int $height * * @return string */ public function resize($method, $width, $height) { $width = (int) $width; $height = (int) $height; // wrong params if (empty($this->imageUrl) || $width < Creator::$minSize || $height < Creator::$minSize || $width > Creator::$maxSize || $height > Creator::$maxSize || !in_array($method, Creator::$methods)) { return ImageHelper::getBlankImageUrl(); } // absolute link if (preg_match('{^(https?:)?//}', $this->imageUrl)) { return $this->imageUrl; } // clean url $image_url = ImageHelper::cleanImageUrl($this->imageUrl); if (empty($image_url)) { ImageHelper::getBlankImageUrl(); } // check extension $destExt = pathinfo($image_url, PATHINFO_EXTENSION); $destExt = strtolower($destExt); if (empty($destExt) || !in_array($destExt, array('jpeg', 'jpg', 'png', 'gif'))) { return ImageHelper::getBlankImageUrl(); } // set dir name with all params $resizedDir = "{$width}-{$height}-{$method}"; $resizedDir .= $this->quality != Creator::$defaultQuality ? "-q{$this->quality}" : ''; $resizedDir .= ($this->disableAlpha || $method == 'place') && $this->bgColor != Creator::$defaultBgColor ? "-{$this->bgColor}" : ''; // additional params $params = ''; $params .= $this->silhouette ? 's' : ''; $params .= $this->disableAlpha ? 'a' : ''; $params .= $method == 'crop' && !$this->noTopOffset && !$this->noBottomOffset && $this->placeUpper ? 'u' : ''; $params .= $method == 'crop' && $this->noTopOffset ? 'n' : ''; $params .= $method == 'crop' && !$this->noTopOffset && $this->noBottomOffset ? 'b' : ''; $params .= $this->disableCopy ? 'c' : ''; $params .= !$this->disableCopy && $this->skipSmall ? 't' : ''; $resizedDir .= !empty($params) ? '-' . $params : ''; return UrlHelper::getBaseUrl() . Creator::$resizedBaseDir . '/' . $resizedDir . '/' . $image_url; }
<?php require '../vendor/autoload.php'; use Alexantr\ImageResize\Helpers\ImageHelper; use Alexantr\ImageResize\Helpers\UrlHelper; use Alexantr\ImageResize\Image; // base examples $src = Image::init('uploads/folder/antelope_canyon.jpg')->crop(150, 150); test_image($src); // double "../" $url = 'uploads/folder/../../uploads/folder/floating_leaves.jpg'; echo '<!-- ' . ImageHelper::cleanImageUrl($url) . ' -->' . "\n"; $src = Image::init($url)->fitHeight(150); test_image($src); $src = Image::init('uploads/./././folder/../Cat.jpeg')->crop(150, 150); test_image($src); $src = Image::init('./uploads/Cat.jpeg')->placeUpper()->crop(150, 150); test_image($src); $src = Image::init('example/../uploads/Cat.jpeg')->noTopOffset()->crop(150, 150); test_image($src); $src = Image::init('uploads/Cat.jpeg?foo=bar')->noBottomOffset()->crop(150, 150); test_image($src); $src = Image::init('../example/uploads/cat.gif#foobar')->fitWidth(150); test_image($src); // variant with path relative to document root $url = UrlHelper::getBaseUrl() . '/uploads/Apple.png'; echo '<!-- ' . $url . ' -->' . "\n"; $src = Image::init($url)->place(120, 150); test_image($src); // wrong url $src = Image::init('folder/foo.bar')->crop(150, 150);