1

Possible Duplicate:
Calculating image size ratio for resizing

I need my own custom resizing of images for the project I am working on. I don't want to use a library, because I don't want to add any unnecessary overheads. I was able to get my class to to scale and crop the image. However, it's not quite giving me what I want. Currently it:

  1. Resizes the image
  2. Crops it to a specific height

But, I need it to:

  1. Resize the image so it fits in the specified dimensions. So an image that is 1000 x 500 that must fit into 300 x 300, must be resized so it's LARGEST side, fits. i.e 1000 / (3 + 1/3) = 300, therefore the length must be: 500 / (3 + 1/3) = 150.
  2. Now we have an image that is 300 x 150, but does not fit in the desired 300 x 300 space. So, the final step would then be to center this 300 x 150 image on a 300 x 300 white canvas.

Below is what I have so far, but as I mentioned, the second cropping step is missing:

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);

      }


    }


}

How would I get it to crop the way I want it to?

Community
  • 1
  • 1
rockstardev
  • 13,479
  • 39
  • 164
  • 296

0 Answers0