Jump to content

cropping an image


1internet

Recommended Posts

e.g. if image is 500x300

and I want it to be 200x200
then the new width and height will be 200x200
but first the new dimensions of the image will be created, so the height will go from 300 to 200
and then the width will become 500 x 200/300 = 333
then we will use 333-200 = 133
this is how much we need to crop the width
but it needs to be cropped each side so, 133/2 = 66
so 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.

Link to comment
Share on other sites

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 by 1internet
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

$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 by 1internet
Link to comment
Share on other sites

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?

post-27180-0-71900400-1364334741_thumb.jpg

post-27180-0-29878300-1364334748_thumb.jpg

Link to comment
Share on other sites

@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 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.

Edited by 1internet
Link to comment
Share on other sites

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 by PaulRyan
Link to comment
Share on other sites

@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.

Link to comment
Share on other sites

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.

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.