aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorCash Costello <cash.costello@gmail.com>2009-12-12 19:51:49 +0000
committerCash Costello <cash.costello@gmail.com>2009-12-12 19:51:49 +0000
commit621177abc2bc60e9edb3ca723a4afaa9d9e713ef (patch)
treed6064f1fe767d042ea41e4adea88e57b8e5138ff /lib
parentb40060fafaba46e2fb8800b2c2e557a8c6bde9db (diff)
downloadelgg-621177abc2bc60e9edb3ca723a4afaa9d9e713ef.tar.gz
elgg-621177abc2bc60e9edb3ca723a4afaa9d9e713ef.tar.bz2
convert line endings to Unix
Diffstat (limited to 'lib')
-rw-r--r--lib/resize.php1092
-rw-r--r--lib/tidypics.php596
2 files changed, 844 insertions, 844 deletions
diff --git a/lib/resize.php b/lib/resize.php
index 7885a52df..c35eb09ea 100644
--- a/lib/resize.php
+++ b/lib/resize.php
@@ -1,547 +1,547 @@
-<?php
- /**
- * Elgg tidypics library of resizing functions
- *
- */
-
- include dirname(__FILE__) . "/watermark.php";
-
-
- /**
- * Create thumbnails using PHP GD Library
- *
- * @param ElggFile holds the image that was uploaded
- * @param string folder to store thumbnail in
- * @param string name of the thumbnail
- * @return bool true on success
- */
- function tp_create_gd_thumbnails($file, $prefix, $filestorename)
- {
- global $CONFIG;
-
- $image_sizes = get_plugin_setting('image_sizes', 'tidypics');
- if (!$image_sizes) {
- register_error(elgg_echo('tidypics:nosettings'));
- forward($_SERVER['HTTP_REFERER']);
- return false;
- }
- $image_sizes = unserialize($image_sizes);
-
- $thumb = new ElggFile();
- $thumb->owner_guid = $file->owner_guid;
- $thumb->container_guid = $file->container_guid;
-
- // tiny thumbail
- $thumb->setFilename($prefix."thumb".$filestorename);
- $thumbname = $thumb->getFilenameOnFilestore();
- $rtn_code = tp_gd_resize( $file->getFilenameOnFilestore(),
- $thumbname,
- false,
- $image_sizes['thumb_image_width'],
- $image_sizes['thumb_image_height'],
- true);
- if (!$rtn_code)
- return false;
- $file->thumbnail = $prefix."thumb".$filestorename;
-
-
- // album thumbnail
- $thumb->setFilename($prefix."smallthumb".$filestorename);
- $thumbname = $thumb->getFilenameOnFilestore();
- $rtn_code = tp_gd_resize( $file->getFilenameOnFilestore(),
- $thumbname,
- false,
- $image_sizes['small_image_width'],
- $image_sizes['small_image_height'],
- true);
- if (!$rtn_code)
- return false;
- $file->smallthumb = $prefix."smallthumb".$filestorename;
-
-
- // main image
- $thumb->setFilename($prefix."largethumb".$filestorename);
- $thumbname = $thumb->getFilenameOnFilestore();
- $rtn_code = tp_gd_resize( $file->getFilenameOnFilestore(),
- $thumbname,
- true,
- $image_sizes['large_image_width'],
- $image_sizes['large_image_height'],
- false);
- if (!$rtn_code)
- return false;
- $file->largethumb = $prefix."largethumb".$filestorename;
-
-
- unset($thumb);
-
- return true;
- }
-
- /**
- * Writes resized version of an already uploaded image - original from Elgg filestore.php
- * Saves it in the same format as uploaded
- *
- * @param string $input_name The name of the file on the disk
- * @param string $output_name The name of the file to be written
- * @param bool - watermark this image?
- * @param int $maxwidth The maximum width of the resized image
- * @param int $maxheight The maximum height of the resized image
- * @param true|false $square If set to true, will take the smallest of maxwidth and maxheight and use it to set the dimensions on all size; the image will be cropped.
- * @return bool true on success or false on failure
- */
- function tp_gd_resize($input_name, $output_name, $watermark, $maxwidth, $maxheight, $square = false, $x1 = 0, $y1 = 0, $x2 = 0, $y2 = 0) {
-
- // Get the size information from the image
- $imgsizearray = getimagesize($input_name);
- if (!imgsizearray)
- return false;
-
- // Get width and height
- $width = $imgsizearray[0];
- $height = $imgsizearray[1];
- $newwidth = $width;
- $newheight = $height;
-
- // Square the image dimensions if we're wanting a square image
- if ($square) {
- if ($width < $height) {
- $height = $width;
- } else {
- $width = $height;
- }
-
- $newwidth = $width;
- $newheight = $height;
-
- }
-
- if ($width > $maxwidth) {
- $newheight = floor($height * ($maxwidth / $width));
- $newwidth = $maxwidth;
- }
- if ($newheight > $maxheight) {
- $newwidth = floor($newwidth * ($maxheight / $newheight));
- $newheight = $maxheight;
- }
-
- $accepted_formats = array(
- 'image/jpeg' => 'jpeg',
- 'image/pjpeg' => 'jpeg',
- 'image/png' => 'png',
- 'image/x-png' => 'png',
- 'image/gif' => 'gif'
- );
-
- // make sure the function is available
- $function = "imagecreatefrom" . $accepted_formats[$imgsizearray['mime']];
- if (!is_callable($function))
- return false;
-
-
- // load old image
- $oldimage = $function($input_name);
- if (!$oldimage)
- return false;
-
- // allocate the new image
- $newimage = imagecreatetruecolor($newwidth, $newheight);
- if (!$newimage)
- return false;
-
- // Crop the image if we need a square
- if ($square) {
- if ($x1 == 0 && $y1 == 0 && $x2 == 0 && $y2 ==0) {
- $widthoffset = floor(($imgsizearray[0] - $width) / 2);
- $heightoffset = floor(($imgsizearray[1] - $height) / 2);
- } else {
- $widthoffset = $x1;
- $heightoffset = $y1;
- $width = ($x2 - $x1);
- $height = $width;
- }
- } else {
- if ($x1 == 0 && $y1 == 0 && $x2 == 0 && $y2 ==0) {
- $widthoffset = 0;
- $heightoffset = 0;
- } else {
- $widthoffset = $x1;
- $heightoffset = $y1;
- $width = ($x2 - $x1);
- $height = ($y2 - $y1);
- }
- }
-
- if ($square) {
- $newheight = $maxheight;
- $newwidth = $maxwidth;
- }
-
- $rtn_code = imagecopyresampled($newimage, $oldimage, 0,0,$widthoffset,$heightoffset,$newwidth,$newheight,$width,$height);
- if (!rtn_code)
- return $rtn_code;
-
- if ($watermark)
- tp_gd_watermark($newimage);
-
- switch ($imgsizearray['mime']) {
- case 'image/jpeg':
- case 'image/pjpeg':
- $rtn_code = imagejpeg($newimage, $output_name, 85);
- break;
- case 'image/png':
- case 'image/x-png':
- $rtn_code = imagepng($newimage, $output_name);
- break;
- case 'image/gif':
- $rtn_code = imagegif($newimage, $output_name);
- break;
- }
-
- imagedestroy($newimage);
- imagedestroy($oldimage);
-
- return $rtn_code;
- }
-
-
- /**
- * Create thumbnails using PHP imagick extension
- *
- * @param ElggFile holds the image that was uploaded
- * @param string folder to store thumbnail in
- * @param string name of the thumbnail
- * @return bool true on success
- */
- function tp_create_imagick_thumbnails($file, $prefix, $filestorename)
- {
- $image_sizes = get_plugin_setting('image_sizes', 'tidypics');
- if (!$image_sizes) {
- register_error(elgg_echo('tidypics:nosettings'));
- return false;
- }
- $image_sizes = unserialize($image_sizes);
-
- $thumb = new ElggFile();
- $thumb->owner_guid = $file->owner_guid;
- $thumb->container_guid = $file->container_guid;
-
- // tiny thumbnail
- $thumb->setFilename($prefix."thumb".$filestorename);
- $thumbname = $thumb->getFilenameOnFilestore();
- $rtn_code = tp_imagick_resize( $file->getFilenameOnFilestore(),
- $thumbname,
- $image_sizes['thumb_image_width'],
- $image_sizes['thumb_image_height'],
- true);
- if (!$rtn_code)
- return false;
- $file->thumbnail = $prefix."thumb".$filestorename;
-
-
- // album thumbnail
- $thumb->setFilename($prefix."smallthumb".$filestorename);
- $thumbname = $thumb->getFilenameOnFilestore();
- $rtn_code = tp_imagick_resize( $file->getFilenameOnFilestore(),
- $thumbname,
- $image_sizes['small_image_width'],
- $image_sizes['small_image_height'],
- true);
- if (!$rtn_code)
- return false;
- $file->smallthumb = $prefix."smallthumb".$filestorename;
-
-
- // main image
- $thumb->setFilename($prefix."largethumb".$filestorename);
- $thumbname = $thumb->getFilenameOnFilestore();
- $rtn_code = tp_imagick_resize( $file->getFilenameOnFilestore(),
- $thumbname,
- $image_sizes['large_image_width'],
- $image_sizes['large_image_height'],
- false);
- if (!$rtn_code)
- return false;
- $file->largethumb = $prefix."largethumb".$filestorename;
-
- tp_imagick_watermark($thumbname);
-
- unset($thumb);
-
- return true;
- }
-
-
- /**
- * Resize using PHP imagick extension
- *
- * Writes resized version of an already uploaded image
- *
- *
- * @param string $input_name The name of the file input field on the submission form
- * @param string $output_name The name of the file to be written
- * @param int $maxwidth The maximum width of the resized image
- * @param int $maxheight The maximum height of the resized image
- * @param true|false $square If set to true, will take the smallest of maxwidth and maxheight and use it to set the dimensions on all size; the image will be cropped.
- * @return bool true on success
- */
- function tp_imagick_resize($input_name, $output_name, $maxwidth, $maxheight, $square = false, $x1 = 0, $y1 = 0, $x2 = 0, $y2 = 0) {
-
- // Get the size information from the image
- $imgsizearray = getimagesize($input_name);
- if (!$imgsizearray)
- return false;
-
-
- // Get width and height
- $width = $imgsizearray[0];
- $height = $imgsizearray[1];
- $newwidth = $width;
- $newheight = $height;
-
- // initial guess at final dimensions for new image (doesn't check for squareness yet
- if ($newwidth > $maxwidth) {
- $newheight = floor($newheight * ($maxwidth / $newwidth));
- $newwidth = $maxwidth;
- }
- if ($newheight > $maxheight) {
- $newwidth = floor($newwidth * ($maxheight / $newheight));
- $newheight = $maxheight;
- }
-
- // Handle squareness for both original and new image
- if ($square) {
- if ($width < $height) {
- $height = $width;
- } else {
- $width = $height;
- }
-
- if ($maxheight == $maxwidth) {
- // if input arguments = square, no need to use above calculations (which can have round-off errors)
- $newwidth = $maxwidth;
- $newheight = $maxheight;
- } else {
- if ($newwidth < $newheight) {
- $newheight = $newwidth;
- } else {
- $newwidth = $newheight;
- }
- }
- }
-
-
- // Crop the original image - this needs to be checked over
- if ($square) {
- if ($x1 == 0 && $y1 == 0 && $x2 == 0 && $y2 ==0) {
- $xoffset = floor(($imgsizearray[0] - $width) / 2);
- $yoffset = floor(($imgsizearray[1] - $height) / 2);
- } else { // assume we're being passed good croping coordinates
- $xoffset = $x1;
- $yoffset = $y1;
- $width = ($x2 - $x1);
- $height = $width;
- }
- } else {
- if ($x1 == 0 && $y1 == 0 && $x2 == 0 && $y2 ==0) {
- $xoffset = 0;
- $yoffset = 0;
- } else {
- $xoffset = $x1;
- $yoffset = $y1;
- $width = ($x2 - $x1);
- $height = ($y2 - $y1);
- }
- }
-
-
- try {
- $img = new Imagick($input_name);
- } catch (ImagickException $e) {
- return false;
- }
-
- $img->cropImage($width, $height, $xoffset, $yoffset);
-
- // use the default IM filter (windowing filter), I think 1 means default blurring or number of lobes
- $img->resizeImage($newwidth, $newheight, imagick::FILTER_LANCZOS, 1);
- $img->setImagePage($newwidth, $newheight, 0, 0);
-
- if ($img->writeImage($output_name) != true) {
- $img->destroy();
- return false;
- }
-
- $img->destroy();
-
- return true;
- }
-
- /**
- * Create thumbnails using ImageMagick executables
- *
- * @param ElggFile holds the image that was uploaded
- * @param string folder to store thumbnail in
- * @param string name of the thumbnail
- * @return bool true on success
- */
- function tp_create_im_cmdline_thumbnails($file, $prefix, $filestorename)
- {
- $image_sizes = get_plugin_setting('image_sizes', 'tidypics');
- if (!$image_sizes) {
- register_error(elgg_echo('tidypics:nosettings'));
- return false;
- }
- $image_sizes = unserialize($image_sizes);
-
- $thumb = new ElggFile();
- $thumb->owner_guid = $file->owner_guid;
- $thumb->container_guid = $file->container_guid;
-
- // tiny thumbnail
- $thumb->setFilename($prefix."thumb".$filestorename);
- $thumbname = $thumb->getFilenameOnFilestore();
- $rtn_code = tp_im_cmdline_resize( $file->getFilenameOnFilestore(),
- $thumbname,
- $image_sizes['thumb_image_width'],
- $image_sizes['thumb_image_height'],
- true);
- if (!$rtn_code)
- return false;
- $file->thumbnail = $prefix."thumb".$filestorename;
-
-
- // album thumbnail
- $thumb->setFilename($prefix."smallthumb".$filestorename);
- $thumbname = $thumb->getFilenameOnFilestore();
- $rtn_code = tp_im_cmdline_resize( $file->getFilenameOnFilestore(),
- $thumbname,
- $image_sizes['small_image_width'],
- $image_sizes['small_image_height'],
- true);
- if (!$rtn_code)
- return false;
- $file->smallthumb = $prefix."smallthumb".$filestorename;
-
-
- // main image
- $thumb->setFilename($prefix."largethumb".$filestorename);
- $thumbname = $thumb->getFilenameOnFilestore();
- $rtn_code = tp_im_cmdline_resize( $file->getFilenameOnFilestore(),
- $thumbname,
- $image_sizes['large_image_width'],
- $image_sizes['large_image_height'],
- false);
- if (!$rtn_code)
- return false;
- $file->largethumb = $prefix."largethumb".$filestorename;
-
-
- tp_im_cmdline_watermark($thumbname);
-
-
-
- unset($thumb);
-
- return true;
- }
-
- /*
- * Gets the jpeg contents of the resized version of an already uploaded image
- * (Returns false if the uploaded file was not an image)
- *
- * @param string $input_name The name of the file input field on the submission form
- * @param string $output_name The name of the file to be written
- * @param int $maxwidth The maximum width of the resized image
- * @param int $maxheight The maximum height of the resized image
- * @param true|false $square If set to true, will take the smallest of maxwidth and maxheight and use it to set the dimensions on all size; the image will be cropped.
- * @return bool
- */
- function tp_im_cmdline_resize($input_name, $output_name, $maxwidth, $maxheight, $square = false, $x1 = 0, $y1 = 0, $x2 = 0, $y2 = 0) {
-
-
- // Get the size information from the image
- if ($imgsizearray = getimagesize($input_name)) {
-
- // Get width and height
- $width = $imgsizearray[0];
- $height = $imgsizearray[1];
- $newwidth = $width;
- $newheight = $height;
-
- // Square the image dimensions if we're wanting a square image
- if ($square) {
- if ($width < $height) {
- $height = $width;
- } else {
- $width = $height;
- }
-
- $newwidth = $width;
- $newheight = $height;
-
- }
-
- if ($width > $maxwidth) {
- $newheight = floor($height * ($maxwidth / $width));
- $newwidth = $maxwidth;
- }
- if ($newheight > $maxheight) {
- $newwidth = floor($newwidth * ($maxheight / $newheight));
- $newheight = $maxheight;
- }
-
- $accepted_formats = array(
- 'image/jpeg' => 'jpeg',
- 'image/pjpeg' => 'jpeg',
- 'image/png' => 'png',
- 'image/x-png' => 'png',
- 'image/gif' => 'gif'
- );
- // If it's a file we can manipulate ...
- if (array_key_exists($imgsizearray['mime'],$accepted_formats)) {
-
- // Crop the image if we need a square
- if ($square) {
- if ($x1 == 0 && $y1 == 0 && $x2 == 0 && $y2 ==0) {
- $widthoffset = floor(($imgsizearray[0] - $width) / 2);
- $heightoffset = floor(($imgsizearray[1] - $height) / 2);
- } else {
- $widthoffset = $x1;
- $heightoffset = $y1;
- $width = ($x2 - $x1);
- $height = $width;
- }
- } else {
- if ($x1 == 0 && $y1 == 0 && $x2 == 0 && $y2 ==0) {
- $widthoffset = 0;
- $heightoffset = 0;
- } else {
- $widthoffset = $x1;
- $heightoffset = $y1;
- $width = ($x2 - $x1);
- $height = ($y2 - $y1);
- }
- }
-
- // Resize and return the image contents!
- if ($square) {
- $newheight = $maxheight;
- $newwidth = $maxwidth;
- }
- $im_path = get_plugin_setting('im_path', 'tidypics');
- if(!$im_path) {
- $im_path = "/usr/bin/";
- }
- if(substr($im_path, strlen($im_path)-1, 1) != "/") $im_path .= "/";
- $command = $im_path . "convert \"$input_name\" -resize ".$newwidth."x".$newheight."^ -gravity center -extent ".$newwidth."x".$newheight." \"$output_name\"";
- exec($command);
- return true;
-
- }
- }
-
- return false;
- }
-
+<?php
+ /**
+ * Elgg tidypics library of resizing functions
+ *
+ */
+
+ include dirname(__FILE__) . "/watermark.php";
+
+
+ /**
+ * Create thumbnails using PHP GD Library
+ *
+ * @param ElggFile holds the image that was uploaded
+ * @param string folder to store thumbnail in
+ * @param string name of the thumbnail
+ * @return bool true on success
+ */
+ function tp_create_gd_thumbnails($file, $prefix, $filestorename)
+ {
+ global $CONFIG;
+
+ $image_sizes = get_plugin_setting('image_sizes', 'tidypics');
+ if (!$image_sizes) {
+ register_error(elgg_echo('tidypics:nosettings'));
+ forward($_SERVER['HTTP_REFERER']);
+ return false;
+ }
+ $image_sizes = unserialize($image_sizes);
+
+ $thumb = new ElggFile();
+ $thumb->owner_guid = $file->owner_guid;
+ $thumb->container_guid = $file->container_guid;
+
+ // tiny thumbail
+ $thumb->setFilename($prefix."thumb".$filestorename);
+ $thumbname = $thumb->getFilenameOnFilestore();
+ $rtn_code = tp_gd_resize( $file->getFilenameOnFilestore(),
+ $thumbname,
+ false,
+ $image_sizes['thumb_image_width'],
+ $image_sizes['thumb_image_height'],
+ true);
+ if (!$rtn_code)
+ return false;
+ $file->thumbnail = $prefix."thumb".$filestorename;
+
+
+ // album thumbnail
+ $thumb->setFilename($prefix."smallthumb".$filestorename);
+ $thumbname = $thumb->getFilenameOnFilestore();
+ $rtn_code = tp_gd_resize( $file->getFilenameOnFilestore(),
+ $thumbname,
+ false,
+ $image_sizes['small_image_width'],
+ $image_sizes['small_image_height'],
+ true);
+ if (!$rtn_code)
+ return false;
+ $file->smallthumb = $prefix."smallthumb".$filestorename;
+
+
+ // main image
+ $thumb->setFilename($prefix."largethumb".$filestorename);
+ $thumbname = $thumb->getFilenameOnFilestore();
+ $rtn_code = tp_gd_resize( $file->getFilenameOnFilestore(),
+ $thumbname,
+ true,
+ $image_sizes['large_image_width'],
+ $image_sizes['large_image_height'],
+ false);
+ if (!$rtn_code)
+ return false;
+ $file->largethumb = $prefix."largethumb".$filestorename;
+
+
+ unset($thumb);
+
+ return true;
+ }
+
+ /**
+ * Writes resized version of an already uploaded image - original from Elgg filestore.php
+ * Saves it in the same format as uploaded
+ *
+ * @param string $input_name The name of the file on the disk
+ * @param string $output_name The name of the file to be written
+ * @param bool - watermark this image?
+ * @param int $maxwidth The maximum width of the resized image
+ * @param int $maxheight The maximum height of the resized image
+ * @param true|false $square If set to true, will take the smallest of maxwidth and maxheight and use it to set the dimensions on all size; the image will be cropped.
+ * @return bool true on success or false on failure
+ */
+ function tp_gd_resize($input_name, $output_name, $watermark, $maxwidth, $maxheight, $square = false, $x1 = 0, $y1 = 0, $x2 = 0, $y2 = 0) {
+
+ // Get the size information from the image
+ $imgsizearray = getimagesize($input_name);
+ if (!imgsizearray)
+ return false;
+
+ // Get width and height
+ $width = $imgsizearray[0];
+ $height = $imgsizearray[1];
+ $newwidth = $width;
+ $newheight = $height;
+
+ // Square the image dimensions if we're wanting a square image
+ if ($square) {
+ if ($width < $height) {
+ $height = $width;
+ } else {
+ $width = $height;
+ }
+
+ $newwidth = $width;
+ $newheight = $height;
+
+ }
+
+ if ($width > $maxwidth) {
+ $newheight = floor($height * ($maxwidth / $width));
+ $newwidth = $maxwidth;
+ }
+ if ($newheight > $maxheight) {
+ $newwidth = floor($newwidth * ($maxheight / $newheight));
+ $newheight = $maxheight;
+ }
+
+ $accepted_formats = array(
+ 'image/jpeg' => 'jpeg',
+ 'image/pjpeg' => 'jpeg',
+ 'image/png' => 'png',
+ 'image/x-png' => 'png',
+ 'image/gif' => 'gif'
+ );
+
+ // make sure the function is available
+ $function = "imagecreatefrom" . $accepted_formats[$imgsizearray['mime']];
+ if (!is_callable($function))
+ return false;
+
+
+ // load old image
+ $oldimage = $function($input_name);
+ if (!$oldimage)
+ return false;
+
+ // allocate the new image
+ $newimage = imagecreatetruecolor($newwidth, $newheight);
+ if (!$newimage)
+ return false;
+
+ // Crop the image if we need a square
+ if ($square) {
+ if ($x1 == 0 && $y1 == 0 && $x2 == 0 && $y2 ==0) {
+ $widthoffset = floor(($imgsizearray[0] - $width) / 2);
+ $heightoffset = floor(($imgsizearray[1] - $height) / 2);
+ } else {
+ $widthoffset = $x1;
+ $heightoffset = $y1;
+ $width = ($x2 - $x1);
+ $height = $width;
+ }
+ } else {
+ if ($x1 == 0 && $y1 == 0 && $x2 == 0 && $y2 ==0) {
+ $widthoffset = 0;
+ $heightoffset = 0;
+ } else {
+ $widthoffset = $x1;
+ $heightoffset = $y1;
+ $width = ($x2 - $x1);
+ $height = ($y2 - $y1);
+ }
+ }
+
+ if ($square) {
+ $newheight = $maxheight;
+ $newwidth = $maxwidth;
+ }
+
+ $rtn_code = imagecopyresampled($newimage, $oldimage, 0,0,$widthoffset,$heightoffset,$newwidth,$newheight,$width,$height);
+ if (!rtn_code)
+ return $rtn_code;
+
+ if ($watermark)
+ tp_gd_watermark($newimage);
+
+ switch ($imgsizearray['mime']) {
+ case 'image/jpeg':
+ case 'image/pjpeg':
+ $rtn_code = imagejpeg($newimage, $output_name, 85);
+ break;
+ case 'image/png':
+ case 'image/x-png':
+ $rtn_code = imagepng($newimage, $output_name);
+ break;
+ case 'image/gif':
+ $rtn_code = imagegif($newimage, $output_name);
+ break;
+ }
+
+ imagedestroy($newimage);
+ imagedestroy($oldimage);
+
+ return $rtn_code;
+ }
+
+
+ /**
+ * Create thumbnails using PHP imagick extension
+ *
+ * @param ElggFile holds the image that was uploaded
+ * @param string folder to store thumbnail in
+ * @param string name of the thumbnail
+ * @return bool true on success
+ */
+ function tp_create_imagick_thumbnails($file, $prefix, $filestorename)
+ {
+ $image_sizes = get_plugin_setting('image_sizes', 'tidypics');
+ if (!$image_sizes) {
+ register_error(elgg_echo('tidypics:nosettings'));
+ return false;
+ }
+ $image_sizes = unserialize($image_sizes);
+
+ $thumb = new ElggFile();
+ $thumb->owner_guid = $file->owner_guid;
+ $thumb->container_guid = $file->container_guid;
+
+ // tiny thumbnail
+ $thumb->setFilename($prefix."thumb".$filestorename);
+ $thumbname = $thumb->getFilenameOnFilestore();
+ $rtn_code = tp_imagick_resize( $file->getFilenameOnFilestore(),
+ $thumbname,
+ $image_sizes['thumb_image_width'],
+ $image_sizes['thumb_image_height'],
+ true);
+ if (!$rtn_code)
+ return false;
+ $file->thumbnail = $prefix."thumb".$filestorename;
+
+
+ // album thumbnail
+ $thumb->setFilename($prefix."smallthumb".$filestorename);
+ $thumbname = $thumb->getFilenameOnFilestore();
+ $rtn_code = tp_imagick_resize( $file->getFilenameOnFilestore(),
+ $thumbname,
+ $image_sizes['small_image_width'],
+ $image_sizes['small_image_height'],
+ true);
+ if (!$rtn_code)
+ return false;
+ $file->smallthumb = $prefix."smallthumb".$filestorename;
+
+
+ // main image
+ $thumb->setFilename($prefix."largethumb".$filestorename);
+ $thumbname = $thumb->getFilenameOnFilestore();
+ $rtn_code = tp_imagick_resize( $file->getFilenameOnFilestore(),
+ $thumbname,
+ $image_sizes['large_image_width'],
+ $image_sizes['large_image_height'],
+ false);
+ if (!$rtn_code)
+ return false;
+ $file->largethumb = $prefix."largethumb".$filestorename;
+
+ tp_imagick_watermark($thumbname);
+
+ unset($thumb);
+
+ return true;
+ }
+
+
+ /**
+ * Resize using PHP imagick extension
+ *
+ * Writes resized version of an already uploaded image
+ *
+ *
+ * @param string $input_name The name of the file input field on the submission form
+ * @param string $output_name The name of the file to be written
+ * @param int $maxwidth The maximum width of the resized image
+ * @param int $maxheight The maximum height of the resized image
+ * @param true|false $square If set to true, will take the smallest of maxwidth and maxheight and use it to set the dimensions on all size; the image will be cropped.
+ * @return bool true on success
+ */
+ function tp_imagick_resize($input_name, $output_name, $maxwidth, $maxheight, $square = false, $x1 = 0, $y1 = 0, $x2 = 0, $y2 = 0) {
+
+ // Get the size information from the image
+ $imgsizearray = getimagesize($input_name);
+ if (!$imgsizearray)
+ return false;
+
+
+ // Get width and height
+ $width = $imgsizearray[0];
+ $height = $imgsizearray[1];
+ $newwidth = $width;
+ $newheight = $height;
+
+ // initial guess at final dimensions for new image (doesn't check for squareness yet
+ if ($newwidth > $maxwidth) {
+ $newheight = floor($newheight * ($maxwidth / $newwidth));
+ $newwidth = $maxwidth;
+ }
+ if ($newheight > $maxheight) {
+ $newwidth = floor($newwidth * ($maxheight / $newheight));
+ $newheight = $maxheight;
+ }
+
+ // Handle squareness for both original and new image
+ if ($square) {
+ if ($width < $height) {
+ $height = $width;
+ } else {
+ $width = $height;
+ }
+
+ if ($maxheight == $maxwidth) {
+ // if input arguments = square, no need to use above calculations (which can have round-off errors)
+ $newwidth = $maxwidth;
+ $newheight = $maxheight;
+ } else {
+ if ($newwidth < $newheight) {
+ $newheight = $newwidth;
+ } else {
+ $newwidth = $newheight;
+ }
+ }
+ }
+
+
+ // Crop the original image - this needs to be checked over
+ if ($square) {
+ if ($x1 == 0 && $y1 == 0 && $x2 == 0 && $y2 ==0) {
+ $xoffset = floor(($imgsizearray[0] - $width) / 2);
+ $yoffset = floor(($imgsizearray[1] - $height) / 2);
+ } else { // assume we're being passed good croping coordinates
+ $xoffset = $x1;
+ $yoffset = $y1;
+ $width = ($x2 - $x1);
+ $height = $width;
+ }
+ } else {
+ if ($x1 == 0 && $y1 == 0 && $x2 == 0 && $y2 ==0) {
+ $xoffset = 0;
+ $yoffset = 0;
+ } else {
+ $xoffset = $x1;
+ $yoffset = $y1;
+ $width = ($x2 - $x1);
+ $height = ($y2 - $y1);
+ }
+ }
+
+
+ try {
+ $img = new Imagick($input_name);
+ } catch (ImagickException $e) {
+ return false;
+ }
+
+ $img->cropImage($width, $height, $xoffset, $yoffset);
+
+ // use the default IM filter (windowing filter), I think 1 means default blurring or number of lobes
+ $img->resizeImage($newwidth, $newheight, imagick::FILTER_LANCZOS, 1);
+ $img->setImagePage($newwidth, $newheight, 0, 0);
+
+ if ($img->writeImage($output_name) != true) {
+ $img->destroy();
+ return false;
+ }
+
+ $img->destroy();
+
+ return true;
+ }
+
+ /**
+ * Create thumbnails using ImageMagick executables
+ *
+ * @param ElggFile holds the image that was uploaded
+ * @param string folder to store thumbnail in
+ * @param string name of the thumbnail
+ * @return bool true on success
+ */
+ function tp_create_im_cmdline_thumbnails($file, $prefix, $filestorename)
+ {
+ $image_sizes = get_plugin_setting('image_sizes', 'tidypics');
+ if (!$image_sizes) {
+ register_error(elgg_echo('tidypics:nosettings'));
+ return false;
+ }
+ $image_sizes = unserialize($image_sizes);
+
+ $thumb = new ElggFile();
+ $thumb->owner_guid = $file->owner_guid;
+ $thumb->container_guid = $file->container_guid;
+
+ // tiny thumbnail
+ $thumb->setFilename($prefix."thumb".$filestorename);
+ $thumbname = $thumb->getFilenameOnFilestore();
+ $rtn_code = tp_im_cmdline_resize( $file->getFilenameOnFilestore(),
+ $thumbname,
+ $image_sizes['thumb_image_width'],
+ $image_sizes['thumb_image_height'],
+ true);
+ if (!$rtn_code)
+ return false;
+ $file->thumbnail = $prefix."thumb".$filestorename;
+
+
+ // album thumbnail
+ $thumb->setFilename($prefix."smallthumb".$filestorename);
+ $thumbname = $thumb->getFilenameOnFilestore();
+ $rtn_code = tp_im_cmdline_resize( $file->getFilenameOnFilestore(),
+ $thumbname,
+ $image_sizes['small_image_width'],
+ $image_sizes['small_image_height'],
+ true);
+ if (!$rtn_code)
+ return false;
+ $file->smallthumb = $prefix."smallthumb".$filestorename;
+
+
+ // main image
+ $thumb->setFilename($prefix."largethumb".$filestorename);
+ $thumbname = $thumb->getFilenameOnFilestore();
+ $rtn_code = tp_im_cmdline_resize( $file->getFilenameOnFilestore(),
+ $thumbname,
+ $image_sizes['large_image_width'],
+ $image_sizes['large_image_height'],
+ false);
+ if (!$rtn_code)
+ return false;
+ $file->largethumb = $prefix."largethumb".$filestorename;
+
+
+ tp_im_cmdline_watermark($thumbname);
+
+
+
+ unset($thumb);
+
+ return true;
+ }
+
+ /*
+ * Gets the jpeg contents of the resized version of an already uploaded image
+ * (Returns false if the uploaded file was not an image)
+ *
+ * @param string $input_name The name of the file input field on the submission form
+ * @param string $output_name The name of the file to be written
+ * @param int $maxwidth The maximum width of the resized image
+ * @param int $maxheight The maximum height of the resized image
+ * @param true|false $square If set to true, will take the smallest of maxwidth and maxheight and use it to set the dimensions on all size; the image will be cropped.
+ * @return bool
+ */
+ function tp_im_cmdline_resize($input_name, $output_name, $maxwidth, $maxheight, $square = false, $x1 = 0, $y1 = 0, $x2 = 0, $y2 = 0) {
+
+
+ // Get the size information from the image
+ if ($imgsizearray = getimagesize($input_name)) {
+
+ // Get width and height
+ $width = $imgsizearray[0];
+ $height = $imgsizearray[1];
+ $newwidth = $width;
+ $newheight = $height;
+
+ // Square the image dimensions if we're wanting a square image
+ if ($square) {
+ if ($width < $height) {
+ $height = $width;
+ } else {
+ $width = $height;
+ }
+
+ $newwidth = $width;
+ $newheight = $height;
+
+ }
+
+ if ($width > $maxwidth) {
+ $newheight = floor($height * ($maxwidth / $width));
+ $newwidth = $maxwidth;
+ }
+ if ($newheight > $maxheight) {
+ $newwidth = floor($newwidth * ($maxheight / $newheight));
+ $newheight = $maxheight;
+ }
+
+ $accepted_formats = array(
+ 'image/jpeg' => 'jpeg',
+ 'image/pjpeg' => 'jpeg',
+ 'image/png' => 'png',
+ 'image/x-png' => 'png',
+ 'image/gif' => 'gif'
+ );
+ // If it's a file we can manipulate ...
+ if (array_key_exists($imgsizearray['mime'],$accepted_formats)) {
+
+ // Crop the image if we need a square
+ if ($square) {
+ if ($x1 == 0 && $y1 == 0 && $x2 == 0 && $y2 ==0) {
+ $widthoffset = floor(($imgsizearray[0] - $width) / 2);
+ $heightoffset = floor(($imgsizearray[1] - $height) / 2);
+ } else {
+ $widthoffset = $x1;
+ $heightoffset = $y1;
+ $width = ($x2 - $x1);
+ $height = $width;
+ }
+ } else {
+ if ($x1 == 0 && $y1 == 0 && $x2 == 0 && $y2 ==0) {
+ $widthoffset = 0;
+ $heightoffset = 0;
+ } else {
+ $widthoffset = $x1;
+ $heightoffset = $y1;
+ $width = ($x2 - $x1);
+ $height = ($y2 - $y1);
+ }
+ }
+
+ // Resize and return the image contents!
+ if ($square) {
+ $newheight = $maxheight;
+ $newwidth = $maxwidth;
+ }
+ $im_path = get_plugin_setting('im_path', 'tidypics');
+ if(!$im_path) {
+ $im_path = "/usr/bin/";
+ }
+ if(substr($im_path, strlen($im_path)-1, 1) != "/") $im_path .= "/";
+ $command = $im_path . "convert \"$input_name\" -resize ".$newwidth."x".$newheight."^ -gravity center -extent ".$newwidth."x".$newheight." \"$output_name\"";
+ exec($command);
+ return true;
+
+ }
+ }
+
+ return false;
+ }
+
?> \ No newline at end of file
diff --git a/lib/tidypics.php b/lib/tidypics.php
index e24e4d77e..b0a8a70d2 100644
--- a/lib/tidypics.php
+++ b/lib/tidypics.php
@@ -1,299 +1,299 @@
-<?php
- /**
- * Elgg tidypics library of common functions
- *
- */
-
- /**
- * Get images for display on front page
- *
- * @param int number of images
- * @param int (optional) guid of owner
- * @return string of html for display
- *
- * To use with the custom index plugin, use something like this:
-
- if (is_plugin_enabled('tidypics')) {
-?>
- <!-- display latest photos -->
- <div class="index_box">
- <h2><a href="<?php echo $vars['url']; ?>pg/photos/world/"><?php echo elgg_echo("tidypics:mostrecent"); ?></a></h2>
- <div class="contentWrapper">
- <?php
- echo tp_get_latest_photos(5);
- ?>
- </div>
- </div>
-<?php
- }
-?>
-
- * Good luck
- */
- function tp_get_latest_photos($num_images, $owner_guid = 0)
- {
- $prev_context = set_context('front');
- $image_html = tp_list_entities('object', 'image', $owner_guid, $num_images, false, false, false);
- set_context($prev_context);
- return $image_html;
- }
-
-
- /**
- * Get image directory path
- *
- * Each album gets a subdirectory based on its container id
- *
- * @return string path to image directory
- */
- function tp_get_img_dir()
- {
- $file = new ElggFile();
- return $file->getFilenameOnFilestore() . 'image/';
- }
-
-
-
- /*********************************************************************
- * the functions below replace broken core functions or add functions
- * that could/should exist in the core
- */
-
- /**
- *
- */
- function tp_get_entities($type = "", $subtype = "", $owner_guid = 0, $order_by = "", $limit = 10, $offset = 0, $count = false, $site_guid = 0, $container_guid = null, $timelower = 0, $timeupper = 0)
- {
- global $CONFIG;
-
- if ($subtype === false || $subtype === null || $subtype === 0)
- return false;
-
- if ($order_by == "") $order_by = "time_created desc";
- $order_by = sanitise_string($order_by);
- $limit = (int)$limit;
- $offset = (int)$offset;
- $site_guid = (int) $site_guid;
- $timelower = (int) $timelower;
- $timeupper = (int) $timeupper;
- if ($site_guid == 0)
- $site_guid = $CONFIG->site_guid;
-
- $where = array();
-
- if (is_array($subtype)) {
- $tempwhere = "";
- if (sizeof($subtype))
- foreach($subtype as $typekey => $subtypearray) {
- foreach($subtypearray as $subtypeval) {
- $typekey = sanitise_string($typekey);
- if (!empty($subtypeval)) {
- if (!$subtypeval = (int) get_subtype_id($typekey, $subtypeval))
- return false;
- } else {
- // @todo: Setting subtype to 0 when $subtype = '' returns entities with
- // no subtype. This is different to the non-array behavior
- // but may be required in some cases.
- $subtypeval = 0;
- }
- if (!empty($tempwhere)) $tempwhere .= " or ";
- $tempwhere .= "(type = '{$typekey}' and subtype = {$subtypeval})";
- }
- }
- if (!empty($tempwhere)) $where[] = "({$tempwhere})";
-
- } else {
-
- $type = sanitise_string($type);
- if ($subtype !== "" AND !$subtype = get_subtype_id($type, $subtype))
- return false;
-
- if ($type != "")
- $where[] = "type='$type'";
- if ($subtype!=="")
- $where[] = "subtype=$subtype";
- }
-
- if ($owner_guid != "") {
- if (!is_array($owner_guid)) {
- $owner_array = array($owner_guid);
- $owner_guid = (int) $owner_guid;
- $where[] = "owner_guid = '$owner_guid'";
- } else if (sizeof($owner_guid) > 0) {
- $owner_array = array_map('sanitise_int', $owner_guid);
- // Cast every element to the owner_guid array to int
- $owner_guid = array_map("sanitise_int", $owner_guid);
- $owner_guid = implode(",",$owner_guid);
- $where[] = "owner_guid in ({$owner_guid})";
- }
- }
- if ($site_guid > 0)
- $where[] = "site_guid = {$site_guid}";
-
- if (!is_null($container_guid)) {
- if (is_array($container_guid)) {
- foreach($container_guid as $key => $val) $container_guid[$key] = (int) $val;
- $where[] = "container_guid in (" . implode(",",$container_guid) . ")";
- } else {
- $container_guid = (int) $container_guid;
- $where[] = "container_guid = {$container_guid}";
- }
- }
- if ($timelower)
- $where[] = "time_created >= {$timelower}";
- if ($timeupper)
- $where[] = "time_created <= {$timeupper}";
-
- if (!$count) {
- $query = "SELECT * from {$CONFIG->dbprefix}entities where ";
- } else {
- $query = "SELECT count(guid) as total from {$CONFIG->dbprefix}entities where ";
- }
- foreach ($where as $w)
- $query .= " $w and ";
- $query .= get_access_sql_suffix(); // Add access controls
- if (!$count) {
- $query .= " order by $order_by";
- if ($limit) $query .= " limit $offset, $limit"; // Add order and limit
- $dt = get_data($query, "entity_row_to_elggstar");
- return $dt;
- } else {
- $total = get_data_row($query);
- return $total->total;
- }
- }
-
- function tp_list_entities($type= "", $subtype = "", $owner_guid = 0, $limit = 10, $fullview = true, $viewtypetoggle = false, $pagination = true) {
-
- $offset = (int) get_input('offset');
- $count = tp_get_entities($type, $subtype, $owner_guid, "", $limit, $offset, true);
- $entities = tp_get_entities($type, $subtype, $owner_guid, "", $limit, $offset);
-
- return tp_view_entity_list($entities, $count, $offset, $limit, $fullview, $viewtypetoggle, $pagination);
- }
-
- function tp_view_entity_list($entities, $count, $offset, $limit, $fullview = true, $viewtypetoggle = false, $pagination = true) {
- $context = get_context();
-
- $html = elgg_view('tidypics/gallery',array(
- 'entities' => $entities,
- 'count' => $count,
- 'offset' => $offset,
- 'limit' => $limit,
- 'baseurl' => $_SERVER['REQUEST_URI'],
- 'fullview' => $fullview,
- 'context' => $context,
- 'viewtypetoggle' => $viewtypetoggle,
- 'viewtype' => get_input('search_viewtype','list'),
- 'pagination' => $pagination
- ));
-
- return $html;
- }
-
- function tp_get_entities_from_annotations_calculate_x($sum = "sum", $entity_type = "", $entity_subtype = "", $name = "", $mdname = '', $mdvalue = '', $owner_guid = 0, $limit = 10, $offset = 0, $orderdir = 'desc', $count = false)
- {
- global $CONFIG;
-
- $sum = sanitise_string($sum);
- $entity_type = sanitise_string($entity_type);
- $entity_subtype = get_subtype_id($entity_type, $entity_subtype);
- $name = get_metastring_id($name);
- $limit = (int) $limit;
- $offset = (int) $offset;
- $owner_guid = (int) $owner_guid;
- if (!empty($mdname) && !empty($mdvalue)) {
- $meta_n = get_metastring_id($mdname);
- $meta_v = get_metastring_id($mdvalue);
- }
-
- if (empty($name)) return 0;
-
- $where = array();
-
- if ($entity_type!="")
- $where[] = "e.type='$entity_type'";
- if ($owner_guid > 0)
- $where[] = "e.owner_guid = $owner_guid";
- if ($entity_subtype)
- $where[] = "e.subtype=$entity_subtype";
- if ($name!="")
- $where[] = "a.name_id='$name'";
-
- if (!empty($mdname) && !empty($mdvalue)) {
- if ($mdname!="")
- $where[] = "m.name_id='$meta_n'";
- if ($mdvalue!="")
- $where[] = "m.value_id='$meta_v'";
- }
-
- if ($sum != "count")
- $where[] = "a.value_type='integer'"; // Limit on integer types
-
- if (!$count) {
- $query = "SELECT distinct e.*, $sum(ms.string) as sum ";
- } else {
- $query = "SELECT count(distinct e.guid) as num, $sum(ms.string) as sum ";
- }
- $query .= " from {$CONFIG->dbprefix}entities e JOIN {$CONFIG->dbprefix}annotations a on a.entity_guid = e.guid JOIN {$CONFIG->dbprefix}metastrings ms on a.value_id=ms.id ";
-
- if (!empty($mdname) && !empty($mdvalue)) {
- $query .= " JOIN {$CONFIG->dbprefix}metadata m on m.entity_guid = e.guid ";
- }
-
- $query .= " WHERE ";
- foreach ($where as $w)
- $query .= " $w and ";
- $query .= get_access_sql_suffix("a"); // now add access
- $query .= ' and ' . get_access_sql_suffix("e"); // now add access
- if (!$count) $query .= ' group by e.guid';
-
- if (!$count) {
- $query .= ' order by sum ' . $orderdir;
- $query .= ' limit ' . $offset . ' , ' . $limit;
- return get_data($query, "entity_row_to_elggstar");
- } else {
- if ($row = get_data_row($query)) {
- return $row->num;
- }
- }
- return false;
- }
-
- /**
- * Is page owner a group - convenience function
- *
- * @return true/false
- */
- function tp_is_group_page() {
-
- if ($group = page_owner_entity()) {
- if ($group instanceof ElggGroup)
- return true;
- }
-
- return false;
- }
-
-
- /**
- * Is the request from a known browser
- *
- * @return true/false
- */
- function tp_is_person()
- {
- $known = array('msie', 'mozilla', 'firefox', 'safari', 'webkit', 'opera', 'netscape', 'konqueror', 'gecko');
-
- $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
-
- foreach ($known as $browser)
- {
- if (strpos($agent, $browser) !== false) {
- return true;
- }
- }
-
- return false;
- }
+<?php
+ /**
+ * Elgg tidypics library of common functions
+ *
+ */
+
+ /**
+ * Get images for display on front page
+ *
+ * @param int number of images
+ * @param int (optional) guid of owner
+ * @return string of html for display
+ *
+ * To use with the custom index plugin, use something like this:
+
+ if (is_plugin_enabled('tidypics')) {
+?>
+ <!-- display latest photos -->
+ <div class="index_box">
+ <h2><a href="<?php echo $vars['url']; ?>pg/photos/world/"><?php echo elgg_echo("tidypics:mostrecent"); ?></a></h2>
+ <div class="contentWrapper">
+ <?php
+ echo tp_get_latest_photos(5);
+ ?>
+ </div>
+ </div>
+<?php
+ }
+?>
+
+ * Good luck
+ */
+ function tp_get_latest_photos($num_images, $owner_guid = 0)
+ {
+ $prev_context = set_context('front');
+ $image_html = tp_list_entities('object', 'image', $owner_guid, $num_images, false, false, false);
+ set_context($prev_context);
+ return $image_html;
+ }
+
+
+ /**
+ * Get image directory path
+ *
+ * Each album gets a subdirectory based on its container id
+ *
+ * @return string path to image directory
+ */
+ function tp_get_img_dir()
+ {
+ $file = new ElggFile();
+ return $file->getFilenameOnFilestore() . 'image/';
+ }
+
+
+
+ /*********************************************************************
+ * the functions below replace broken core functions or add functions
+ * that could/should exist in the core
+ */
+
+ /**
+ *
+ */
+ function tp_get_entities($type = "", $subtype = "", $owner_guid = 0, $order_by = "", $limit = 10, $offset = 0, $count = false, $site_guid = 0, $container_guid = null, $timelower = 0, $timeupper = 0)
+ {
+ global $CONFIG;
+
+ if ($subtype === false || $subtype === null || $subtype === 0)
+ return false;
+
+ if ($order_by == "") $order_by = "time_created desc";
+ $order_by = sanitise_string($order_by);
+ $limit = (int)$limit;
+ $offset = (int)$offset;
+ $site_guid = (int) $site_guid;
+ $timelower = (int) $timelower;
+ $timeupper = (int) $timeupper;
+ if ($site_guid == 0)
+ $site_guid = $CONFIG->site_guid;
+
+ $where = array();
+
+ if (is_array($subtype)) {
+ $tempwhere = "";
+ if (sizeof($subtype))
+ foreach($subtype as $typekey => $subtypearray) {
+ foreach($subtypearray as $subtypeval) {
+ $typekey = sanitise_string($typekey);
+ if (!empty($subtypeval)) {
+ if (!$subtypeval = (int) get_subtype_id($typekey, $subtypeval))
+ return false;
+ } else {
+ // @todo: Setting subtype to 0 when $subtype = '' returns entities with
+ // no subtype. This is different to the non-array behavior
+ // but may be required in some cases.
+ $subtypeval = 0;
+ }
+ if (!empty($tempwhere)) $tempwhere .= " or ";
+ $tempwhere .= "(type = '{$typekey}' and subtype = {$subtypeval})";
+ }
+ }
+ if (!empty($tempwhere)) $where[] = "({$tempwhere})";
+
+ } else {
+
+ $type = sanitise_string($type);
+ if ($subtype !== "" AND !$subtype = get_subtype_id($type, $subtype))
+ return false;
+
+ if ($type != "")
+ $where[] = "type='$type'";
+ if ($subtype!=="")
+ $where[] = "subtype=$subtype";
+ }
+
+ if ($owner_guid != "") {
+ if (!is_array($owner_guid)) {
+ $owner_array = array($owner_guid);
+ $owner_guid = (int) $owner_guid;
+ $where[] = "owner_guid = '$owner_guid'";
+ } else if (sizeof($owner_guid) > 0) {
+ $owner_array = array_map('sanitise_int', $owner_guid);
+ // Cast every element to the owner_guid array to int
+ $owner_guid = array_map("sanitise_int", $owner_guid);
+ $owner_guid = implode(",",$owner_guid);
+ $where[] = "owner_guid in ({$owner_guid})";
+ }
+ }
+ if ($site_guid > 0)
+ $where[] = "site_guid = {$site_guid}";
+
+ if (!is_null($container_guid)) {
+ if (is_array($container_guid)) {
+ foreach($container_guid as $key => $val) $container_guid[$key] = (int) $val;
+ $where[] = "container_guid in (" . implode(",",$container_guid) . ")";
+ } else {
+ $container_guid = (int) $container_guid;
+ $where[] = "container_guid = {$container_guid}";
+ }
+ }
+ if ($timelower)
+ $where[] = "time_created >= {$timelower}";
+ if ($timeupper)
+ $where[] = "time_created <= {$timeupper}";
+
+ if (!$count) {
+ $query = "SELECT * from {$CONFIG->dbprefix}entities where ";
+ } else {
+ $query = "SELECT count(guid) as total from {$CONFIG->dbprefix}entities where ";
+ }
+ foreach ($where as $w)
+ $query .= " $w and ";
+ $query .= get_access_sql_suffix(); // Add access controls
+ if (!$count) {
+ $query .= " order by $order_by";
+ if ($limit) $query .= " limit $offset, $limit"; // Add order and limit
+ $dt = get_data($query, "entity_row_to_elggstar");
+ return $dt;
+ } else {
+ $total = get_data_row($query);
+ return $total->total;
+ }
+ }
+
+ function tp_list_entities($type= "", $subtype = "", $owner_guid = 0, $limit = 10, $fullview = true, $viewtypetoggle = false, $pagination = true) {
+
+ $offset = (int) get_input('offset');
+ $count = tp_get_entities($type, $subtype, $owner_guid, "", $limit, $offset, true);
+ $entities = tp_get_entities($type, $subtype, $owner_guid, "", $limit, $offset);
+
+ return tp_view_entity_list($entities, $count, $offset, $limit, $fullview, $viewtypetoggle, $pagination);
+ }
+
+ function tp_view_entity_list($entities, $count, $offset, $limit, $fullview = true, $viewtypetoggle = false, $pagination = true) {
+ $context = get_context();
+
+ $html = elgg_view('tidypics/gallery',array(
+ 'entities' => $entities,
+ 'count' => $count,
+ 'offset' => $offset,
+ 'limit' => $limit,
+ 'baseurl' => $_SERVER['REQUEST_URI'],
+ 'fullview' => $fullview,
+ 'context' => $context,
+ 'viewtypetoggle' => $viewtypetoggle,
+ 'viewtype' => get_input('search_viewtype','list'),
+ 'pagination' => $pagination
+ ));
+
+ return $html;
+ }
+
+ function tp_get_entities_from_annotations_calculate_x($sum = "sum", $entity_type = "", $entity_subtype = "", $name = "", $mdname = '', $mdvalue = '', $owner_guid = 0, $limit = 10, $offset = 0, $orderdir = 'desc', $count = false)
+ {
+ global $CONFIG;
+
+ $sum = sanitise_string($sum);
+ $entity_type = sanitise_string($entity_type);
+ $entity_subtype = get_subtype_id($entity_type, $entity_subtype);
+ $name = get_metastring_id($name);
+ $limit = (int) $limit;
+ $offset = (int) $offset;
+ $owner_guid = (int) $owner_guid;
+ if (!empty($mdname) && !empty($mdvalue)) {
+ $meta_n = get_metastring_id($mdname);
+ $meta_v = get_metastring_id($mdvalue);
+ }
+
+ if (empty($name)) return 0;
+
+ $where = array();
+
+ if ($entity_type!="")
+ $where[] = "e.type='$entity_type'";
+ if ($owner_guid > 0)
+ $where[] = "e.owner_guid = $owner_guid";
+ if ($entity_subtype)
+ $where[] = "e.subtype=$entity_subtype";
+ if ($name!="")
+ $where[] = "a.name_id='$name'";
+
+ if (!empty($mdname) && !empty($mdvalue)) {
+ if ($mdname!="")
+ $where[] = "m.name_id='$meta_n'";
+ if ($mdvalue!="")
+ $where[] = "m.value_id='$meta_v'";
+ }
+
+ if ($sum != "count")
+ $where[] = "a.value_type='integer'"; // Limit on integer types
+
+ if (!$count) {
+ $query = "SELECT distinct e.*, $sum(ms.string) as sum ";
+ } else {
+ $query = "SELECT count(distinct e.guid) as num, $sum(ms.string) as sum ";
+ }
+ $query .= " from {$CONFIG->dbprefix}entities e JOIN {$CONFIG->dbprefix}annotations a on a.entity_guid = e.guid JOIN {$CONFIG->dbprefix}metastrings ms on a.value_id=ms.id ";
+
+ if (!empty($mdname) && !empty($mdvalue)) {
+ $query .= " JOIN {$CONFIG->dbprefix}metadata m on m.entity_guid = e.guid ";
+ }
+
+ $query .= " WHERE ";
+ foreach ($where as $w)
+ $query .= " $w and ";
+ $query .= get_access_sql_suffix("a"); // now add access
+ $query .= ' and ' . get_access_sql_suffix("e"); // now add access
+ if (!$count) $query .= ' group by e.guid';
+
+ if (!$count) {
+ $query .= ' order by sum ' . $orderdir;
+ $query .= ' limit ' . $offset . ' , ' . $limit;
+ return get_data($query, "entity_row_to_elggstar");
+ } else {
+ if ($row = get_data_row($query)) {
+ return $row->num;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Is page owner a group - convenience function
+ *
+ * @return true/false
+ */
+ function tp_is_group_page() {
+
+ if ($group = page_owner_entity()) {
+ if ($group instanceof ElggGroup)
+ return true;
+ }
+
+ return false;
+ }
+
+
+ /**
+ * Is the request from a known browser
+ *
+ * @return true/false
+ */
+ function tp_is_person()
+ {
+ $known = array('msie', 'mozilla', 'firefox', 'safari', 'webkit', 'opera', 'netscape', 'konqueror', 'gecko');
+
+ $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
+
+ foreach ($known as $browser)
+ {
+ if (strpos($agent, $browser) !== false) {
+ return true;
+ }
+ }
+
+ return false;
+ }
?> \ No newline at end of file