| 14509 |
anikendra |
1 |
<?php
|
|
|
2 |
class ResizeComponent extends Component
|
|
|
3 |
{
|
|
|
4 |
var $image;
|
|
|
5 |
var $image_type;
|
|
|
6 |
|
|
|
7 |
function load($filename) {
|
|
|
8 |
|
|
|
9 |
$image_info = getimagesize($filename);
|
|
|
10 |
$this->image_type = $image_info[2];
|
|
|
11 |
if( $this->image_type == IMAGETYPE_JPEG ) {
|
|
|
12 |
|
|
|
13 |
$this->image = imagecreatefromjpeg($filename);
|
|
|
14 |
} elseif( $this->image_type == IMAGETYPE_GIF ) {
|
|
|
15 |
|
|
|
16 |
$this->image = imagecreatefromgif($filename);
|
|
|
17 |
} elseif( $this->image_type == IMAGETYPE_PNG ) {
|
|
|
18 |
|
|
|
19 |
$this->image = imagecreatefrompng($filename);
|
|
|
20 |
}
|
|
|
21 |
}
|
|
|
22 |
function save($filename, $compression=100, $permissions=null) {
|
|
|
23 |
|
|
|
24 |
if( $this->image_type == IMAGETYPE_JPEG ) {
|
|
|
25 |
imagejpeg($this->image,$filename,$compression);
|
|
|
26 |
} elseif( $this->image_type == IMAGETYPE_GIF ) {
|
|
|
27 |
|
|
|
28 |
imagegif($this->image,$filename);
|
|
|
29 |
} elseif( $this->image_type == IMAGETYPE_PNG ) {
|
|
|
30 |
|
|
|
31 |
imagepng($this->image,$filename);
|
|
|
32 |
}
|
|
|
33 |
|
|
|
34 |
imagedestroy($this->image);
|
|
|
35 |
|
|
|
36 |
if( $permissions != null) {
|
|
|
37 |
|
|
|
38 |
chmod($filename,$permissions);
|
|
|
39 |
}
|
|
|
40 |
}
|
|
|
41 |
function output($image_type=IMAGETYPE_JPEG) {
|
|
|
42 |
|
|
|
43 |
if( $image_type == IMAGETYPE_JPEG ) {
|
|
|
44 |
imagejpeg($this->image);
|
|
|
45 |
} elseif( $image_type == IMAGETYPE_GIF ) {
|
|
|
46 |
|
|
|
47 |
imagegif($this->image);
|
|
|
48 |
} elseif( $image_type == IMAGETYPE_PNG ) {
|
|
|
49 |
|
|
|
50 |
imagepng($this->image);
|
|
|
51 |
}
|
|
|
52 |
}
|
|
|
53 |
function getWidth() {
|
|
|
54 |
return imagesx($this->image);
|
|
|
55 |
}
|
|
|
56 |
function getHeight() {
|
|
|
57 |
return imagesy($this->image);
|
|
|
58 |
}
|
|
|
59 |
function resizeToHeight($height) {
|
|
|
60 |
|
|
|
61 |
$ratio = $height / $this->getHeight();
|
|
|
62 |
$width = $this->getWidth() * $ratio;
|
|
|
63 |
$this->resize($width,$height);
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
function resizeToWidth($width) {
|
|
|
67 |
$ratio = $width / $this->getWidth();
|
|
|
68 |
$height = $this->getheight() * $ratio;
|
|
|
69 |
$this->resize($width,$height);
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
function scale($scale) {
|
|
|
73 |
$width = $this->getWidth() * $scale/100;
|
|
|
74 |
$height = $this->getheight() * $scale/100;
|
|
|
75 |
$this->resize($width,$height);
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
function resize($width,$height) {
|
|
|
79 |
$new_image = imagecreatetruecolor($width, $height);
|
|
|
80 |
|
|
|
81 |
if( ($this->image_type == IMAGETYPE_GIF) || ($this->image_type == IMAGETYPE_PNG)){
|
|
|
82 |
$transparency = imagecolortransparent($this->image);
|
|
|
83 |
if ($transparency >= 0) {
|
|
|
84 |
$transparent_color = imagecolorsforindex($this->image, $trnprt_indx);
|
|
|
85 |
$transparency = imagecolorallocate($new_image, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
|
|
|
86 |
imagefill($new_image, 0, 0, $transparency);
|
|
|
87 |
imagecolortransparent($new_image, $transparency);
|
|
|
88 |
}
|
|
|
89 |
elseif ($this->image_type == IMAGETYPE_PNG) {
|
|
|
90 |
imagealphablending($new_image, false);
|
|
|
91 |
$color = imagecolorallocatealpha($new_image, 0, 0, 0, 127);
|
|
|
92 |
imagefill($new_image, 0, 0, $color);
|
|
|
93 |
imagesavealpha($new_image, true);
|
|
|
94 |
}
|
|
|
95 |
}
|
|
|
96 |
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
|
|
|
97 |
$this->image = $new_image;
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
function resizeAuto() {
|
|
|
101 |
$width = $this->getWidth();
|
|
|
102 |
$height = $this->getheight();
|
|
|
103 |
|
|
|
104 |
if($width > $height)
|
|
|
105 |
{
|
|
|
106 |
$this->resizeToWidth(320);
|
|
|
107 |
/*$ratio=$height/$width;
|
|
|
108 |
if($ratio>0.75)
|
|
|
109 |
{
|
|
|
110 |
$this->resizeToHeight(320);
|
|
|
111 |
}
|
|
|
112 |
else
|
|
|
113 |
{
|
|
|
114 |
$this->resizeToWidth(480);
|
|
|
115 |
}*/
|
|
|
116 |
}
|
|
|
117 |
elseif($height >= $width)
|
|
|
118 |
{
|
|
|
119 |
$this->resizeToHeight(480);
|
|
|
120 |
/*$ratio=$width/$height;
|
|
|
121 |
if($ratio>0.75)
|
|
|
122 |
{
|
|
|
123 |
$this->resizeToWidth(320);
|
|
|
124 |
}
|
|
|
125 |
else
|
|
|
126 |
{
|
|
|
127 |
$this->resizeToHeight(480);
|
|
|
128 |
}*/
|
|
|
129 |
}
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
function crop($thumb_width, $thumb_height){
|
|
|
133 |
$width = $this->getWidth();
|
|
|
134 |
$height = $this->getheight();
|
|
|
135 |
|
|
|
136 |
$original_aspect = $width / $height;
|
|
|
137 |
$thumb_aspect = $thumb_width / $thumb_height;
|
|
|
138 |
|
|
|
139 |
if ( $original_aspect >= $thumb_aspect )
|
|
|
140 |
{
|
|
|
141 |
// If image is wider than thumbnail (in aspect ratio sense)
|
|
|
142 |
$new_height = $thumb_height;
|
|
|
143 |
$new_width = $width / ($height / $thumb_height);
|
|
|
144 |
}
|
|
|
145 |
else
|
|
|
146 |
{
|
|
|
147 |
// If the thumbnail is wider than the image
|
|
|
148 |
$new_width = $thumb_width;
|
|
|
149 |
$new_height = $height / ($width / $thumb_width);
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );
|
|
|
153 |
if( ($this->image_type == IMAGETYPE_GIF) || ($this->image_type == IMAGETYPE_PNG)){
|
|
|
154 |
$transparency = imagecolortransparent($this->image);
|
|
|
155 |
if ($transparency >= 0) {
|
|
|
156 |
$transparent_color = imagecolorsforindex($this->image, $trnprt_indx);
|
|
|
157 |
$transparency = imagecolorallocate($thumb, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
|
|
|
158 |
imagefill($thumb, 0, 0, $transparency);
|
|
|
159 |
imagecolortransparent($thumb, $transparency);
|
|
|
160 |
}
|
|
|
161 |
elseif ($this->image_type == IMAGETYPE_PNG) {
|
|
|
162 |
imagealphablending($thumb, false);
|
|
|
163 |
$color = imagecolorallocatealpha($thumb, 0, 0, 0, 127);
|
|
|
164 |
imagefill($thumb, 0, 0, $color);
|
|
|
165 |
imagesavealpha($thumb, true);
|
|
|
166 |
}
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
imagecopyresampled($thumb,
|
|
|
170 |
$this->image,
|
|
|
171 |
|
|
|
172 |
|
|
|
173 |
0, 0,
|
|
|
174 |
$new_width, $new_height,
|
|
|
175 |
$width, $height);
|
|
|
176 |
$this->image = $thumb;
|
|
|
177 |
}
|
|
|
178 |
}
|
|
|
179 |
?>
|