· 8 years ago · Dec 24, 2017, 05:10 AM
1<?php
2// *****************************************************************************
3// Purpose gets pictures by product
4// Inputs $productID - product ID
5// Remarks
6// Returns array of item
7// each item consits of
8// "photoID" - photo ID
9// "productID" - product ID
10// "filename" - conventional photo filename
11// "thumbnail" - thumbnail photo filename
12// "enlarged" - enlarged photo filename
13// "default_picture" - 1 if default picture, otherwise 0
14function GetPictures( $productID )
15{
16 $sql = "select photoID, productID, filename, thumbnail, enlarged from "
17 .PRODUCT_PICTURES." where productID = ".$productID.' ORDER BY priority';
18 $q=db_query( $sql );
19 $q2 = db_query("select default_picture from ".PRODUCTS_TABLE.
20 " where productID = ".$productID );
21 $product = db_fetch_row($q2);
22 $default_picture = $product[0];
23 $res = array();
24 while( $row = db_fetch_row($q) )
25 {
26 if ( (string)$row["photoID"] == (string)$default_picture )
27 $row["default_picture"] = 1;
28 else
29 $row["default_picture"] = 0;
30 $res[] = $row;
31 }
32 return $res;
33}
34
35/**
36 * Delete three pictures (filename, thumbnail, enlarged) for product
37 *
38 * @param int $photoID - identifier is corresponded three pictures ( see PRODUCT_PICTURES table in database_structure.xml )
39 * @return PEAR_Error | null
40 */
41function DeleteThreePictures( $photoID ){
42
43 $q=db_query("select filename, thumbnail, enlarged, productID from ".
44 PRODUCT_PICTURES." where photoID=".$photoID );
45 if ( $picture=db_fetch_row($q) )
46 {
47 if ( $picture["filename"]!="" && $picture["filename"]!=null )
48 if ( file_exists(DIR_PRODUCTS_PICTURES."/".$picture["filename"]) )
49 Functions::exec('file_remove', array(DIR_PRODUCTS_PICTURES."/".$picture["filename"]));
50
51 if ( $picture["thumbnail"]!="" && $picture["thumbnail"]!=null )
52 if ( file_exists(DIR_PRODUCTS_PICTURES."/".$picture["thumbnail"]) )
53 Functions::exec('file_remove', array(DIR_PRODUCTS_PICTURES."/".$picture["thumbnail"]));
54
55 if ( $picture["enlarged"]!="" && $picture["enlarged"]!=null )
56 if ( file_exists(DIR_PRODUCTS_PICTURES."/".$picture["enlarged"]) )
57 Functions::exec('file_remove', array(DIR_PRODUCTS_PICTURES."/".$picture["enlarged"]));
58
59 $q1 = db_query("select default_picture from ".PRODUCTS_TABLE." where productID=".$picture["productID"]);
60 if ( $product = db_fetch_row($q1) )
61 {
62 if ( $product["default_picture"] == $photoID )
63 db_query("update ".PRODUCTS_TABLE." set default_picture=NULL where productID=".$picture["productID"] );
64 }
65 db_query("delete from ".PRODUCT_PICTURES." where photoID=".$photoID );
66 }
67}
68
69 function wm($file, $width, $height, $destination_file = null,$watermark_file = null,$position = 'right', $alpha_level = 50){
70 $width = intval($width);
71 $height = intval($height);
72 if ( !function_exists('gd_info') )
73 return PEAR::raiseError('PHP extension gd not loaded', 1);
74 // return PEAR::raiseError (1, 1);
75 $src_img = $this->read($file, $info);
76 if(!$src_img)
77 return PEAR::raiseError('Error read image', 1);
78 // return PEAR::raiseError (2, 1);
79 if ( !function_exists('imagecreatetruecolor') )
80 return PEAR::raiseError('function «imagecreatetruecolor» dosn\'t exists', 1);
81 // return PEAR::raiseError (3, 1);
82 if ( !function_exists('imagecopyresized') )
83 return PEAR::raiseError('function «imagecopyresized» dosn\'t exists', 1);
84 // return PEAR::raiseError (4, 1);
85 if ( !function_exists('getimagesize') )
86 return PEAR::raiseError('function «getimagesize» dosn\'t exists', 1);
87 $src_width = imagesx($src_img);
88 if(!$width) $width = $src_width;
89 $src_height = imagesy($src_img);
90 if(!$height) $height = $src_height;
91 if ( $src_width> $src_height && $src_width> $width ){
92 $ratio = $src_width/$src_height;
93 $height /= $ratio;
94 }elseif( $src_height> $height ) {
95 $ratio = $src_height/$src_width;
96 $width /= $ratio;
97 }
98 if($src_height <$height || $src_width <$width){
99 $width = $src_width;
100 $height = $src_height;
101 }
102 /* if ($width == $src_width){//skip image resize
103 if (($file!=$destination_file)&&!copy ($file, $destination_file)){
104 //return PEAR::raiseError ('Error write image', 1);
105 }
106 return null;
107 } */
108 $dst_img = imagecreatetruecolor( $width, $height );
109 if ( !$dst_img ) {
110 @imagedestroy( $src_img );
111 return PEAR::raiseError( «Error creating true color image {$width}×{$height}», 1 );
112 // return PEAR::raiseError ( 6, 1 );
113 }
114 if ( function_exists('imagecopyresampled') )
115 $res = @imagecopyresampled ( $dst_img, $src_img, 0, 0, 0, 0, $src_width, $src_height, $src_width, $src_height );
116 else
117 $res = @imagecopyresized ( $dst_img, $src_img, 0, 0, 0, 0, $src_width, $src_height, $src_width, $src_height );
118 if ( !$res ) {
119 @imagedestroy( $srcIm );
120 @imagedestroy( $destImg );
121 return PEAR::raiseError( 'Error copy resized image', 1 );
122 // return PEAR::raiseError ( 7, 1 );
123 }
124 if(defined('CONF_PICTRESIZE_QUALITY')){
125 $quality = intval(constant('CONF_PICTRESIZE_QUALITY'));
126 $quality = ($quality>100)?100:(($quality<0)?0:$quality);
127 }else{
128 $quality = 80;
129 }
130 //Future add watermark
131 $watermark_file = DIR_IMG.'/watermark.png';
132 if($watermark_file && file_exists($watermark_file)){
133 $dst_img = $this->addWatermark($dst_img,$watermark_file,'right', 75);
134 }
135 $res = @imagejpeg( $dst_img, !is_null($destination_file)?$destination_file:$file, $quality);
136 if(!$res)
137 return PEAR::raiseError('Error write image', 1);
138 // return PEAR::raiseError (8, 1);
139 @imagedestroy( $destImg );
140 @imagedestroy( $srcIm );
141
142 }
143
144
145
146// *****************************************************************************
147// Purpose deletes main picture for product
148// Inputs $photoID - picture ID ( see PRODUCT_PICTURES table )
149// Remarks $photoID identifier is corresponded three pictures ( see PRODUCT_PICTURES
150// table in database_structure.xml ), but this function delelete only thumbnail
151// picture from server and set thumbnail column value to ''
152// Returns nothing
153function DeleteFilenamePicture( $photoID )
154{
155 $q=db_query("select filename from ".PRODUCT_PICTURES." where photoID=".
156 $photoID );
157 if ( $filename = db_fetch_row($q) )
158 {
159 if ( file_exists(DIR_PRODUCTS_PICTURES."/".$filename["filename"]) )
160 Functions::exec('file_remove', array(DIR_PRODUCTS_PICTURES."/".$filename["filename"]));
161 db_query("update ".PRODUCT_PICTURES." set filename=''".
162 " where photoID=".$photoID );
163 }
164}
165
166/**
167 * Deletes thumbnail picture for product, but this function delelete only thumbnail picture from server and set thumbnail column value to ''
168 *
169 * @param int $photoID - identifier is corresponded three pictures ( see PRODUCT_PICTURES table in database_structure.xml )
170 * @return PEAR_Error | null
171 */
172function DeleteThumbnailPicture( $photoID ){
173
174 $photoID = intval($photoID);
175
176 $q=db_query("select thumbnail from ".PRODUCT_PICTURES." where photoID=".$photoID );
177
178 if ( $thumbnail=db_fetch_row($q) ){
179
180 if ( file_exists(DIR_PRODUCTS_PICTURES."/".$thumbnail["thumbnail"]) ){
181 $res = Functions::exec('file_remove', array(DIR_PRODUCTS_PICTURES."/".$thumbnail["thumbnail"]));
182 if(PEAR::isError($res))return $res;
183 }
184
185 db_query("update ".PRODUCT_PICTURES." set thumbnail='' where photoID=".$photoID );
186 }
187}
188
189/**
190 * Delete enlarged picture for product
191 *
192 * @param int $photoID
193 * @return PEAR_Error | null
194 */
195function DeleteEnlargedPicture( $photoID ){
196
197 $photoID = intval($photoID);
198 $q=db_query("select enlarged from ".PRODUCT_PICTURES." where photoID={$photoID}" );
199 if ( $enlarged=db_fetch_row($q) ){
200
201 if ( file_exists(DIR_PRODUCTS_PICTURES."/".$enlarged["enlarged"]) ){
202
203 $res = Functions::exec('file_remove', array(DIR_PRODUCTS_PICTURES."/".$enlarged["enlarged"]));
204 if(PEAR::isError($res))return $res;
205 }
206
207 db_query("update ".PRODUCT_PICTURES." set enlarged='' where photoID={$photoID}");
208 }
209}
210
211
212// *****************************************************************************
213// Purpose updates filenames
214// Inputs $fileNames array of items
215// each item consits of
216// "filename" - normal picture
217// "thumbnail" - thumbnail picture
218// "enlarged" - enlarged picture
219// key is picture ID ( see PRODUCT_PICTURES )
220// Remarks
221// if $default_picture == -1 then default picture is not set
222// Returns nothing
223function UpdatePictures( $productID, $fileNames, $default_picture )
224{
225 foreach( $fileNames as $key => $value ){
226
227 db_phquery('UPDATE ?#PRODUCT_PICTURES SET filename=?,thumbnail=?,enlarged=? WHERE photoID=?',$value['filename'],$value['thumbnail'],$value['enlarged'],$key);
228 }
229 if ( $default_picture != -1 )db_phquery('UPDATE ?#PRODUCTS_TABLE SET default_picture=? WHERE productID=?',$default_picture,$productID);
230}
231
232
233
234// *****************************************************************************
235// Purpose adds new picture
236// Inputs $filename, $thumbnail, $enlarged - keys of item in $_FILES
237// corresponded to these file names
238// $productID - product ID
239// $default_picture - default picture ID
240// Remarks
241// if $new_filename == "" then function does not something
242// if $default_picture == -1 then default picture is set to new inserted
243// item to PRODUCT_PICTURES
244// Returns nothing
245function AddNewPictures( $productID, $filename, $thumbnail, $enlarged, $default_picture ){
246
247 if ( !trim($_FILES[$filename]["name"]) )return ;
248
249 $new_filename="";
250 $new_thumbnail="";
251 $new_enlarged="";
252
253 if ( $_FILES[$filename]["size"]!=0 && is_image($_FILES[$filename]["name"]) ){
254
255 if(PEAR::isError($res = File::checkUpload($_FILES[$filename]))||
256 PEAR::isError($res = Functions::exec('file_move_uploaded', array($_FILES[$filename]["tmp_name"], DIR_PRODUCTS_PICTURES."/".$_FILES[$filename]["name"])))
257 ){
258 return $res;
259 }
260
261 $new_filename = $_FILES[$filename]["name"];
262 SetRightsToUploadedFile( DIR_PRODUCTS_PICTURES."/".$new_filename );
263 }
264
265 if ( $_FILES[$thumbnail]["size"]!=0 && is_image($_FILES[$thumbnail]["name"])){
266
267 $res = Functions::exec('file_move_uploaded', array($_FILES[$thumbnail]["tmp_name"], DIR_PRODUCTS_PICTURES."/".$_FILES[$thumbnail]["name"]));
268 if (PEAR::isError($res))return $res;
269
270 $new_thumbnail=$_FILES[$thumbnail]["name"];
271 SetRightsToUploadedFile( DIR_PRODUCTS_PICTURES."/".$new_thumbnail );
272 }
273
274 if ( $_FILES[$enlarged]["size"]!=0 && is_image($_FILES[$enlarged]["name"])){
275
276 $res = Functions::exec('file_move_uploaded', array($_FILES[$enlarged]["tmp_name"], DIR_PRODUCTS_PICTURES."/".$_FILES[$enlarged]["name"]));
277 if (PEAR::isError($res))return $res;
278
279 $new_enlarged=$_FILES[$enlarged]["name"];
280 SetRightsToUploadedFile( DIR_PRODUCTS_PICTURES."/".$new_enlarged );
281 }
282
283 if ( $new_filename!="" ){
284
285 db_phquery("
286 INSERT ?#PRODUCT_PICTURES (productID, filename, thumbnail, enlarged)
287 VALUES( ?, ?, ?, ?)", $productID, $new_filename, $new_thumbnail, $new_enlarged);
288
289 if ( $default_picture == -1 ){
290
291 db_phquery("UPDATE ?#PRODUCTS_TABLE SET default_picture=? WHERE productID=?", db_insert_id(), $productID);
292 }
293 }
294}
295
296
297// *****************************************************************************
298// Purpose gets thumbnail file name
299// Inputs $productID - product ID
300// Remarks
301// Returns file name, it is not full path
302function GetThumbnail($productID)
303{
304 $q=db_query( "select default_picture from ".PRODUCTS_TABLE.
305 " where productID=".$productID );
306 if ( $product = db_fetch_row($q) )
307 {
308 $q2 = db_query("select filename, thumbnail, enlarged from ".PRODUCT_PICTURES.
309 " where photoID='".$product["default_picture"]."' and productID=".$productID);
310 if ( $picture=db_fetch_row($q2) )
311 {
312 if ( file_exists(DIR_PRODUCTS_PICTURES."/".$picture["thumbnail"]) && strlen($picture["thumbnail"])>0 )
313 return $picture["thumbnail"];
314 else if ( file_exists(DIR_PRODUCTS_PICTURES."/".$picture["filename"]) && strlen($picture["filename"])>0 )
315 return $picture["filename"];
316 }
317 else //default picture is not defined - get one of the pics if there are any
318 {
319
320 $q2 = db_query( "select filename, thumbnail, enlarged from ".PRODUCT_PICTURES." where productID=".$productID );
321 if ( $picture=db_fetch_row($q2) )
322 {
323 if ( file_exists(DIR_PRODUCTS_PICTURES."/".$picture["thumbnail"]) && strlen($picture["thumbnail"])>0 )
324 return $picture["thumbnail"];
325 if ( file_exists(DIR_PRODUCTS_PICTURES."/".$picture["filename"]) && strlen($picture["filename"])>0 )
326 return $picture["filename"];
327 }
328
329 }
330 }
331 return "";
332}
333
334function shrink_size($src_width, $src_height, $width, $height ){
335
336 if ( $src_width > $src_height && $src_width > $width ){
337
338 $ratio = $src_width/$src_height;
339 $height /= $ratio;
340 }elseif( $src_height > $height ) {
341
342 $ratio = $src_height/$src_width;
343 $width /= $ratio;
344 }
345 if($src_height < $height || $src_width < $width){
346 $width = $src_width;
347 $height = $src_height;
348 }
349 return array(round($width, 0), round($height, 0));
350}
351
352class ns_image{
353
354 function read($fileName, &$info){
355
356 if(!file_exists($fileName)){
357 return PEAR::raiseError('Error upload file', 1);;
358 }
359 $info = @getimagesize($fileName);
360 if (!$info) return PEAR::raiseError('Error getimagesize', 1);;
361 switch ($info[2]) {
362 case 1:
363 // Create recource from gif image
364 $srcIm = @imagecreatefromgif( $fileName );
365 break;
366 case 2:
367 // Create recource from jpg image
368 $srcIm = @imagecreatefromjpeg( $fileName );
369 break;
370 case 3:
371 // Create resource from png image
372 $srcIm = @imagecreatefrompng( $fileName );
373 break;
374 case 5:
375 // Create resource from psd image
376 break;
377 case 6:
378 // Create recource from bmp image imagecreatefromwbmp
379 $srcIm = @imagecreatefromwbmp( $fileName );
380 break;
381 case 7:
382 // Create resource from tiff image
383 break;
384 case 8:
385 // Create resource from tiff image
386 break;
387 case 9:
388 // Create resource from jpc image
389 break;
390 case 10:
391 // Create resource from jp2 image
392 break;
393 default:
394 break;
395 }
396
397 if (!$srcIm) return FALSE;
398 else return $srcIm;
399 }
400
401 function resize($file, $width, $height, $destination_file = null,$watermark_file = null,$position = 'right', $alpha_level = 50){
402 $width = intval($width);
403 $height = intval($height);
404 if ( !function_exists('gd_info') )
405 return PEAR::raiseError('PHP extension gd not loaded', 1);
406// return PEAR::raiseError(1, 1);
407
408 $src_img = $this->read($file, $info);
409 if(PEAR::isError($src_img)){
410 return $src_img;
411 }
412
413 if(!$src_img)
414 return PEAR::raiseError('Error read image', 1);
415// return PEAR::raiseError(2, 1);
416
417 if ( !function_exists('imagecreatetruecolor') )
418 return PEAR::raiseError('function "imagecreatetruecolor" dosn\'t exists', 1);
419// return PEAR::raiseError(3, 1);
420
421 if ( !function_exists('imagecopyresized') )
422 return PEAR::raiseError('function "imagecopyresized" dosn\'t exists', 1);
423// return PEAR::raiseError(4, 1);
424
425 if ( !function_exists('getimagesize') )
426 return PEAR::raiseError('function "getimagesize" dosn\'t exists', 1);
427
428 $src_width = imagesx($src_img);
429 if(!$width) $width = $src_width;
430 $src_height = imagesy($src_img);
431 if(!$height) $height = $src_height;
432 if ( $src_width > $src_height && $src_width > $width ){
433
434 $ratio = $src_width/$src_height;
435 $height /= $ratio;
436 }elseif( $src_height > $height ) {
437
438 $ratio = $src_height/$src_width;
439 $width /= $ratio;
440 }
441
442 if($src_height < $height || $src_width < $width){
443 $width = $src_width;
444 $height = $src_height;
445 }
446 if($width == $src_width){//skip image resize
447 if(($file!=$destination_file)&&!copy($file, $destination_file)){
448 return PEAR::raiseError('Error write image', 1);
449 }
450 return null;
451 }
452
453 $dst_img = imagecreatetruecolor( $width, $height );
454
455 if ( !$dst_img ) {
456 @imagedestroy( $src_img );
457 return PEAR::raiseError( "Error creating true color image {$width}×{$height}", 1 );
458// return PEAR::raiseError( 6, 1 );
459 }
460
461 if ( function_exists('imagecopyresampled') )
462 $res = @imagecopyresampled ( $dst_img, $src_img, 0, 0, 0, 0, $width, $height, $src_width, $src_height );
463 else
464 $res = @imagecopyresized ( $dst_img, $src_img, 0, 0, 0, 0, $width, $height, $src_width, $src_height );
465
466 if ( !$res ) {
467 @imagedestroy( $src_img );
468 @imagedestroy( $dst_img );
469
470 return PEAR::raiseError( 'Error copy resized image', 1 );
471// return PEAR::raiseError( 7, 1 );
472 }
473
474 if(defined('CONF_PICTRESIZE_QUALITY')){
475 $quality = intval(constant('CONF_PICTRESIZE_QUALITY'));
476 $quality = ($quality>100)?100:(($quality<0)?0:$quality);
477 }else{
478 $quality = 80;
479 }
480 //Future add watermark
481 $watermark_file = DIR_IMG.'/watermark.png';
482 if(false&&$watermark_file && file_exists($watermark_file)){
483 $dst_img = $this->addWatermark($dst_img,$watermark_file,$position, $alpha_level);
484 }
485
486 $res = @imagejpeg( $dst_img, !is_null($destination_file)?$destination_file:$file, $quality);
487 if(!$res)
488 return PEAR::raiseError('Error write image', 1);
489// return PEAR::raiseError(8, 1);
490
491 @imagedestroy( $dst_img );
492 @imagedestroy( $src_img );
493 }
494
495 function addWatermark($image,$watermark_file = null,$position = 'right', $alpha_level = 100)
496 {
497 static $watermark = false;
498 static $width;
499 static $height;
500 if(!$watermark&&$watermark_file){
501 if(!$watermark = imagecreatefrompng($watermark_file)){
502 return $image;
503 }
504 $width = imagesx($watermark);
505 $height = imagesy($watermark);
506 }
507
508 if ( $position == 'right' ) {
509 $dest_x = imagesx($image) - $width - 5;
510 $dest_y = imagesy($image) - $height - 5;
511 }else{
512 $dest_x = intval(imagesx($image)*0.5) - intval($width*0.5);
513 $dest_y = intval(imagesy($image)*0.5) - intval($height*0.5);
514 }
515 imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $width, $height, $alpha_level);
516 return $image;
517 }
518}
519
520Functions::register(new ns_image(), 'img_resize', 'resize');
521Functions::register(new ns_image(), 'img_wm', 'wm');
522?>