/** * Create image based on $path * * @param string $webroot * @param string $path * * @throws \Exception */ public static function create($webroot, $path) { // show blank image if ($path == self::BLANK_IMAGE_NAME) { self::showBlankImage(); } // get params from path $params = self::parsePath($path); if (!is_array($params)) { self::showBlankImage(); } $dir_name = $params['dir_name']; $width = $params['width']; $height = $params['height']; $method = $params['method']; $quality = $params['quality']; $bg_color = $params['bg_color']; $silhouette = $params['silhouette']; $disable_alpha = $params['disable_alpha']; $place_upper = $params['place_upper']; $no_top_offset = $params['no_top_offset']; $no_bottom_offset = $params['no_bottom_offset']; $disable_copy = $params['disable_copy']; $skip_small = $params['skip_small']; $image_url = $params['image_url']; // wrong params if (empty($image_url) || $width < self::$minSize || $height < self::$minSize || $width > self::$maxSize || $height > self::$maxSize || !in_array($method, self::$methods)) { self::showBlankImage(); } // clean url $image_url = ImageHelper::cleanImageUrl($image_url); if (empty($image_url)) { self::showBlankImage(); } // if image already exists $dest_path = $webroot . self::$resizedBaseDir . '/' . $dir_name . '/' . $image_url; if (is_file($dest_path)) { self::showImage($dest_path); } // original image abs path $orig_path = $webroot . '/' . $image_url; // create paths for missing image if (!is_file($orig_path)) { if ($silhouette) { if (self::$defaultSilhouettePath !== null && is_file(self::$defaultSilhouettePath)) { $image_name = basename(self::$defaultSilhouettePath); $orig_path = self::$defaultSilhouettePath; $dest_path = $webroot . self::$resizedBaseDir . '/' . $dir_name . '/custom-' . $image_name; } else { $image_name = self::DEFAULT_SILHOUETTE_NAME; $orig_path = __DIR__ . '/images/' . $image_name; $dest_path = $webroot . self::$resizedBaseDir . '/' . $dir_name . '/' . $image_name; } } else { if (self::$defaultImagePath !== null && is_file(self::$defaultImagePath)) { $image_name = basename(self::$defaultImagePath); $orig_path = self::$defaultImagePath; $dest_path = $webroot . self::$resizedBaseDir . '/' . $dir_name . '/custom-' . $image_name; } else { $image_name = self::DEFAULT_IMAGE_NAME; $orig_path = __DIR__ . '/images/' . $image_name; $dest_path = $webroot . self::$resizedBaseDir . '/' . $dir_name . '/' . $image_name; } } // already exists - with php each time if (is_file($dest_path)) { self::showImage($dest_path); } } // can't find default image if (!is_file($orig_path)) { self::showBlankImage(); } // check sizes $size = getimagesize($orig_path); if (!$size) { self::showBlankImage(); } $w = $size[0]; $h = $size[1]; if ($w == 0 || $h == 0 || empty($size['mime']) || !in_array($size['mime'], self::$mimeTypes)) { self::showBlankImage(); } $mime_type = $size['mime']; // create dir $dir_path = dirname($dest_path); if (!is_dir($dir_path)) { if (!@mkdir($dir_path, 0775, true)) { self::showBlankImage(); } } $rotate = 0; // try to read exif orientation if (function_exists('exif_read_data') && $mime_type == 'image/jpeg') { $exif = exif_read_data($orig_path); if (!empty($exif['Orientation'])) { switch ($exif['Orientation']) { case 3: $rotate = 180; break; case 6: $rotate = -90; break; case 8: $rotate = 90; break; } } } // switch width & height if ($rotate == 90 || $rotate == -90) { $old_w = $w; $w = $h; $h = $old_w; } if (!$disable_copy) { // copy with identical sizes if ($method == 'fitw' && $width == $w || $method == 'fith' && $height == $h || $method != 'fitw' && $method != 'fith' && $width == $w && $height == $h) { copy($orig_path, $dest_path); if (is_file($dest_path)) { self::showImage($dest_path); } } // copy smaller if ($skip_small) { if ($method == 'fitw' && $width >= $w || $method == 'fith' && $height >= $h || $method != 'fitw' && $method != 'fith' && $width >= $w && $height >= $h) { copy($orig_path, $dest_path); if (is_file($dest_path)) { self::showImage($dest_path); } } } } if ($mime_type == 'image/gif') { $im = imagecreatefromgif($orig_path); } elseif ($mime_type == 'image/png') { $im = imagecreatefrompng($orig_path); } elseif ($mime_type == 'image/jpeg') { $im = imagecreatefromjpeg($orig_path); } else { $im = false; } if ($im === false) { self::showBlankImage(); } // rotate original if ($rotate != 0) { $im = imagerotate($im, $rotate, 0); } $dst_x = 0; $dst_y = 0; $x = 0; $y = 0; if ($method == 'crop') { $ratio = max($width / $w, $height / $h); $new_w = round($w * $ratio); $new_h = round($h * $ratio); $x = round(($w - $width / $ratio) / 2); if ($no_top_offset) { $y = 0; } elseif ($no_bottom_offset) { $y = floor($h - $height / $ratio); } else { $y = round(($h - $height / $ratio) / 2); } // place upper if ($y > 0 && $place_upper) { $y = round($y / 3 * 2); } } elseif ($method == 'fitw') { $new_w = $width; $new_h = $new_w / $w * $h; $width = $new_w; $height = $new_h; } elseif ($method == 'fith') { $new_h = $height; $new_w = $new_h * $w / $h; $width = $new_w; $height = $new_h; } elseif ($method == 'place') { $ratio = min($width / $w, $height / $h); $new_w = round($w * $ratio); $new_h = round($h * $ratio); $dst_x = round(($width - $new_w) / 2); $dst_y = round(($height - $new_h) / 2); } else { $ratio = min($width / $w, $height / $h); $new_w = round($w * $ratio); $new_h = round($h * $ratio); $width = $new_w; $height = $new_h; } // copying $rgb = ImageHelper::hex2rgb($bg_color); $new_im = imagecreatetruecolor($width, $height); if (!$disable_alpha && $mime_type == 'image/png') { imagealphablending($new_im, false); imagesavealpha($new_im, true); $color = imagecolorallocatealpha($new_im, $rgb['r'], $rgb['g'], $rgb['b'], 127); imagefilledrectangle($new_im, 0, 0, $width, $height, $color); } else { $color = imagecolorallocate($new_im, $rgb['r'], $rgb['g'], $rgb['b']); imagefill($new_im, 0, 0, $color); } imagecopyresampled($new_im, $im, $dst_x, $dst_y, $x, $y, $new_w, $new_h, $w, $h); // saving if ($mime_type == 'image/png') { imagepng($new_im, $dest_path, 9); } elseif ($mime_type == 'image/gif') { imagegif($new_im, $dest_path); } else { if (self::$enableProgressiveJpeg) { imageinterlace($new_im, 1); } imagejpeg($new_im, $dest_path, $quality); } imagedestroy($im); imagedestroy($new_im); if (is_file($dest_path)) { self::showImage($dest_path, $mime_type); } else { self::showBlankImage(); } }
/** * 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);