PageRenderTime 793ms CodeModel.GetById 24ms RepoModel.GetById 3ms app.codeStats 0ms

/htdocs/wp-content/plugins/nextgen-gallery/lib/image.php

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