Deprecated: Function create_function() is deprecated in /home/ursaftwz/public_html/myscripthub.com/wp-content/plugins/codecolorer/lib/geshi.php on line 4698

Resize the image unlimited times without losing the original image’s quality

 

1. Create image.php  File

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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php

class Image {

private $data = false; // the original - never changes
private $width;
private $height;
private $type;
private $image; // created and updated in $this->resize()

public function url ($website) {
$website = str_replace(' ', '%20', trim($website));
if ($info = @getimagesize($website)) {
switch ($info[2]) {
case 1: $this->type = 'gif'; break;
case 2: $this->type = 'jpg'; break;
case 3: $this->type = 'png'; break;
default: return false; break; // image type not supported
}
$this->width = $info[0];
$this->height = $info[1];
$data = file_get_contents ($website);
if ($data !== FALSE) {
$this->data = $data;
return true;
}
}
return false;
}

public function type () {
$type = '';
if (!empty($this->type)) $type = '.' . $this->type;
return $type;
}

public function convert ($type) {
if (in_array($type, array('jpg', 'gif', 'png'))) {
$this->type = $type;
return $this->resize();
}
return false;
}

public function resize ($width='', $height='') { // constrains proportions within $width and $height
if ($this->data === FALSE) return false;
if (empty($width)) {
$width = $this->width;
$height = $this->height;
} else {
list ($width, $height) = $this->proportional ($width, $height);
}
$this->image = ''; // to clean the slate if necessary
$this->image = @imagecreatefromstring($this->data);
$image = imagecreatetruecolor ($width, $height);
if ($this->type == 'png') {
imagealphablending ($image, false);
imagesavealpha ($image, true);
$transparent = imagecolorallocatealpha ($image, 255, 255, 255, 127);
imagefilledrectangle ($image, 0, 0, $width, $height, $transparent);
} else {
$white = imagecolorallocate ($image, 255, 255, 255);
imagefilledrectangle ($image, 0, 0, $width, $height, $white);
}
imagecopyresampled ($image, $this->image, 0, 0, 0, 0, $width, $height, $this->width, $this->height);
$this->image = $image;
return true;
}

public function square ($pixels) { // generates a square image of $pixels size
if ($this->data === FALSE) return false;
$width = $height = $pixels;
$x = $y = 0;
$size = $this->width;
if ($this->height > $this->width) {
$y = round(($this->height - $this->width) / 2);
} elseif ($this->width > $this->height) {
$x = round(($this->width - $this->height) / 2);
$size = $this->height;
}
$this->image = ''; // to clean the slate if necessary
$this->image = @imagecreatefromstring($this->data);
$image = imagecreatetruecolor ($width, $height);
if ($this->type == 'png') {
imagealphablending ($image, false);
imagesavealpha ($image, true);
$transparent = imagecolorallocatealpha ($image, 255, 255, 255, 127);
imagefilledrectangle ($image, 0, 0, $width, $height, $transparent);
} else {
$white = imagecolorallocate ($image, 255, 255, 255);
imagefilledrectangle ($image, 0, 0, $width, $height, $white);
}
imagecopyresampled ($image, $this->image, 0, 0, $x, $y, $width, $height, $size, $size);
$this->image = $image;
return true;
}

public function save ($location, $quality=80) {
if (!is_dir(dirname($location))) mkdir(dirname($location), 0755, true);
if (!empty($this->image)) {
if ($this->type == 'jpg') {
return imagejpeg ($this->image, $location, $quality);
} elseif ($this->type == 'gif') {
return imagegif ($this->image, $location);
} elseif ($this->type == 'png') {
if ($quality >= 90) {
$quality = 0;
} else {
$quality = abs(round($quality / 10) - 9);
}
return imagepng ($this->image, $location, $quality);
}
} elseif (!empty($this->data)) {
if (file_put_contents ($location, $this->data)) return true;
}
return false;
}

public function display ($quality=80) {
if (!empty($this->image)) {
if ($this->type == 'jpg') {
header ('Content-type: image/jpeg');
return imagejpeg ($this->image, NULL, $quality);
} elseif ($this->type == 'gif') {
header ('Content-type: image/gif');
return imagegif ($this->image);
} elseif ($this->type == 'png') {
if ($quality >= 90) {
$quality = 0;
} else {
$quality = abs(round($quality / 10) - 9);
}
header ('Content-type: image/png');
return imagepng ($this->image, NULL, $quality);
}
} elseif (!empty($this->data)) {
switch ($this->type) {
case 'jpg': header ('Content-type: image/jpeg'); break;
case 'gif': header ('Content-type: image/gif'); break;
case 'png': header ('Content-type: image/png'); break;
}
echo $this->data;
return true;
}
return false;
}

private function proportional ($width, $height) {
if (empty($height)) $height = $width * .8;
$use = (($width / $height) <= ($this->width / $this->height)) ? 'width' : 'height';
$max_dimension = ($use == 'width') ? $width : $height;
$ratio = ($this->width >= $this->height) ? $max_dimension / $this->width : $max_dimension / $this->height;
if($this->width > $max_dimension || $this->height > $max_dimension) {
$width = round($this->width * $ratio);
$height = round($this->height * $ratio);
} else { // the resize is larger than the original
$width = round($this->width);
$height = round($this->height);
}
return array ($width, $height);
}

}

?>

2. Include  image.php

include_once(‘image.php’);

3.  Creates a new image object

$image = new Image;

 3.1. Opens an image resource from specified website

1
$image->url ('http://www.somesite.com/images/copy.jpg');

3.2 covert image gif  to  jpg

1
$image->convert ('jpg');

3.3  resize the original image within the width and height specified without losing the original image’s quality

1
2
3
4
5
resize ( integer $width [, integer $height ] ) //width in pixel,height in pixel

$image->resize (500);

$image->resize (600,500);

3.4 Generate a square image

1
$image->square (80); // size in pixel

3.5 save an image 

1
$image->save ('/images/copied.jpg');

3.6 Display image directly to the browser

1
$image->display (80);

 

 

Leave a Reply