iPixel Posted January 11, 2011 Share Posted January 11, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/224095-properly-cropping-images-without-affecting-image-content/ Share on other sites More sharing options...
jdavidbakr Posted January 11, 2011 Share Posted January 11, 2011 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); } Quote Link to comment https://forums.phpfreaks.com/topic/224095-properly-cropping-images-without-affecting-image-content/#findComment-1157942 Share on other sites More sharing options...
litebearer Posted January 11, 2011 Share Posted January 11, 2011 I believe he is seeking - Content Based Image Recognition http://www.searchlores.org/finn_cbir_1.htm Quote Link to comment https://forums.phpfreaks.com/topic/224095-properly-cropping-images-without-affecting-image-content/#findComment-1157947 Share on other sites More sharing options...
jdavidbakr Posted January 11, 2011 Share Posted January 11, 2011 Ah, you're right. That's way over my head. Quote Link to comment https://forums.phpfreaks.com/topic/224095-properly-cropping-images-without-affecting-image-content/#findComment-1157956 Share on other sites More sharing options...
iPixel Posted January 11, 2011 Author Share Posted January 11, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/224095-properly-cropping-images-without-affecting-image-content/#findComment-1157959 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.