Question : Center fit.. GD image size

Hello, base on the last script I'm trying to cropt to the center but fit the old size to the new size where if longer then it stops at the height if taller it stops at the width.. so it fits with out distortion.. I am having a brain fart and don't know where I went off as I had it working.. one of those days. tk for the help.. Cheers -Jeremy
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
The center part of the script from the other question... 

						$src_x = $src_y = 0;
						$src_w = $src_w;
						$src_h = $src_h;
						$cmp_x = $src_w  / $dst_w;
						$cmp_y = $src_h / $dst_h;
						// calculate x or y coordinate and width or height of source
						if ( $cmp_x > $cmp_y ) {
							$src_w = round( ( $src_w / $cmp_x * $cmp_y ) );
							$src_x = round( ( $src_w - ( $src_w / $cmp_x * $cmp_y ) ) / 2 );
						} elseif ( $cmp_y > $cmp_x ) {
							$src_h = round( ( $src_h / $cmp_y * $cmp_x ) );
							$src_y = round( ( $src_h - ( $src_h / $cmp_y * $cmp_x ) ) / 2 );
						}
						$dst_w=$numWidth;
						$dst_h=$numHeight;
						$src_w=$this->arrOriginalDetails[0];
						$src_h=$this->arrOriginalDetails[1];
Related Solutions: php GD crop

Answer : Center fit.. GD image size

Hey jeremyBass26:

I made a few changes to the code to do what you described.

The new image dimensions are set with the $reSizeW and $reSizeH variables.  In this example we are using 300 X 75.

This time if the source image is 600 x 200:
The new Image will be 300 X 75.
But it will only crop starting at position (0, 25) with a width of 600 and height of 150. that way it preserves the aspect ratio.
So the top 25 px and the bottom 25 px were cropped from the original before the re-size

If the source image is 1500 X 750:
The new image will be 300 X 75.
It will begin cropping the original starting as position (0, 188) with a width and height of 1500 and a height of 375 to preserve the aspect ratio.
so the top 188 px and the bottom 188 px were cropped from the original before the re-size.


Give it a try and let me know if this is what you wanted.


1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
<?php

$resOriginalImage='600_x_200.jpg';
$image_details = getimagesize($resOriginalImage);


if ($image_details === false){
	echo 'Not a valid image supplied, or this script does not have permissions to access it.';

}else{
	$Original_W = $image_details[0];
	$Original_H = $image_details[1];
	$type = $image_details[2];
	$mime = $image_details['mime'];
}

switch ($type){
	case 1:
		//	GIF
		$source = imagecreatefromgif($resOriginalImage);
		break;
		
	case 2:
		//	JPG
		$source = imagecreatefromjpeg($resOriginalImage);
		break;
		
	case 3:
		//	PNG
		$source = imagecreatefrompng($resOriginalImage);
		break;
		
	default:
		echo 'Unsupported image file format.';
		
}

$reSizeW = 300;
$reSizeH = 75;
$aspect_ratio = $reSizeW/$reSizeH;
$original_aspect_ratio = $Original_W / $Original_H;
$src_x = $src_y = 0;


	$newW = $reSizeW;
	$newH = $reSizeH;
	
	if($aspect_ratio > 1){
		//New image is wide
		$src_x = 0;
		$src_y = round(( $Original_H / 2 ) -  (($Original_W/$aspect_ratio) / 2));
		
		$src_w = $Original_W;
		$src_h = $Original_W/$aspect_ratio;
	}else{
		//New image is tall
		$src_x = round(( $Original_W / 2 ) -  (($Original_H * $aspect_ratio)/2));
		$src_y = 0;
		
		$src_w = $Original_H * $aspect_ratio ;
		$src_h = $Original_H;
	}

	
	
	$centerFitImage = imagecreatetruecolor($newW, $newH);
	imagecopyresampled($centerFitImage, $source, 0, 0, $src_x, $src_y,  $newW, $newH, $src_w, $src_h);
	
	//Save Center Fix Image.
	imagejpeg($centerFitImage, "new_center.jpg");


?>
Random Solutions  
 
programming4us programming4us