Jump to content

Properly Cropping Images without affecting image content...


iPixel

Recommended Posts

I tried to google it but i'm not quite sure how to formulate the proper phrase to express what i'm looking to do.

 

What i'm trying to accomplish is cropping an image without chopping away any bits of the content within that image. For example, imagine you have a square image the size 1000px by 1000px canvas and within that canvas is a photo of an object that's roughly 400px BY 750px.

 

I would like to crop the 1000x1000 canvas to be within 5 pixels of the longest side of the content image, so in this case i'd like to crop it to a 755x755 squared pixel image.

 

So i guess the main question is how can i check for the content on a canvas which btw may not always be white, but might be other colors and of other things in the background. I understand some things may make it impossible, but let's just for now assume the canvas is always white.

 

Thanks!

Link to comment
Share on other sites

There are a whole slew of function for handling images.  Here's a function I use to resize the image to a max size (the resulting image will be no wider than $max_w and no taller than $max_h)  It's pulled out of a class, $this->location is the filename for the image in question.

 

  function resize_image($max_w,$max_h) {
    // This function resizes this image to fit in the box max_w bi max_h                                                       
    $image_size = getimagesize(kImagesDirectory.'/'.$this->location);
    $ratio = $image_size[0]/$image_size[1];
    $goal_ratio = $max_w/$max_h;

    // Calculate the new image size                                                                                            
    if ($ratio < $goal_ratio) {
      // Tall image                                                                                                            
      $height = $max_h;
      $width = $max_w*$ratio;
    } else {
      // Wide image                                                                                                            
      $width = $max_w;
      $height = $max_h/$ratio;
    }

    // Create a new image for the resized image                                                                                
    $newimage = imagecreatetruecolor($width,$height);
    $oldimage = imagecreatefromjpeg(kImagesDirectory.'/'.$this->location);
    imagecopyresampled($newimage,$oldimage,0,0,0,0,$width,$height,$image_size[0],$image_size[1]);
    imagejpeg($newimage,kImagesDirectory.'/'.$this->location);
  }

Link to comment
Share on other sites

It will end up being a healthy blend of both.

Yea content recognition is correct, i will look into the link you sent and hopefully there will be enough of what i need. Thanks for that.

 

Future posters, please keep posting any other ideas links and whatnot.

 

Thanks!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.