forked from mangoO-Microfinance/mangoO-Microfinance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction_pic.php
46 lines (37 loc) · 1.2 KB
/
function_pic.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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?PHP
/**
* Resizing uploaded image files
* @param int width : Target width dimension
* @param int height : Target height dimension
* @return string path : Storage path for newly created image file
*/
function resizeImage($width, $height, $path){
// Get original image dimensions
list($w, $h) = getimagesize($_FILES['image']['tmp_name']);
// Calculate new image size with ratio
$ratio = max($width/$w, $height/$h);
$h = ceil($height / $ratio);
$x = ($w - $width / $ratio) / 2;
$w = ceil($width / $ratio);
// New file name
$basename = strtolower(pathinfo($_FILES['image']['name'], PATHINFO_FILENAME));
$path = $path.$width.'x'.$height.'.jpg';
//$path = 'uploads/photos/cust'.$_SESSION['cust_id'].'_'.$width.'x'.$height.'.jpg';
// Read binary data from image file
$imgString = file_get_contents($_FILES['image']['tmp_name']);
// Create image from string
$image = imagecreatefromstring($imgString);
$tmp_img = imagecreatetruecolor($width, $height);
imagecopyresampled(
$tmp_img, $image,
0, 0,
$x, 0,
$width, $height,
$w, $h);
imagejpeg($tmp_img, $path, 95);
return $path;
// Cleanup memory
imagedestroy($image);
imagedestroy($tmp_img);
}
?>