コード例 #1
0
ファイル: Creator.php プロジェクト: alexantr/image-resize
 /**
  * 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();
     }
 }