1internet Posted March 25, 2013 Share Posted March 25, 2013 e.g. if image is 500x300 and I want it to be 200x200then the new width and height will be 200x200but first the new dimensions of the image will be created, so the height will go from 300 to 200and then the width will become 500 x 200/300 = 333then we will use 333-200 = 133this is how much we need to crop the widthbut it needs to be cropped each side so, 133/2 = 66so we will crop 66 each side So I imagine it will be something like $new_image = imagecreatetruecolor($new_width, $new_height); imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height) Only instead of 0,0,0,0 there will be 66, in there somewhere, but I have experimented with it, with no luck. Can't really get my head around this here, I am sure there is some easier solution. Appreciate all help. Quote Link to comment Share on other sites More sharing options...
1internet Posted March 25, 2013 Author Share Posted March 25, 2013 (edited) UPDATE imagecopyresampled($new_image, $old_image, 66, 0, 0, 0, $new_width-133, $new_height, $image_width, $image_height) I thought this might work, but doesn't seem to either, but might hopefully shed some more light on what I am trying to achieve. Edited March 25, 2013 by 1internet Quote Link to comment Share on other sites More sharing options...
DaveyK Posted March 25, 2013 Share Posted March 25, 2013 Well, I personally like to calculate the full dimensions of the to-be-created image before running the actual creation of the image. What you first need to know is... is the source image always going to be same size? (So always 500x300?) Quote Link to comment Share on other sites More sharing options...
1internet Posted March 25, 2013 Author Share Posted March 25, 2013 No, but I can calculate that easily, I was just using it as an example, as to what to do from there. Quote Link to comment Share on other sites More sharing options...
1internet Posted March 26, 2013 Author Share Posted March 26, 2013 So I have imagecopyresampled ($dst_image , $src_image, 0, 0, $src_x, $src_y, $new_width, $new_height, $image_width-$src_x, $image_height-$src_y); src_x and src_y are the co-ordinates I want to crop the image from, but I am not sure how how to create the co-ordinates of where to cut them to - i.e. set the crop height and width. I have been working on this for hours testing every permutation under the sun, and cant figure it out. How is this done? Quote Link to comment Share on other sites More sharing options...
DaveyK Posted March 26, 2013 Share Posted March 26, 2013 How is this for starters? // Max Target dimensions $max_width = 1550; $max_height = 620; $max_size = 2048000; // 2000 kiloBits * 1024 = 2MB (MegaBytes); // Min Target Dimensions $min_width = 400; $min_height = 350; // Get current dimensions $old_width = $info[0]; $old_height = $info[1]; // Define the scale thats most relevant (so wide images scale in width and high images scale in height) $scale = min($max_width/$old_width, $max_height/$old_height); $new_width = ($old_width > $max_width) ? round($old_width * $scale, 2): $old_width; $new_height = ($old_height > $max_height) ? round($old_height * $scale, 2): $old_height; Its not a complete script, but it does calculate the new height. From what I understand you want to cut it from the center? Then why bother scaling it? Quote Link to comment Share on other sites More sharing options...
1internet Posted March 26, 2013 Author Share Posted March 26, 2013 I understand all that side, I am just trying to work out how I cut e.g. 50px from each side, I can only manage to get it from either the left or right, but not both. Quote Link to comment Share on other sites More sharing options...
Jessica Posted March 26, 2013 Share Posted March 26, 2013 instead of 0, 0, start at 50, 50... Quote Link to comment Share on other sites More sharing options...
1internet Posted March 26, 2013 Author Share Posted March 26, 2013 Thanks, but all that did was create some black areas on the image. Quote Link to comment Share on other sites More sharing options...
Jessica Posted March 26, 2013 Share Posted March 26, 2013 So I have imagecopyresampled ($dst_image , $src_image, 0, 0, $src_x, $src_y, $new_width, $new_height, $image_width-$src_x, $image_height-$src_y);src_x and src_y are the co-ordinates I want to crop the image from, but I am not sure how how to create the co-ordinates of where to cut them to - i.e. set the crop height and width. I have been working on this for hours testing every permutation under the sun, and cant figure it out. How is this done? I misread. If you have $new_width, $new_height they should be set to your new width and height. Post your entire code. Quote Link to comment Share on other sites More sharing options...
1internet Posted March 26, 2013 Author Share Posted March 26, 2013 (edited) $temp = $_FILES['image']['tmp_name']; $image_size = getimagesize($temp); $image_width = $image_size[0]; $image_height = $image_size[1]; $image_ratio = $image_width/$image_height; $image_name = 'crop_test'; $new_width = 500; $new_height = 250; $new_ratio = $new_width/$new_height; $offset_type = null; $resized_width = ''; $resized_height = ''; if($image_width == $image_height){ if ($new_width > $new_height) { $resized_width = $new_width; $resized_height = $new_width; $offset_type = 'height'; $offset = $resized_height - $new_height; } elseif ($new_height > $new_width) { $resized_height = $new_height; $resized_width = $new_height; $offset_type = 'width'; $offset = $resized_width - $new_width; } else { $resized_width = $new_width; $resized_height = $new_height; $offset == 0; } } if($new_ratio > $image_ratio){ $resized_width = $new_width; $ratio = $resized_width/$image_width; $resized_height = round($image_height*$ratio); $offset_type = 'height'; $offset = $resized_height-$new_height; } if($image_ratio > $new_ratio){ $resized_height = $new_height; $ratio = $resized_height/$image_height; $resized_width = round($image_width*$ratio); $offset_type = 'width'; $offset = $resized_width-$new_width; } if($offset_type == 'height'){ $src_y = round($offset/$ratio/2); $src_x = 0; } if($offset_type == 'width'){ $src_x = round($offset/$ratio/2); $src_y = 0; } $extension = '.jpg'; $src_image = imagecreatefromjpeg($temp); $dst_image = imagecreatetruecolor($new_width, $new_height); imagecopyresampled ($dst_image , $src_image, 0, 0, $src_x, $src_y, $new_width, $new_height, $image_width-$src_x, $image_height-$src_y); imagejpeg($dst_image , 'crop/'. $image_name.$extension, 100); So I want it to be able to crop the center of an image, if the width or height exceeds the $new_width or $new_height, once the image has been re-sized. I just used 500 and 250 as an example of the new image, but this will be arbitrary. Edited March 26, 2013 by 1internet Quote Link to comment Share on other sites More sharing options...
Barand Posted March 26, 2013 Share Posted March 26, 2013 (edited) Why don't you tell us exactly what you are trying to do instead of posting your solutions that don't work. All they tell us is what you are not trying to do. Edited March 26, 2013 by Barand Quote Link to comment Share on other sites More sharing options...
Jessica Posted March 26, 2013 Share Posted March 26, 2013 I copied your script, and using a static image (http://img.gawkerassets.com/img/18ip3heqdjqlwjpg/xlarge.jpg) I made this small change to your script: $src_w = $image_width-($src_x*2); $src_h = $image_height-($src_y*2); imagecopyresampled ($dst_image , $src_image, 0, 0, $src_x, $src_y, $new_width, $new_height, $src_w, $src_h); And got this cropped image. Is that what you were looking for? Quote Link to comment Share on other sites More sharing options...
1internet Posted March 26, 2013 Author Share Posted March 26, 2013 (edited) @Sen So I want to create thumbnails, all of equal sizes, e.g. 100x100. But the original sizes could be any dimensions, e.g. 700x400, 1200x600, 300x500, etc. So I want to resize the image so the width is 100, if it means that the height will be greater than 100, if not then then I will resize the height to 100, so that the width will be greater than 100. Then the dimension that is greater than 100,( e.g. if we resize 600x400 to 150x100, thus the 150 is greater than 100), will be subtracted from 100, 150-100 = 50. We then divide the 50 by 2 giving us 25, and remove 25px either side of the image. @The Web MasonThats what I can am trying to do, as it looks like you have cropped evenly from the top and the bottom, but I don't quite follow the logic. Edited March 26, 2013 by 1internet Quote Link to comment Share on other sites More sharing options...
Barand Posted March 26, 2013 Share Posted March 26, 2013 So, despite the question, cropping has nothing to do with it? Quote Link to comment Share on other sites More sharing options...
DaveyK Posted March 26, 2013 Share Posted March 26, 2013 (edited) Basic scale then crop Barand, correct me if I'm wrong but cutting off the sides if required is cropping right? EDIT: autocorrect... Edited March 26, 2013 by DaveyK Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted March 26, 2013 Share Posted March 26, 2013 (edited) Try this: <?PHP $filename = 'image.jpg'; $imageFile = imagecreatefromjpeg($filename); $imageData = getimagesize($filename); $maxWidth = 200; $maxHeight = 200; $imageWidth = $imageData[0]; $imageHeight = $imageData[1]; $widthDeduction = floor(($imageWidth-$maxWidth)/2); $heightDeduction = floor(($imageHeight-$maxHeight)/2); //### Just some display data #echo $imageWidth.' : '.$widthDeduction .'<br>'; #echo $imageHeight.' : '.$heightDeduction; #exit; $thumb = imagecreatetruecolor($maxWidth, $maxHeight); imagecopyresampled($thumb, $imageFile, 0 - $widthDeduction, // Center the image horizontally 0 - $heightDeduction, // Center the image vertically 0, 0, $imageWidth, $imageHeight, $imageWidth, $imageHeight); header('Content-Type: image/jpeg'); imagejpeg($thumb); ?> Edited March 26, 2013 by PaulRyan Quote Link to comment Share on other sites More sharing options...
Barand Posted March 26, 2013 Share Posted March 26, 2013 @AdvancedMember I think I get it. You want to crop the original to the same proportions as the thumbnail and then resize? Quote Link to comment Share on other sites More sharing options...
Jessica Posted March 26, 2013 Share Posted March 26, 2013 @The Web Mason Thats what I can am trying to do, as it looks like you have cropped evenly from the top and the bottom, but I don't quite follow the logic. It worked, didn't it? The logic is right there in the code I posted. I only added two lines and changed one. Also, our names are above our titles. I am Jessica and the other person you replied to is Barand. Quote Link to comment Share on other sites More sharing options...
1internet Posted March 27, 2013 Author Share Posted March 27, 2013 @Jessica Yes, it did, and I get the logic now too, it totally makes sense. That's awesome, thanks a lot. Quote Link to comment Share on other sites More sharing options...
Jessica Posted March 27, 2013 Share Posted March 27, 2013 So, what I changed was instead of just subtracting half of the cropped amount from the height (or width), I subtracted the entire thing. If your image was 100 px tall and you needed to crop 10 off each side (so final image is 80 pixels tall), your code was doing 10 off the top as the starting point, and then (100-10) = 90 tall. That's an image which is still 90 pixels tall. You needed to take off the entire cropping amount from the height, and the half amount from the starting point. Quote Link to comment 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.