source: trunk/inc/images.inc.php

Last change on this file was 2, checked in by george, 14 years ago
  • Přidáno: Trunk revize 13719.
File size: 10.3 KB
Line 
1<?
2
3/***************************************************************************
4* Dolphin Smart Community Builder
5* -----------------
6* begin : Mon Mar 23 2006
7* copyright : (C) 2006 BoonEx Group
8* website : http://www.boonex.com/
9* This file is part of Dolphin - Smart Community Builder
10*
11* Dolphin is free software. This work is licensed under a Creative Commons Attribution 3.0 License.
12* http://creativecommons.org/licenses/by/3.0/
13*
14* Dolphin is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16* See the Creative Commons Attribution 3.0 License for more details.
17* You should have received a copy of the Creative Commons Attribution 3.0 License along with Dolphin,
18* see license.txt file; if not, write to marketing@boonex.com
19***************************************************************************/
20
21require_once( 'header.inc.php' );
22require_once( BX_DIRECTORY_PATH_INC . 'db.inc.php' );
23require_once( BX_DIRECTORY_PATH_INC . 'params.inc.php' );
24
25
26bx_import ('BxDolImageResize');
27
28$gdInstalled = extension_loaded( 'gd' );
29$use_gd = getParam( 'enable_gd' ) == 'on' ? 1 : 0;
30
31/**
32 * Resizes image given in $srcFilename to dimensions specified with $sizeX x $sizeY and
33 * saves it to $dstFilename
34 *
35 * @param string $srcFilename - source image filename
36 * @param string $dstFilename - destination image filename
37 * @param int $sizeX - width of destination image
38 * @param int $sizeY - height of destination image
39 * @param bool $forceJPGOutput - always make result in JPG format
40 *
41 * @return int - zero on success, non-zero on fail
42 *
43 *
44 * NOTE: Source image should be in GIF, JPEG or PNG format
45*/
46function imageResize( $srcFilename, $dstFilename, $sizeX, $sizeY, $forceJPGOutput = false )
47{
48 $o =& BxDolImageResize::instance($sizeX, $sizeY);
49 $o->removeCropOptions ();
50 $o->setJpegOutput ($forceJPGOutput);
51 $o->setSize ($sizeX, $sizeY);
52 if ((($sizeX == 32) && (32 == $sizeY)) || (($sizeX == 64) && (64 == $sizeY)))
53 $o->setSquareResize (true);
54 else
55 $o->setSquareResize (false);
56 return $o->resize($srcFilename, $dstFilename);
57}
58
59/**
60 * Sends PNG image header to browser and produces security image with text, specified in
61 * $text parameter
62 *
63 * @param string $text - text to output on security image
64 * @param string $hash - MD5 hash for $text parameter
65 *
66 * @return int - zero on success, non-zero on fail
67 *
68 * NOTE: Source image should be in GIF, JPEG or PNG format
69*/
70function produceSecurityImage( $text, $hash )
71{
72 global $use_gd;
73 global $CONVERT;
74 global $gdInstalled;
75 global $dir;
76
77 // constant values
78 $backgroundSizeX = 2000;
79 $backgroundSizeY = 350;
80 $sizeX = 200;
81 $sizeY = 35;
82 $fontFile = BX_DIRECTORY_PATH_ROOT . "simg/verdana.ttf";
83 $textLength = strlen( $text );
84
85 // generate random security values
86 $backgroundIndex = rand( 1, 3 );
87 $backgroundOffsetX = rand( 0, $backgroundSizeX - $sizeX - 1 );
88 $backgroundOffsetY = rand( 0, $backgroundSizeY - $sizeY - 1 );
89 $angle = rand( -3, 3 );
90 $fontColorR = rand( 0, 127 );
91 $fontColorG = rand( 0, 127 );
92 $fontColorB = rand( 0, 127 );
93 // this are library depending parameters
94 if ( $use_gd )
95 {
96 $fontSize = rand( 14, 24 );
97 $textX = rand( 0, (int)($sizeX - 0.9 * $textLength * $fontSize) ); // these coefficients are empiric
98 $textY = rand( (int)(1.25 * $fontSize), (int)($sizeY - 0.2 * $fontSize) ); // don't try to learn how they were taken out
99 }
100 else
101 {
102 $fontSize = rand( 14, 28 );
103 $textX = rand( 0, (int)($sizeX - 0.68 * $textLength * $fontSize) ); // these coefficients are empiric
104 $textY = rand( 0, (int)($sizeY - 1.4 * $fontSize) ); // don't try to learn how they were taken out
105 }
106
107 if ( $use_gd )
108 {
109 if ( !$gdInstalled )
110 return IMAGE_ERROR_GD_NOT_INSTALLED;
111 $gdInfoArray = gd_info();
112 if ( !$gdInfoArray['PNG Support'] )
113 return IMAGE_ERROR_GD_TYPE_NOT_SUPPORTED;
114
115 // create image with background
116 $src_im = imagecreatefrompng( BX_DIRECTORY_PATH_ROOT."simg/images/bg{$backgroundIndex}.png" );
117 if ( function_exists( 'imagecreatetruecolor' ) )
118 {
119 // this is more qualitative function, but it doesn't exist in old GD
120 $dst_im = imagecreatetruecolor( $sizeX, $sizeY );
121 $resizeResult = imagecopyresampled( $dst_im, $src_im, 0, 0, $backgroundOffsetX, $backgroundOffsetY,
122 $sizeX, $sizeY, $sizeX, $sizeY );
123 }
124 else
125 {
126 // this is for old GD versions
127 $dst_im = imagecreate( $sizeX, $sizeY );
128 $resizeResult = imagecopyresized( $dst_im, $src_im, 0, 0, $backgroundOffsetX, $backgroundOffsetY,
129 $sizeX, $sizeY, $sizeX, $sizeY );
130 }
131 if ( !$resizeResult )
132 return IMAGE_ERROR_GD_RESIZE_ERROR;
133
134 // write text on image
135 if ( !function_exists( 'imagettftext' ) )
136 return IMAGE_ERROR_GD_TTF_NOT_SUPPORTED;
137 $color = imagecolorallocate( $dst_im, $fontColorR, $fontColorG, $fontColorB );
138 imagettftext( $dst_im, $fontSize, -$angle, $textX, $textY, $color, $fontFile, $text );
139
140 // output header
141 header( "Content-Type: image/png" );
142
143 // output image
144 imagepng( $dst_im );
145
146 // free memory
147 imagedestroy( $src_im );
148 imagedestroy( $dst_im );
149
150 return IMAGE_ERROR_SUCCESS;
151 }
152 else
153 {
154 // create image with background
155 $workFilename = "{$dir['tmp']}bg{$hash}.png";
156 $cmd = "$CONVERT " . BX_DIRECTORY_PATH_ROOT . "simg/images/bg{$backgroundIndex}.png -crop {$sizeX}x{$sizeY}+{$backgroundOffsetX}+{$backgroundOffsetY} $workFilename";
157 @exec( $cmd );
158 if ( !file_exists( $workFilename ) )
159 return IMAGE_ERROR_IMAGEMAGICK_ERROR;
160
161 // write text on image
162 $workFilename = "{$dir['tmp']}{$hash}.png";
163 $color = '#' . sprintf( '%02x', $fontColorR ) . sprintf( '%02x', $fontColorG ) . sprintf( '%02x', $fontColorB );
164 $textX += $backgroundOffsetX;
165 $textY += $backgroundOffsetY;
166 $cmd = "$CONVERT {$dir['tmp']}bg{$hash}.png -font $fontFile -fill \"{$color}\" -pointsize $fontSize -gravity NorthWest -draw \"translate {$textX},{$textY} rotate {$angle} text 0,0 '{$text}'\" $workFilename";
167 @exec( $cmd );
168 if ( !file_exists( $workFilename ) )
169 return IMAGE_ERROR_IMAGEMAGICK_ERROR;
170
171 // output header
172 header( "Content-Type: image/png" );
173
174 // output image
175 $fp = fopen( $workFilename, 'r' );
176 $fsize = filesize( $workFilename );
177 if ( !$fp )
178 return IMAGE_ERROR_FILE_OPEN_FAILED;
179 print fread( $fp, $fsize );
180 fclose( $fp );
181
182 // unlink temporary files
183 unlink( "{$dir['tmp']}bg{$hash}.png" );
184 unlink( "{$dir['tmp']}{$hash}.png" );
185
186 return IMAGE_ERROR_SUCCESS;
187 }
188}
189
190/**
191 * Applies watermark to image given in $srcFilename with specified opacity and saves result
192 * to $dstFilename
193 *
194 * @param string $srcFilename - source image filename
195 * @param string $dstFilename - destination image filename
196 * @param string $wtrFilename - watermark filename
197 * @param int $wtrTransparency - watermark transparency (from 0 to 100)
198 *
199 * @return int - zero on success, non-zero on fail
200 *
201 *
202 * NOTE: Source image should be in GIF, JPEG or PNG format
203 * NOTE: if $wtrTransparency = 0 then no action will be done with source image
204 * but if $wtrTransparency = 100 then watermark will fully override source image
205*/
206function applyWatermark( $srcFilename, $dstFilename, $wtrFilename, $wtrTransparency )
207{
208 $o =& BxDolImageResize::instance();
209 return $o->applyWatermark ($srcFilename, $dstFilename, $wtrFilename, $wtrTransparency);
210}
211
212/**
213 * Moves and resize uploaded file
214 *
215 * @param array $_FILES - system array of uploaded files
216 * @param string $fname - name of "file" form
217 * @param string $path_and_name - path and name of new file to create
218 * @param string $maxsize - max available size (optional)
219 * @param boolean $imResize - call imageResize function immediately (optional)
220
221 *
222 * @return int in case of error and extention of new file
223 * in case of success
224 *
225 * NOTE: Source image should be in GIF, JPEG, PNG or BMP format
226*/
227function moveUploadedImage( $_FILES, $fname, $path_and_name, $maxsize='', $imResize='true' )
228{
229 global $max_photo_height;
230 global $max_photo_width;
231
232 $height = $max_photo_height;
233 if ( !$height )
234 $height = 400;
235 $width = $max_photo_width;
236 if ( !$width )
237 $width = 400;
238
239 if ( $maxsize && ($_FILES[$fname]['size'] > $maxsize || $_FILES[$fname]['size'] == 0) )
240 {
241 if ( file_exists($_FILES[$fname]['tmp_name']) )
242 {
243 unlink($_FILES[$fname]['tmp_name']);
244 }
245 return false;
246 }
247 else
248 {
249 $scan = getimagesize($_FILES[$fname]['tmp_name']);
250
251 if ( ($scan['mime'] == 'image/jpeg' && $ext = '.jpg' ) ||
252 ( $scan['mime'] == 'image/gif' && $ext = '.gif' ) ||
253 ( $scan['mime'] == 'image/png' && $ext = '.png' ) ) //deleted .bmp format
254 {
255
256 $path_and_name .= $ext;
257 move_uploaded_file( $_FILES[$fname]['tmp_name'], $path_and_name );
258
259 if ( $imResize )
260 imageResize( $path_and_name, $path_and_name, $width, $height );
261
262 }
263 else
264 {
265 return IMAGE_ERROR_WRONG_TYPE;
266 }
267 }
268
269 return $ext;
270}
271
272function ResizeAnyPromo($sFromPath, $sToPath, $iChmod = 0644) {
273 $iDefWidth = (int)getParam('promoWidth');
274 if ($iDefWidth==0) $iDefWidth = 960;
275
276 imageResize( $sFromPath, $sToPath, $iDefWidth, $iDefWidth, true );
277 chmod( $sToPath, $iChmod );
278}
279
280function ResizeAllPromos() {
281 global $dir;
282 global $site;
283
284 //cleaning old
285 $sDestFold = $dir['imagesPromo'];
286 $rDir = opendir( $sDestFold );
287 if( $rDir ) {
288 while( $sFile = readdir( $rDir ) ) {
289 if( $sFile == '.' or $sFile == '..' or !is_file( $sDestFold . $sFile ) )
290 continue;
291 unlink($sDestFold . $sFile);
292 }
293 closedir( $rDir );
294 }
295
296 //inserting new
297 $iImgCnt = 1;
298 $sDestFold = $dir['imagesPromo'] . 'original/';
299 $rDir = opendir( $sDestFold );
300 if( $rDir ) {
301 while( $sFile = readdir( $rDir ) ) {
302 if( $sFile == '.' or $sFile == '..' or !is_file( $sDestFold . $sFile ) )
303 continue;
304 ResizeAnyPromo($sDestFold . $sFile, $dir['imagesPromo'] . 'big0' . $iImgCnt . '.jpg');
305 //imageResize( $sDestFold . $sFile, $dir['imagesPromo'] . 'big0' . $iImgCnt . '.jpg', $iDefWidth, $iDefWidth, true );
306 //chmod( $dir['imagesPromo'] . 'big0' . $iImgCnt . '.jpg', 0644 );
307 $iImgCnt++;
308 }
309 closedir( $rDir );
310 }
311
312}
313
314?>
Note: See TracBrowser for help on using the repository browser.