function image_resize img max_w max_h new_image Getting original width

 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
function image_resize($img, $max_w, $max_h,$new_image)
{
// Getting original width, height
list($or_w, $or_h, $or_t) = getimagesize($img);
// is JPG?
if ($or_t == 2) {
// calculating ratio
$ratio = ($or_h / $or_w);
// Original image
$or_image = imagecreatefromjpeg($img);
// Resize or not?
if ($or_w > $max_w || $or_h > $max_h) {
// resize by height, then width (height dominant)
if ($max_h < $max_w) {
$rs_h = $max_h;
$rs_w = $rs_h / $ratio;
}
// resize by width, then height (width dominant)
else {
$rs_w = $max_w;
$rs_h = $ratio * $rs_w;
}
// Old to new image
$rs_image = imagecreatetruecolor($rs_w, $rs_h);
imagecopyresampled($rs_image, $or_image, 0, 0, 0, 0, $rs_w, $rs_h, $or_w, $or_h);
}
// image requires no resizing
else {
$rs_w = $or_w;
$rs_h = $or_h;
$rs_image = $or_image;
}
// generate new(resized) image
imagejpeg($rs_image, $new_image, 100);
}
}