-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageResize.php
140 lines (99 loc) · 3.38 KB
/
ImageResize.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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
/*
*/
interface ImageConstants{
const IMAGE_PATH = '/var/www/test/images/';
}
class ImageResize implements ImageConstants {
private $path;
private $image_width;
private $image_height;
private $image_type;
private $image_resource;
private $new_image_resource;
public $new_height;
public $new_width;
function __construct($path,$new_height,$new_width,$url=false){
if(!empty($path)){
// if the file name has space in it, encode it properly
$this->path= $path;
}else{
throw new Exception('Image path not available');
}
$this->new_height = $new_height;
$this->new_width = $new_width;
if($url==true){
$this->downLoadImage();
$this->createNewImage();
$this->copyResizedImage();
$this->copyFileLocally();
}
}
////function_exists( 'exif_imagetype' )
private function downLoadImage(){
$image_src = imagecreatefromstring(file_get_contents($this->path));
$this->image_resource = $image_src;
$this->image_height = imagesy($this->image_resource);
$this->image_width = imagesx($this->image_resource);
$this->setImageType(exif_imagetype($this->path));
}
public function getImageWidth(){
return $this->image_width;
}
public function getImageHeight(){
return $this->image_height;
}
/*
* Customize according to the needs
*/
public function setImageType($image_type){
$temp ='';
switch ($image_type){
case IMAGETYPE_JPEG:
//2 for both jpg and jpeg
$temp= 'jpeg';
break;
case IMAGETYPE_PNG:
//3
$temp ='png';
break;
// case IMAGETYPE_BMP:
// //6
// return 'bmp';
// Refer PHP manual
default:
break;
}
$this->image_type = $temp;
}
public function getImageType(){
return $this->image_type ;
}
private function createNewImage(){
$this->new_image_resource = imagecreatetruecolor($this->new_width, $this->new_height);
}
private function copyResizedImage(){
imagecopyresized($this->new_image_resource, $this->image_resource, 0, 0, 0, 0, $this->new_width, $this->new_height, $this->getImageWidth(), $this->getImageHeight());
}
private function copyFileLocally(){
$image_convert_function ='image'.$this->getImageType();
$random_string = $this->getRandomString();
$path = self::IMAGE_PATH.$random_string.'.'.$this->getImageType();
$image_convert_function($this->new_image_resource,$path);
}
public function getRandomString(){
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randstring = '';
for ($i = 0; $i < 10; $i++){
$randstring .= $characters[rand(0, strlen($characters))];
}
return $randstring;
}
public function __destruct() {
imagedestroy($this->image_resource);
imagedestroy($this->new_image_resource);
}
}
$image = new ImageResize('http://4.bp.blogspot.com/-54mMfVRIguw/UH3m5ZRPXOI/AAAAAAAAJ1g/K6uHNoGtwpA/s500/GOOGLE_CBF_009.jpg',72,72,true);
?>