/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] CROPPING an image to a certain height and width

I need to crop an image so its exactly 309 x 175 pixels without stretching the image.

I can do a square but couldn’t figure out how to modify it:

[code=php]list($width, $height) = getimagesize($file) ;

$thumb_size = 100;

$size = getimagesize($file);
$width = $size[0];
$height = $size[1];

if($width > $height) {
$x = ceil(($width – $height) / 2 );
$y = 0;
$width = $height;
} elseif($height > $width) {
$y = ceil(($height – $width) / 2);
$x = 0;
$height = $width;
}

$tn = imagecreatetruecolor($thumb_size,$thumb_size);
$image = imagecreatefromjpeg($file);
imagecopyresampled($tn,$image,0,0,$x,$y,$thumb_size,$thumb_size,$width,$height);

imagejpeg($tn,$save,100);[/code]

Also I tried doing it this way, but it would just stretch the image:

[code=php] list($width, $height) = getimagesize($file);
$origRatio = $width/$height;

$modwidth = 309;
$modheight = 175;

$tn = imagecreatetruecolor($modwidth, $modheight);
$image = imagecreatefromjpeg($file);
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);

imagejpeg($tn, $save, 100) ; [/code]

There has got to be a way.. right??? ?

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@HHCOwnerJul 01.2009 — I haven't tested this script but you can try it.

Hope it helps. ?

[code=php]
1 <?
2 $image = 'pics/whatever.jpg'; // the image to crop
3 $dest_image = 'pics/cropped_whatever.jpg'; // make sure the directory is writeable
4 $margin = 10; // to keep the image and layer in sync
5 if(!isset($_POST['step'])) $title = '1';
6 else $title = $_POST['step'];
7 echo '
8 <html>
9 <head>
10 <title>lixlpixel image crop | step '.$title.'</title>
11 </head>
12 <body style="margin: '.$margin.'px;">';
13 if(!isset($_POST['tx']) && !isset($_POST['fx']))
14 {
15 echo '
16 <form method="post" action="'.$_SERVER['PHP_SELF'].'">
17 <input type="image" src="'.$image.'"><p>';
18 if(!isset($_POST['x']))
19 {
20 echo '
21 <input type="hidden" name="step" value="2">
22 click to mark first corner';
23 }else{
24 echo '
25 <input type="hidden" name="step" value="3">
26 <input type="hidden" name="fx" value="'.$_POST['x'].'">
27 <input type="hidden" name="fy" value="'.$_POST['y'].'">
28 click to mark second corner | <a href="'.$_SERVER['PHP_SELF'].'">start over</a>';
29 }
30 echo '
31 </form>
32 ';
33 }
34 if(isset($_POST['fx']))
35 {
36 echo '
37 <form method="post" action="'.$_SERVER['PHP_SELF'].'">
38 <input type="hidden" name="step" value="4">
39 <input type="image" src="'.$image.'">
40 <input type="hidden" name="tx" value="'.$_POST['fx'].'">
41 <input type="hidden" name="ty" value="'.$_POST['fy'].'">
42 <input type="hidden" name="width" value="'.($_POST['x']-$_POST['fx']).'">
43 <input type="hidden" name="height" value="'.($_POST['y']-$_POST['fy']).'"><p>
44 <div style="position: absolute;
45 left:'.($_POST['fx']+$margin).'px;
46 top: '.($_POST['fy']+$margin).'px;
47 width: '.($_POST['x']-$_POST['fx']).'px;
48 height: '.($_POST['y']-$_POST['fy']).'px;
49 border: 1px solid #fff;">
50 </div>
51 click image to crop rectangle | <a href="'.$_SERVER['PHP_SELF'].'">start over</a>
52 </form>';
53 }
54 if(isset($_POST['tx']))
55 {
56 $img = imagecreatetruecolor($_POST['width'],$_POST['height']);
57 $org_img = imagecreatefromjpeg($image);
58 $ims = getimagesize($image);
59 imagecopy($img,$org_img, 0, 0, $_POST['tx'], $_POST['ty'], $ims[0], $ims[1]);
60 imagejpeg($img,$dest_image,90);
61 imagedestroy($img);
62 echo '
63 <img src="'.$dest_image.'" width="'.$_POST['width'].'" height="'.$_POST['height'].'"><p>
64 <a href="'.$_SERVER['PHP_SELF'].'">try again</a>';
65 }
66 echo '
67 </body>
68 </html>';
69 ?>

[/code]
×

Success!

Help @cinematic_jesi spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.8,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...