-
Notifications
You must be signed in to change notification settings - Fork 0
/
imgResize.php
25 lines (21 loc) · 1019 Bytes
/
imgResize.php
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
<?php
function resize_image($target, $output, $w, $h, $ext){
list($org_w, $org_h) = getimagesize($target);
$scale_ratio = $org_w / $org_h;
// the below condition helps you to change the width or height of the newly created resized image according to the aspect ratio of the original image i.e ig orginial img is tall or if its really wide image. Hence the below condtions will help you resize the new image with proper height and width
if (($w/$h) > $scale_ratio){
$w = $h * $scale_ratio;
} else {
$h = $w / $scale_ratio;
}
$img = "";
if ($ext == "jpg" || $ext == "JPG"){
$img = imagecreatefromjpeg($target);
} else if ($ext == "png" || $ext == "PNG"){
$img = imagecreatefrompng($target);
}
$tci = imagecreatetruecolor($w, $h);
imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $org_w, $org_h);
imagejpeg($tci, $output, 80);
}
?>