Exemplo n.º 1
0
 /**
  * Clean url
  *
  * @param string $image_url
  *
  * @return string
  */
 public static function cleanImageUrl($image_url)
 {
     $image_url = str_replace('\\', '/', $image_url);
     $image_url = preg_replace('{^\\./}', '', $image_url);
     while (strpos($image_url, '/./') !== false) {
         $image_url = str_replace('/./', '/', $image_url);
         // remove "/./"
     }
     while (preg_match('{(^|/)[^/]+/\\.\\./}', $image_url)) {
         $image_url = preg_replace('{(^|/)[^/]+/\\.\\./}', '$1', $image_url);
         // remove "folder/../"
     }
     $image_url = preg_replace('{^\\.\\./[^/]+/}', '', $image_url);
     // remove "../folder/" from beginning
     $image_url_exploded = explode('#', $image_url);
     $image_url_exploded = explode('?', $image_url_exploded[0]);
     $image_url = $image_url_exploded[0];
     if (strpos($image_url, '/') === 0) {
         $baseUrl = UrlHelper::getBaseUrl();
         if (!empty($baseUrl) && strpos($image_url, $baseUrl) === 0) {
             $image_url = mb_substr($image_url, mb_strlen($baseUrl));
         }
     }
     $image_url = ltrim($image_url, '/');
     return $image_url;
 }
Exemplo n.º 2
0
 /**
  * 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;
 }
Exemplo n.º 3
0
$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);
test_image($src);
// wrong url - no extension
$src = Image::init('foobar')->crop(150, 150);
test_image($src);
echo "<br>\n";
// quality examples
$src = Image::init('uploads/folder/antelope_canyon.jpg')->quality(100)->crop(250, 200);
test_image($src);
$src = Image::init('uploads/folder/antelope_canyon.jpg')->quality(50)->crop(250, 200);
test_image($src);