1

可能重复:
计算图像大小比例以调整大小

我需要为我正在处理的项目自定义调整图像大小。我不想使用图书馆,因为我不想增加任何不必要的开销。我能够让我的班级缩放和裁剪图像。但是,它并没有给我想要的东西。目前它:

  1. 调整图像大小
  2. 将其裁剪到特定高度

但是,我需要它:

  1. 调整图像大小,使其适合指定的尺寸。因此,一个 1000 x 500 的图像必须适合 300 x 300,必须调整大小,使其成为最大的一面,适合。即 1000 / (3 + 1/3) = 300,因此长度必须是:500 / (3 + 1/3) = 150。
  2. 现在我们有一个 300 x 150 的图像,但不适合所需的 300 x 300 空间。因此,最后一步是将这个 300 x 150 图像居中放置在 300 x 300 白色画布上。

以下是我到目前为止所拥有的,但正如我所提到的,缺少第二个裁剪步骤:

class scale_and_crop
{
    public $width;
    public $height;

    public function __construct($width = 150, $height = 150) {
        $this->height = $height;
        $this->width = $width;
    }

    public function process($source, $target) {

      // 0. See if already geneated
      if (file_exists($target)) {
        // do nothing
      }
      else {

        // 1. recursively create folders
        $targetFolders = explode('/', $target);
        $mkThisDir = './';
        $count = 0;

        foreach ($targetFolders as $t) {
          if ($count == sizeof($targetFolders) - 1 ) continue;
          $mkThisDir .= $t.'/';
          if (!file_exists($mkThisDir) ) {
            mkdir($mkThisDir);
          }
          $count++;
        }

        // 2. ... and then copy
        $realTarget = './'.$target;
        copy( './'.$source, $realTarget);

        // 3. Resize
        $code = '.jpg';
        $image_path = $source;

        $thumb_width = $this->width;
        $thumb_height = $this->height;
        if (!(is_integer($thumb_width) && $thumb_width > 0) && !($thumb_width === "*")) {
            echo "The width is invalid";
            exit(1);
        }

        if (!(is_integer($thumb_height) && $thumb_height > 0) && !($thumb_height === "*")) {
            echo "The height is invalid";
            exit(1);
        }


            if (!file_exists($image_path)) {
               $image_path = strtolower($image_path);
              if (!file_exists($image_path)) {
                    $image_path = strtoupper($image_path);
                if (!file_exists($image_path)) {
                  print 'IMAGE_NOT_AVAILABLE'; 
                  die();
                }
              }
            }

            $source_image = null;
            $extension = pathinfo($image_path, PATHINFO_EXTENSION);
            switch ($extension) {
                case "jpg":
                case "jpeg":
                    $source_image = imagecreatefromjpeg($image_path);
                    break;
                case "gif":
                    $source_image = imagecreatefromgif($image_path);
                    break;
                case "png":
                    $source_image = imagecreatefrompng($image_path);
                    break;
                default:
                    print 'Fatal IMAGE Error: E838209-837620002'; die();  
                    break;
            }

            if ($source_image === FALSE) {
              die("Fatal error #42870664: could not generate image.");
        }

        $source_width = imageSX($source_image);
        $source_height = imageSY($source_image);


        if (($source_width / $source_height) == ($thumb_width / $thumb_height)) {
            $source_x = 0;
            $source_y = 0;
        }

        if (($source_width / $source_height) > ($thumb_width / $thumb_height)) {
            $source_y = 0;
            $temp_width = $source_height * $thumb_width / $thumb_height;
            $source_x = ($source_width - $temp_width) / 2;
            $source_width = $temp_width;
        }

        if (($source_width / $source_height) < ($thumb_width / $thumb_height)) {
            $source_x = 0;
            $temp_height = $source_width * $thumb_height / $thumb_width;
            $source_y = ($source_height - $temp_height) / 2;
            $source_height = $temp_height;
        }

        $target_image = ImageCreateTrueColor($thumb_width, $thumb_height);

        imagecopyresampled($target_image, $source_image, 0, 0, $source_x, $source_y, $thumb_width,     $thumb_height, $source_width, $source_height);

        $newFile = $target;

        $returnUrl = '';

        switch ($extension) {
            case "jpg":
            case "jpeg":
                imagejpeg($target_image,  $newFile, 100);
                $returnUrl = $newFile;
                break;
            case "gif":
                imagegif($target_image,  $newFile);
                $returnUrl = $newFile;
                break;
            case "png":
                imagepng($target_image, $newFile,  9);
                $returnUrl = $newFile; 
                break;
            default:
                exit(1);
                break;
        }

        imagedestroy($target_image);
        imagedestroy($source_image);

      }


    }


}

我怎样才能让它按照我想要的方式裁剪?

4

0 回答 0