nggImage

This is a wrapper class to the C_Image_Wrapper which provides all of the necessary logic for retrieving attributes as needed.

Defined (1)

The class is defined in the following location(s).

/lib/image.php  
  1. class nggImage{ 
  2.  
  3. /**** Public variables ****/  
  4. var $errmsg = ''; // Error message to display, if any 
  5. var $error = FALSE; // Error state 
  6. var $imageURL = ''; // URL Path to the image 
  7. var $thumbURL = ''; // URL Path to the thumbnail 
  8. var $imagePath = ''; // Server Path to the image 
  9. var $thumbPath = ''; // Server Path to the thumbnail 
  10. var $href = ''; // A href link code 
  11.  
  12. // TODO: remove thumbPrefix and thumbFolder (constants) 
  13. var $thumbPrefix = 'thumbs_'; // FolderPrefix to the thumbnail 
  14. var $thumbFolder = '/thumbs/'; // Foldername to the thumbnail 
  15.  
  16. /**** Image Data ****/ 
  17. var $galleryid = 0; // Gallery ID 
  18. var $pid = 0; // Image ID  
  19. var $filename = ''; // Image filename 
  20. var $description = ''; // Image description  
  21. var $alttext = ''; // Image alttext  
  22. var $imagedate = ''; // Image date/time  
  23. var $exclude = ''; // Image exclude 
  24. var $thumbcode = ''; // Image effect code 
  25.  
  26. /**** Gallery Data ****/ 
  27. var $name = ''; // Gallery name 
  28. var $path = ''; // Gallery path  
  29. var $title = ''; // Gallery title 
  30. var $pageid = 0; // Gallery page ID 
  31. var $previewpic = 0; // Gallery preview pic  
  32.  
  33. var $permalink = ''; 
  34. var $tags = ''; 
  35.  
  36. /** 
  37. * Constructor 
  38. *  
  39. * @param object $gallery The nggGallery object representing the gallery containing this image 
  40. * @return void 
  41. */ 
  42. function nggImage($gallery) {  
  43.  
  44. //This must be an object 
  45. $gallery = (object) $gallery; 
  46.  
  47. // Build up the object 
  48. foreach ($gallery as $key => $value) 
  49. $this->$key = $value ; 
  50.  
  51. // Finish initialisation 
  52. $this->name = $gallery->name; 
  53. $this->path = $gallery->path; 
  54. $this->title = stripslashes($gallery->title); 
  55. $this->pageid = $gallery->pageid;  
  56. $this->previewpic = $gallery->previewpic; 
  57.  
  58. // set urls and paths 
  59. //20140217:this->path can't be used, because it can contain parameters 
  60. //for example: mysite/mycategory?lang=en causing improper URL format and also plugin incompatibility (Qtranslate for example. 
  61. $this->imageURL = site_url() . '/' . $this->path . '/' . $this->filename; 
  62. $this->thumbURL = site_url() . '/' . $this->path . '/thumbs/thumbs_' . $this->filename; 
  63. $this->imagePath = WINABSPATH.$this->path . '/' . $this->filename; 
  64. $this->thumbPath = WINABSPATH.$this->path . '/thumbs/thumbs_' . $this->filename; 
  65. $this->meta_data = @unserialize($this->meta_data) ; 
  66. $this->imageHTML = $this->get_href_link(); 
  67. $this->thumbHTML = $this->get_href_thumb_link(); 
  68.  
  69. do_action_ref_array('ngg_get_image', array(&$this)); 
  70.  
  71. // Note wp_cache_add will increase memory needs (4-8 kb) 
  72. //wp_cache_add($this->pid, $this, 'ngg_image'); 
  73. // Get tags only if necessary 
  74. unset($this->tags); 
  75.  
  76. /** 
  77. * Get the thumbnail code (to add effects on thumbnail click) 
  78. * Applies the filter 'ngg_get_thumbcode' 
  79. */ 
  80. function get_thumbcode($galleryname = '') { 
  81.  
  82. // clean up the name 
  83. $galleryname = sanitize_title( $galleryname ); 
  84.  
  85. // read the option setting 
  86. $ngg_options = get_option('ngg_options'); 
  87.  
  88. // get the effect code 
  89. if ($ngg_options['thumbEffect'] != "none") 
  90. $this->thumbcode = stripslashes($ngg_options['thumbCode']);  
  91.  
  92. // for highslide to a different approach  
  93. if ($ngg_options['thumbEffect'] == "highslide") { 
  94. $this->thumbcode = str_replace("%GALLERY_NAME%", "'" . $galleryname . "'", $this->thumbcode); 
  95. } else { 
  96. $this->thumbcode = str_replace("%GALLERY_NAME%", $galleryname, $this->thumbcode); 
  97.  
  98. /** 
  99. * The list of available variables: 
  100. * - %GALLERY_NAME% - The name of the gallery 
  101. * - %IMG_WIDTH% - The width of the full image. 
  102. * - %IMG_HEIGHT% - The height of the full image. 
  103. */ 
  104. $this->thumbcode = str_replace(array('%IMG_WIDTH%', '%IMG_HEIGHT%'), array($this->meta_data['width'], $this->meta_data['height']), $this->thumbcode); 
  105.  
  106. return apply_filters('ngg_get_thumbcode', $this->thumbcode, $this); 
  107.  
  108. function get_href_link() { 
  109. // create the a href link from the picture 
  110. $this->href = "\n".'<a href="'.$this->imageURL.'" title="'.htmlspecialchars( stripslashes( nggGallery::i18n($this->description, 'pic_' . $this->pid . '_description') ) ).'" '.$this->get_thumbcode($this->name).'>'."\n\t"; 
  111. $this->href .= '<img alt="'.$this->alttext.'" src="'.$this->imageURL.'"/>'."\n".'</a>'."\n"; 
  112.  
  113. return $this->href; 
  114.  
  115. function get_href_thumb_link() { 
  116. // create the a href link with the thumbanil 
  117. $this->href = "\n".'<a href="'.$this->imageURL.'" title="'.htmlspecialchars( stripslashes( nggGallery::i18n($this->description, 'pic_' . $this->pid . '_description') ) ).'" '.$this->get_thumbcode($this->name).'>'."\n\t"; 
  118. $this->href .= '<img alt="'.$this->alttext.'" src="'.$this->thumbURL.'"/>'."\n".'</a>'."\n"; 
  119.  
  120. return $this->href; 
  121.  
  122. /** 
  123. * This function creates a cache for all singlepics to reduce the CPU load 
  124. *  
  125. * @param int $width 
  126. * @param int $height 
  127. * @param string $mode could be watermark | web20 | crop 
  128. * @return the url for the image or false if failed  
  129. */ 
  130. function cached_singlepic_file($width = '', $height = '', $mode = '' ) { 
  131.  
  132. $ngg_options = get_option('ngg_options'); 
  133.  
  134. include_once( nggGallery::graphic_library() ); 
  135.  
  136. // cache filename should be unique 
  137. $cachename = $this->pid . '_' . $mode . '_'. $width . 'x' . $height . '_' . $this->filename; 
  138. $cachefolder = WINABSPATH .$ngg_options['gallerypath'] . 'cache/'; 
  139. $cached_url = site_url() . '/' . $ngg_options['gallerypath'] . 'cache/' . $cachename; 
  140. $cached_file = $cachefolder . $cachename; 
  141.  
  142. // check first for the file 
  143. if ( file_exists($cached_file) ) 
  144. return $cached_url; 
  145.  
  146. // create folder if needed 
  147. if ( !file_exists($cachefolder) ) 
  148. if ( !wp_mkdir_p($cachefolder) ) 
  149. return false; 
  150.  
  151. $thumb = new ngg_Thumbnail($this->imagePath, TRUE); 
  152. // echo $thumb->errmsg; 
  153.  
  154. if (!$thumb->error) { 
  155. if ($mode == 'crop') { 
  156. // calculates the new dimentions for a downsampled image 
  157. list ( $ratio_w, $ratio_h ) = wp_constrain_dimensions($thumb->currentDimensions['width'], $thumb->currentDimensions['height'], $width, $height); 
  158. // check ratio to decide which side should be resized 
  159. ( $ratio_h < $height || $ratio_w == $width ) ? $thumb->resize(0, $height) : $thumb->resize($width, 0); 
  160. // get the best start postion to crop from the middle  
  161. $ypos = ($thumb->currentDimensions['height'] - $height) / 2; 
  162. $thumb->crop(0, $ypos, $width, $height);  
  163. } else 
  164. $thumb->resize($width , $height); 
  165.  
  166. if ($mode == 'watermark') { 
  167. if ($ngg_options['wmType'] == 'image') { 
  168. $thumb->watermarkImgPath = $ngg_options['wmPath']; 
  169. $thumb->watermarkImage($ngg_options['wmPos'], $ngg_options['wmXpos'], $ngg_options['wmYpos']);  
  170. if ($ngg_options['wmType'] == 'text') { 
  171. $thumb->watermarkText = $ngg_options['wmText']; 
  172. $thumb->watermarkCreateText($ngg_options['wmColor'], $ngg_options['wmFont'], $ngg_options['wmSize'], $ngg_options['wmOpaque']); 
  173. $thumb->watermarkImage($ngg_options['wmPos'], $ngg_options['wmXpos'], $ngg_options['wmYpos']);  
  174.  
  175. if ($mode == 'web20') { 
  176. $thumb->createReflection(40, 40, 50, false, '#a4a4a4'); 
  177.  
  178. // save the new cache picture 
  179. $thumb->save($cached_file, $ngg_options['imgQuality']); 
  180. $thumb->destruct(); 
  181.  
  182. // check again for the file 
  183. if (file_exists($cached_file)) 
  184. return $cached_url; 
  185.  
  186. return false; 
  187.  
  188. /** 
  189. * Get the tags associated to this image 
  190. */ 
  191. function get_tags() { 
  192. if ( !isset($this->tags) ) 
  193. $this->tags = wp_get_object_terms($this->pid, 'ngg_tag', 'fields=all'); 
  194.  
  195. return $this->tags; 
  196.  
  197. /** 
  198. * Get the permalink to the image 
  199. * TODO Get a permalink to a page presenting the image 
  200. */ 
  201. function get_permalink() { 
  202. if ($this->permalink == '') 
  203. $this->permalink = $this->imageURL; 
  204.  
  205. return $this->permalink;  
  206.  
  207. function __destruct() { 
  208.