]> gitweb.fluxo.info Git - lorea/elgg.git/commitdiff
cleaned up library files
authorCash Costello <cash.costello@gmail.com>
Sat, 1 May 2010 12:24:16 +0000 (12:24 +0000)
committerCash Costello <cash.costello@gmail.com>
Sat, 1 May 2010 12:24:16 +0000 (12:24 +0000)
lib/album.php
lib/exif.php
lib/image.php
lib/migrate.php
lib/resize.php
lib/tidypics.php
lib/watermark.php

index 7e03ed6ebc9f4bdf33d6fdf2a7cb94da945d6707..7cd27dcde28c2c78185622e98f63087b5b910e93 100644 (file)
@@ -1,23 +1,19 @@
 <?php
-       /**
-        * Tidypics Album class
-        * 
-        */
+/**
+ * Tidypics Album class
+ *
+ * @package TidypicsAlbum
+ */
 
 
-       class TidypicsAlbum extends ElggObject
-       {
-               protected function initialise_attributes()
-               {
-                       parent::initialise_attributes();
-                       
-                       $this->attributes['subtype'] = "album";
-               }
-               
-               public function __construct($guid = null) 
-               {
-                       parent::__construct($guid);
-               }
+class TidypicsAlbum extends ElggObject {
+       protected function initialise_attributes() {
+               parent::initialise_attributes();
+
+               $this->attributes['subtype'] = "album";
+       }
+
+       public function __construct($guid = null) {
+               parent::__construct($guid);
        }
-       
-?>
\ No newline at end of file
+}
index 81f5dd9603137141427681096587046c154b0929..55612a433da18aecca1dcdfdc369f0906cb45173 100644 (file)
@@ -1,51 +1,75 @@
 <?php
+/**
+ * Exif Processing Library
+ *
+ * @package TidypicsExif
+ */
 
+/**
+ * Pull EXIF data from image file
+ * 
+ * @param TidypicsImage $file
+ */
 function td_get_exif($file) {
-       
+
        // catch for those who don't have exif module loaded
-       if (!is_callable('exif_read_data'))
+       if (!is_callable('exif_read_data')) {
                return;
-               
+       }
+
        $mime = $file->mimetype;
-       if ($mime != 'image/jpeg' && $mime != 'image/pjpeg')
+       if ($mime != 'image/jpeg' && $mime != 'image/pjpeg') {
                return;
+       }
 
        $filename = $file->getFilenameOnFilestore();
        $exif = exif_read_data($filename);
        create_metadata($file->getGUID(), "tp_exif", serialize($exif), "text", $file->getObjectOwnerGUID(), ACCESS_PUBLIC);
 }
 
+/**
+ * Grab array of EXIF data for display
+ * 
+ * @param int $file_guid GUID of TidypicsImage
+ * @return array|false
+ */
 function tp_exif_formatted($file_guid) {
 
        $metadata_exif = get_metadata_byname($file_guid, "tp_exif");
-       if (!$metadata_exif) { //try to load it from the file if its not in the database
+       if (!$metadata_exif) { 
+               // //try to load it from the file if its not in the database
                $file = new ElggFile($file_guid);
                td_get_exif($file);
                unset($file);
                $metadata_exif = get_metadata_byname($file_guid, "tp_exif");
        }
-       
+
        if (!$metadata_exif) {
                return false;
        }
 
        $exif = unserialize($metadata_exif["value"]);
-       
+
        $model = $exif['Model'];
-       if(!$model) $model = "N/A";
+       if (!$model) {
+               $model = "N/A";
+       }
        $exif_data['Model'] = $model;
 
        $exposure = $exif['ExposureTime'];
-       if (!$exposure) $exposure = "N/A";
+       if (!$exposure) {
+               $exposure = "N/A";
+       }
        $exif_data['Shutter'] = $exposure;
 
        //got the code snippet below from http://www.zenphoto.org/support/topic.php?id=17
        //convert the raw values to understandible values
        $Fnumber = explode("/", $exif['FNumber']);
-       if ($Fnumber[1] != 0)
+       if ($Fnumber[1] != 0) {
                $Fnumber = $Fnumber[0] / $Fnumber[1];
-       else
+       } else {
                $Fnumber = 0;
+       }
        if (!$Fnumber) {
                $Fnumber = "N/A";
        } else {
@@ -54,15 +78,20 @@ function tp_exif_formatted($file_guid) {
        $exif_data['Aperture'] = $Fnumber;
 
        $iso = $exif['ISOSpeedRatings'];
-       if (!$iso) $iso = "N/A";
+       if (!$iso) {
+               $iso = "N/A";
+       }
        $exif_data['ISO Speed'] = $iso;
 
        $Focal = explode("/", $exif['FocalLength']);
-       if ($Focal[1] != 0)
+       if ($Focal[1] != 0) {
                $Focal = $Focal[0] / $Focal[1];
-       else
+       } else {
                $Focal = 0;
-       if (!$Focal || round($Focal) == "0") $Focal = 0;
+       }
+       if (!$Focal || round($Focal) == "0") {
+               $Focal = 0;
+       }
        if (round($Focal) == 0) {
                $Focal = "N/A";
        } else {
@@ -71,9 +100,10 @@ function tp_exif_formatted($file_guid) {
        $exif_data['Focal Length'] = $Focal;
 
        $captured = $exif['DateTime'];
-       if (!$captured) $captured = "N/A";
+       if (!$captured) {
+               $captured = "N/A";
+       }
        $exif_data['Captured'] = $captured;
 
        return $exif_data;
 }
-?>
\ No newline at end of file
index 183d03c1c1daec5d2fc32ea48e5362ca62e74dad..4100f3f7bea31173e3bcdb1557a8e57ed7a624f5 100644 (file)
 <?php
+/**
+ * Tidypics Image class
+ *
+ * @package TidypicsImage
+ */
+
+
+class TidypicsImage extends ElggFile {
+       protected function initialise_attributes() {
+               parent::initialise_attributes();
+
+               $this->attributes['subtype'] = "image";
+       }
+
+       public function __construct($guid = null) {
+               parent::__construct($guid);
+       }
+
        /**
-        * Tidypics Image class
-        * 
+        * Has the photo been tagged with "in this photo" tags
+        *
+        * @return true/false
         */
+       public function isPhotoTagged() {
+               $num_tags = count_annotations($this->getGUID(), 'object', 'image', 'phototag');
+               if ($num_tags > 0) {
+                       return true;
+               } else {
+                       return false;
+               }
+       }
 
+       /**
+        * Get an array of photo tag information
+        *
+        * @return array of json representations of the tags and the tag link text
+        */
+       public function getPhotoTags() {
+               global $CONFIG;
 
-       class TidypicsImage extends ElggFile
-       {
-               protected function initialise_attributes()
-               {
-                       parent::initialise_attributes();
-                       
-                       $this->attributes['subtype'] = "image";
-               }
-               
-               public function __construct($guid = null) 
-               {
-                       parent::__construct($guid);
+               // get tags as annotations
+               $photo_tags = get_annotations($this->getGUID(), 'object', 'image', 'phototag');
+               if (!$photo_tags) {
+                       // no tags or user doesn't have permission to tags, so return
+                       return false;
                }
-               
-               /**
-                * Has the photo been tagged with "in this photo" tags
-                *
-                * @return true/false
-                */
-               public function isPhotoTagged()
-               {
-                       $num_tags = count_annotations($this->getGUID(), 'object', 'image', 'phototag');
-                       if ($num_tags > 0) {
-                               return true;
+
+               $photo_tags_json = "[";
+               foreach ($photo_tags as $p) {
+                       $photo_tag = unserialize($p->value);
+
+                       // create link to page with other photos tagged with same tag
+                       $phototag_text = $photo_tag->value;
+                       $phototag_link = $CONFIG->wwwroot . 'search/?tag=' . $phototag_text . '&amp;subtype=image&amp;object=object';
+                       if ($photo_tag->type === 'user') {
+                               $user = get_entity($photo_tag->value);
+                               if ($user) {
+                                       $phototag_text = $user->name;
+                               } else {
+                                       $phototag_text = "unknown user";
+                               }
+
+                               $phototag_link = $CONFIG->wwwroot . "pg/photos/tagged/" . $photo_tag->value;
+                       }
+
+                       if (isset($photo_tag->x1)) {
+                               // hack to handle format of Pedro Prez's tags - ugh
+                               $photo_tag->coords = "\"x1\":\"{$photo_tag->x1}\",\"y1\":\"{$photo_tag->y1}\",\"width\":\"{$photo_tag->width}\",\"height\":\"{$photo_tag->height}\"";
+                               $photo_tags_json .= '{' . $photo_tag->coords . ',"text":"' . $phototag_text . '","id":"' . $p->id . '"},';
                        } else {
-                               return false;
+                               $photo_tags_json .= '{' . $photo_tag->coords . ',"text":"' . $phototag_text . '","id":"' . $p->id . '"},';
                        }
+
+                       // prepare variable arrays for tagging view
+                       $photo_tag_links[$p->id] = array('text' => $phototag_text, 'url' => $phototag_link);
                }
-               
-               /**
-                * Get an array of photo tag information
-                *
-                * @return array of json representations of the tags and the tag link text
-                */
-               public function getPhotoTags() 
-               {
-                       global $CONFIG;
-                       
-                       // get tags as annotations
-                       $photo_tags = get_annotations($this->getGUID(), 'object', 'image', 'phototag');
-                       if (!$photo_tags) 
-                       {
-                               // no tags or user doesn't have permission to tags, so return
-                               return false;
-                       }
-                       
-                       $photo_tags_json = "[";
-                       foreach ($photo_tags as $p) 
-                       {
-                               $photo_tag = unserialize($p->value);
-                               
-                               // create link to page with other photos tagged with same tag
-                               $phototag_text = $photo_tag->value;
-                               $phototag_link = $CONFIG->wwwroot . 'search/?tag=' . $phototag_text . '&amp;subtype=image&amp;object=object';
-                               if ($photo_tag->type === 'user') 
-                               {
-                                       $user = get_entity($photo_tag->value);
-                                       if ($user) {
-                                               $phototag_text = $user->name;
-                                       } else {
-                                               $phototag_text = "unknown user";
-                                       }
-                                       
-                                       $phototag_link = $CONFIG->wwwroot . "pg/photos/tagged/" . $photo_tag->value;
-                               }
-                               
-                               if (isset($photo_tag->x1)) {
-                                       // hack to handle format of Pedro Prez's tags - ugh
-                                       $photo_tag->coords = "\"x1\":\"{$photo_tag->x1}\",\"y1\":\"{$photo_tag->y1}\",\"width\":\"{$photo_tag->width}\",\"height\":\"{$photo_tag->height}\""; 
-                                       $photo_tags_json .= '{' . $photo_tag->coords . ',"text":"' . $phototag_text . '","id":"' . $p->id . '"},';
-                               } else {
-                                       $photo_tags_json .= '{' . $photo_tag->coords . ',"text":"' . $phototag_text . '","id":"' . $p->id . '"},';
+
+               $photo_tags_json = rtrim($photo_tags_json,',');
+               $photo_tags_json .= ']';
+
+               $ret_data = array('json' => $photo_tags_json, 'links' => $photo_tag_links);
+               return $ret_data;
+       }
+
+       /**
+        * Get the view information for this image
+        *
+        * @param $viewer_guid the guid of the viewer (0 if not logged in)
+        * @return array with number of views, number of unique viewers, and number of views for this viewer
+        */
+       public function getViews($viewer_guid) {
+               $views = get_annotations($this->getGUID(), "object", "image", "tp_view", "", 0, 99999);
+               if ($views) {
+                       $total_views = count($views);
+
+                       if ($this->owner_guid == $viewer_guid) {
+                               // get unique number of viewers
+                               foreach ($views as $view) {
+                                       $diff_viewers[$view->owner_guid] = 1;
                                }
-                               
-                               // prepare variable arrays for tagging view
-                               $photo_tag_links[$p->id] = array('text' => $phototag_text, 'url' => $phototag_link);
+                               $unique_viewers = count($diff_viewers);
                        }
-                       
-                       $photo_tags_json = rtrim($photo_tags_json,',');
-                       $photo_tags_json .= ']';
-                       
-                       $ret_data = array('json' => $photo_tags_json, 'links' => $photo_tag_links);
-                       return $ret_data;
-               }
-               
-               /**
-                * Get the view information for this image
-                * 
-                * @param $viewer_guid the guid of the viewer (0 if not logged in)
-                * @return array with number of views, number of unique viewers, and number of views for this viewer
-                */
-               public function getViews($viewer_guid)
-               {
-                       $views = get_annotations($this->getGUID(), "object", "image", "tp_view", "", 0, 99999);
-                       if ($views) 
-                       {
-                               $total_views = count($views);
-                               
-                               if ($this->owner_guid == $viewer_guid)
-                               {
-                                       // get unique number of viewers
-                                       foreach ($views as $view)
-                                       {
-                                               $diff_viewers[$view->owner_guid] = 1;
-                                       }
-                                       $unique_viewers = count($diff_viewers);
-                               } 
-                               else if ($viewer_guid) 
-                               {
-                                       // get the number of times this user has viewed the photo
-                                       $my_views = 0;
-                                       foreach ($views as $view)
-                                       {
-                                               if ($view->owner_guid == $viewer_guid) {
-                                                       $my_views++;
-                                               }
+                       else if ($viewer_guid) {
+                               // get the number of times this user has viewed the photo
+                               $my_views = 0;
+                               foreach ($views as $view) {
+                                       if ($view->owner_guid == $viewer_guid) {
+                                               $my_views++;
                                        }
                                }
-                               
-                               $view_info = array("total" => $total_views, "unique" => $unique_viewers, "mine" => $my_views);
                        }
-                       else
-                       {
-                               $view_info = array("total" => 0, "unique" => 0, "mine" => 0);
-                       }
-                       
-                       return $view_info;
+
+                       $view_info = array("total" => $total_views, "unique" => $unique_viewers, "mine" => $my_views);
                }
-               
-               /**
-                * Add a tidypics view annotation to this image
-                * 
-                * @param $viewer_guid
-                * @return none
-                */
-               public function addView($viewer_guid)
-               {
-                       if ($viewer_guid != $this->owner_guid && tp_is_person()) {
-                               create_annotation($this->getGUID(), "tp_view", "1", "integer", $viewer_guid, ACCESS_PUBLIC);
-                       }
+               else {
+                       $view_info = array("total" => 0, "unique" => 0, "mine" => 0);
                }
+
+               return $view_info;
        }
-       
+
        /**
-        * get a list of people that can be tagged in an image
-        * 
-        * @param $viewer entity
-        * @return array of guid->name for tagging
+        * Add a tidypics view annotation to this image
+        *
+        * @param $viewer_guid
+        * @return none
         */
-       function tp_get_tag_list($viewer)
-       {
-               $friends = get_user_friends($viewer->getGUID(), '', 999, 0); 
-               $friend_list = array();
-               if ($friends) {
-                       foreach($friends as $friend) {
-                               //error_log("friend $friend->name");
-                               $friend_list[$friend->guid] = $friend->name;
-                       }
+       public function addView($viewer_guid) {
+               if ($viewer_guid != $this->owner_guid && tp_is_person()) {
+                       create_annotation($this->getGUID(), "tp_view", "1", "integer", $viewer_guid, ACCESS_PUBLIC);
                }
-               
-               // is this a group
-               $is_group = tp_is_group_page();
-               if ($is_group)
-               {
-                       $group_guid = page_owner();
-                       $viewer_guid = $viewer->guid;
-                       $members = get_group_members($group_guid, 999);
-                       if (is_array($members))
-                       {
-                               foreach ($members as $member)
-                               {
-                                       if ($viewer_guid != $member->guid) 
-                                       {
-                                               $group_list[$member->guid] = $member->name;
-                                               //error_log("group $member->name");
-                                       }
+       }
+}
+
+/**
+ * get a list of people that can be tagged in an image
+ *
+ * @param $viewer entity
+ * @return array of guid->name for tagging
+ */
+function tp_get_tag_list($viewer) {
+       $friends = get_user_friends($viewer->getGUID(), '', 999, 0);
+       $friend_list = array();
+       if ($friends) {
+               foreach($friends as $friend) {
+                       //error_log("friend $friend->name");
+                       $friend_list[$friend->guid] = $friend->name;
+               }
+       }
+
+       // is this a group
+       $is_group = tp_is_group_page();
+       if ($is_group) {
+               $group_guid = page_owner();
+               $viewer_guid = $viewer->guid;
+               $members = get_group_members($group_guid, 999);
+               if (is_array($members)) {
+                       foreach ($members as $member) {
+                               if ($viewer_guid != $member->guid) {
+                                       $group_list[$member->guid] = $member->name;
+                                       //error_log("group $member->name");
                                }
-                               
-                               // combine group and friends list
-                               $intersect = array_intersect_key($friend_list, $group_list);
-                               $unique_friends = array_diff_key($friend_list, $group_list);
-                               $unique_members = array_diff_key($group_list, $friend_list);
-                               //$friend_list = array_merge($friend_list, $group_list);
-                               //$friend_list = array_unique($friend_list);
-                               $friend_list = $intersect + $unique_friends + $unique_members;
                        }
+
+                       // combine group and friends list
+                       $intersect = array_intersect_key($friend_list, $group_list);
+                       $unique_friends = array_diff_key($friend_list, $group_list);
+                       $unique_members = array_diff_key($group_list, $friend_list);
+                       //$friend_list = array_merge($friend_list, $group_list);
+                       //$friend_list = array_unique($friend_list);
+                       $friend_list = $intersect + $unique_friends + $unique_members;
                }
-               
-               asort($friend_list);
-               
-               return $friend_list;
        }
-?>
\ No newline at end of file
+
+       asort($friend_list);
+
+       return $friend_list;
+}
index ca68ec8c4bad5c59682a252e2392eeefb8ba8940..f8e68a1be22347d48ae218d1d826c1c3b55e1e47 100644 (file)
@@ -1,4 +1,10 @@
 <?php
+/**
+ * Tidypics file plugin migration
+ *
+ * Supports moving photos from the files plugin to Tidypics. All of a users
+ * photos end up in a single album.
+ */
 
 // need access to ElggDiskFilestore::make_file_matrix(), which is protected.
 // this is a PITA.
index fdc321ab105c79162d6c9b0c8d7e062ce3f163b4..95db9db2b1d04b6d241d8a5ae4e33ea413d5f6cc 100644 (file)
 <?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;
+/**
+ * Elgg tidypics library of resizing functions
+ *
+ * @package TidypicsImageResize
+ */
+
+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) {
+               // move this out of library
+               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;
                }
-               $image_sizes = unserialize($image_sizes);
 
-               $thumb = new ElggFile();
-               $thumb->owner_guid = $file->owner_guid;
-               $thumb->container_guid = $file->container_guid;
+               $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 thumbail
-               $thumb->setFilename($prefix."thumb".$filestorename);
-               $thumbname = $thumb->getFilenameOnFilestore();
-               $rtn_code = tp_gd_resize(       $file->getFilenameOnFilestore(),
+       // tiny thumbnail
+       $thumb->setFilename($prefix."thumb".$filestorename);
+       $thumbname = $thumb->getFilenameOnFilestore();
+       $rtn_code = tp_imagick_resize(  $file->getFilenameOnFilestore(),
                                                                        $thumbname,
-                                                                       false,
                                                                        $image_sizes['thumb_image_width'],
-                                                                       $image_sizes['thumb_image_height'], 
+                                                                       $image_sizes['thumb_image_height'],
                                                                        true);
-               if (!$rtn_code)
-                       return false;
-               $file->thumbnail = $prefix."thumb".$filestorename;
-
+       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(),
+       // album thumbnail
+       $thumb->setFilename($prefix."smallthumb".$filestorename);
+       $thumbname = $thumb->getFilenameOnFilestore();
+       $rtn_code = tp_imagick_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;
-
+                                                                       $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(),
+       // main image
+       $thumb->setFilename($prefix."largethumb".$filestorename);
+       $thumbname = $thumb->getFilenameOnFilestore();
+       $rtn_code = tp_imagick_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;
-                       
+                                                                       $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 ($width > $maxwidth) {
-                       $newheight = floor($height * ($maxwidth / $width));
+
+               if ($maxheight == $maxwidth) {
+                       // if input arguments = square, no need to use above calculations (which can have round-off errors)
                        $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;
-                       }
+                       $newheight = $maxheight;
                } else {
-                       if ($x1 == 0 && $y1 == 0 && $x2 == 0 && $y2 ==0) {
-                               $widthoffset = 0;
-                               $heightoffset = 0;
+                       if ($newwidth < $newheight) {
+                               $newheight = $newwidth;
                        } else {
-                               $widthoffset = $x1;
-                               $heightoffset = $y1;
-                               $width = ($x2 - $x1);
-                               $height = ($y2 - $y1);
+                               $newwidth = $newheight;
                        }
                }
-               
-               if ($square) {
-                       $newheight = $maxheight;
-                       $newwidth = $maxwidth;
+       }
+
+
+       // 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;
                }
-               
-               $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;
+       } 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);
                }
-               
-               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);
+       try {
+               $img = new Imagick($input_name);
+       } catch (ImagickException $e) {
+               return false;
+       }
 
-               $thumb = new ElggFile();
-               $thumb->owner_guid = $file->owner_guid;
-               $thumb->container_guid = $file->container_guid;
+       $img->cropImage($width, $height, $xoffset, $yoffset);
 
-               // tiny thumbnail
-               $thumb->setFilename($prefix."thumb".$filestorename);
-               $thumbname = $thumb->getFilenameOnFilestore();
-               $rtn_code = tp_imagick_resize(  $file->getFilenameOnFilestore(),
+       // 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'], 
+                                                                               $image_sizes['thumb_image_height'],
                                                                                true);
-               if (!$rtn_code)
-                       return false;
-               $file->thumbnail = $prefix."thumb".$filestorename;
+       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(),
+       // 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;
-
+                                                                               $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(),
+       // 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_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;
-               
+                                                                               $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;
-               
-               // 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
+
+               // Square the image dimensions if we're wanting a square 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);
-                       }
-               }
 
+                       $newwidth = $width;
+                       $newheight = $height;
 
-               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;
+
+               if ($width > $maxwidth) {
+                       $newheight = floor($height * ($maxwidth / $width));
+                       $newwidth = $maxwidth;
                }
-               $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;
+               if ($newheight > $maxheight) {
+                       $newwidth = floor($newwidth * ($maxheight / $newheight));
+                       $newheight = $maxheight;
                }
-               $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
+
+               $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 ($width < $height) {
+                               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 {
-                                       $width = $height;
+                                       $widthoffset = $x1;
+                                       $heightoffset = $y1;
+                                       $width = ($x2 - $x1);
+                                       $height = ($y2 - $y1);
                                }
-                               
-                               $newwidth = $width;
-                               $newheight = $height;
-                               
                        }
 
-                       if ($width > $maxwidth) {
-                               $newheight = floor($height * ($maxwidth / $width));
+                       // Resize and return the image contents!
+                       if ($square) {
+                               $newheight = $maxheight;
                                $newwidth = $maxwidth;
                        }
-                       if ($newheight > $maxheight) {
-                               $newwidth = floor($newwidth * ($maxheight / $newheight));
-                               $newheight = $maxheight;
+                       $im_path = get_plugin_setting('im_path', 'tidypics');
+                       if (!$im_path) {
+                               $im_path = "/usr/bin/";
                        }
-
-                       $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 .= "/";
-                               // see imagemagick web site for explanation of these parameters
-                               // the ^ in the resize means those are minimum width and height values
-                               $command = $im_path . "convert \"$input_name\" -resize ".$newwidth."x".$newheight."^ -gravity center -extent ".$newwidth."x".$newheight." \"$output_name\"";
-                               $output = array();
-                               $ret = 0;
-                               exec($command, $output, $ret);
-                               if ($ret == 127) {
-                                       trigger_error('Tidypics warning: Image Magick convert is not found', E_USER_WARNING);
-                                       return false;
-                               } else if ($ret > 0) {
-                                       trigger_error('Tidypics warning: Image Magick convert failed', E_USER_WARNING);
-                                       return false;
-                               }
-                               return true;
+                       if (substr($im_path, strlen($im_path)-1, 1) != "/") {
+                               $im_path .= "/";
+                       }
+                       // see imagemagick web site for explanation of these parameters
+                       // the ^ in the resize means those are minimum width and height values
+                       $command = $im_path . "convert \"$input_name\" -resize ".$newwidth."x".$newheight."^ -gravity center -extent ".$newwidth."x".$newheight." \"$output_name\"";
+                       $output = array();
+                       $ret = 0;
+                       exec($command, $output, $ret);
+                       if ($ret == 127) {
+                               trigger_error('Tidypics warning: Image Magick convert is not found', E_USER_WARNING);
+                               return false;
+                       } else if ($ret > 0) {
+                               trigger_error('Tidypics warning: Image Magick convert failed', E_USER_WARNING);
+                               return false;
                        }
+                       return true;
                }
-
-               return false;
        }
 
-?>
\ No newline at end of file
+       return false;
+}
+
index 086885a897ed2921321c139a61073e3e7fa416b2..539db451a0390859758761440e7291215119f344 100644 (file)
@@ -1,89 +1,87 @@
 <?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, null, $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/';
-       }
-       
-       
+/**
+ * Elgg tidypics library of common functions
+ *
+ * @package TidypicsCommon
+ */
+
+/**
+ * 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:
        
-       /*********************************************************************
-        * 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))
+ 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, null, $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);
                                        $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 (!empty($tempwhere)) $where[] = "({$tempwhere})";
 
-               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 ";
+       } 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})";
                }
-               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;
+       }
+       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 {
-                       $total = get_data_row($query);
-                       return $total->total;
+                       $container_guid = (int) $container_guid;
+                       $where[] = "container_guid = {$container_guid}";
                }
        }
+       if ($timelower)
+               $where[] = "time_created >= {$timelower}";
+       if ($timeupper)
+               $where[] = "time_created <= {$timeupper}";
 
-       function tp_list_entities($type= "", $subtype = "", $owner_guid = 0, $container_guid = null, $limit = 10, $fullview = true, $viewtypetoggle = false, $pagination = true) {
-               
-               $offset = (int) get_input('offset');
-               $count = tp_get_entities($type, $subtype, $owner_guid, "", $limit, $offset, true, 0, $container_guid);
-               
-               $entities = tp_get_entities($type, $subtype, $owner_guid, "", $limit, $offset, false, 0, $container_guid);
+       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
 
-               return tp_view_entity_list($entities, $count, $offset, $limit, $fullview, $viewtypetoggle, $pagination);
+       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_view_entity_list($entities, $count, $offset, $limit, $fullview = true, $viewtypetoggle = false, $pagination = true) {
-               $context = get_context();
+}
+
+function tp_list_entities($type= "", $subtype = "", $owner_guid = 0, $container_guid = null, $limit = 10, $fullview = true, $viewtypetoggle = false, $pagination = true) {
+
+       $offset = (int) get_input('offset');
+       $count = tp_get_entities($type, $subtype, $owner_guid, "", $limit, $offset, true, 0, $container_guid);
 
-               $html = elgg_view('tidypics/gallery',array(
+       $entities = tp_get_entities($type, $subtype, $owner_guid, "", $limit, $offset, false, 0, $container_guid);
+
+       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,
                        'viewtypetoggle' => $viewtypetoggle,
                        'viewtype' => get_input('search_viewtype','list'),
                        'pagination' => $pagination
-               ));
-               
-               return $html;
+       ));
+
+       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);
        }
-       
-       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;
+       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'";
        }
-       
-       /**
-        * 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;
+
+       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 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;
+}
+
+/**
+ * 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
+
+       return false;
+}
index c7b8503c513acf0e15cff194776d2ac79a5aa3a1..6b16f0e4ac771cb558b864cf30d19d024b94d85a 100644 (file)
 <?php
-
+/**
+ * Watermarking functions
+ *
+ * @package TidypicsWatermark
+ */
+
+/**
+ * Make replacements in watermark text
+ *
+ * @param string $text
+ * @param ElggUser $owner
+ * @return string
+ */
 function tp_process_watermark_text($text, $owner) {
        global $CONFIG;
 
        $text = str_replace("%name%", $owner->name, $text);
        $text = str_replace("%sitename%", $CONFIG->sitename, $text);
-       
+
        return $text;
 }
 
+/**
+ * Create the watermark image filename
+ *
+ * @param string $text
+ * @param ElggUser $owner
+ * @return string
+ */
 function tp_get_watermark_filename($text, $owner) {
-       global $CONFIG;
 
        $base = strtolower($text);
        $base = preg_replace("/[^\w-]+/", "-", $base);
        $base = trim($base, '-');
-       
+
        $filename = tp_get_img_dir();
        $filename .= strtolower($owner->username . "_" . $base . "_stamp");
-       
+
        return $filename;
 }
 
+/**
+ * Use GD to apply watermark to image
+ *
+ * @param resource $image GD image resource
+ */
 function tp_gd_watermark($image) {
+       global $CONFIG;
+       
        $watermark_text = get_plugin_setting('watermark_text', 'tidypics');
-       if (!$watermark_text)
+       if (!$watermark_text) {
                return;
-       
+       }
+
        // plugins can do their own watermark and return false to prevent this function from running
-       if (trigger_plugin_hook('tp_watermark', 'gd', $image, true) === false)
+       if (trigger_plugin_hook('tp_watermark', 'gd', $image, true) === false) {
                return;
-       
-       global $CONFIG;
-               
+       }
+
        $owner = get_loggedin_user();
 
        $watermark_text = tp_process_watermark_text($watermark_text, $owner);
-               
+
        // transparent gray
        imagealphablending($image, true);
        $textcolor = imagecolorallocatealpha($image, 50, 50, 50, 60);
-       
+
        // font and location
-       $font = $CONFIG->pluginspath . "tidypics/fonts/LiberationSerif-Regular.ttf"; 
+       $font = $CONFIG->pluginspath . "tidypics/fonts/LiberationSerif-Regular.ttf";
        $bbox = imagettfbbox(20, 0, $font, $watermark_text);
-       
+
        $text_width = $bbox[2] - $bbox[0];
        $text_height = $bbox[1] - $bbox[7];
-       
+
        $image_width = imagesx($image);
        $image_height = imagesy($image);
-       
+
        $left = $image_width / 2 - $text_width / 2;
        $top = $image_height - 20;
-       
+
        // write the text on the image
        imagettftext($image, 20, 0, $left, $top, $textcolor, $font, $watermark_text);
 }
 
+/**
+ * imagick watermarking
+ *
+ * @param string $filename
+ * @return bool
+ */
 function tp_imagick_watermark($filename) {
 
        $watermark_text = get_plugin_setting('watermark_text', 'tidypics');
-       if (!$watermark_text)
-               return;
-       
+       if (!$watermark_text) {
+               return false;
+       }
+
        // plugins can do their own watermark and return false to prevent this function from running
-       if (trigger_plugin_hook('tp_watermark', 'imagick', $filename, true) === false)
-               return;
-       
+       if (trigger_plugin_hook('tp_watermark', 'imagick', $filename, true) === false) {
+               return true;
+       }
+
        $owner = get_loggedin_user();
 
        $watermark_text = tp_process_watermark_text($watermark_text, $owner);
-       
-    $img = new Imagick($filename);
 
-    $img->readImage($image);
+       $img = new Imagick($filename);
+
+       $img->readImage($image);
 
-    $draw = new ImagickDraw();
+       $draw = new ImagickDraw();
 
-    //$draw->setFont("");
+       //$draw->setFont("");
 
-    $draw->setFontSize(28);
+       $draw->setFontSize(28);
 
-    $draw->setFillOpacity(0.5);
+       $draw->setFillOpacity(0.5);
 
-    $draw->setGravity(Imagick::GRAVITY_SOUTH);
+       $draw->setGravity(Imagick::GRAVITY_SOUTH);
+
+       $img->annotateImage($draw, 0, 0, 0, $watermark_text);
 
-    $img->annotateImage($draw, 0, 0, 0, $watermark_text);
-       
        if ($img->writeImage($filename) != true) {
                $img->destroy();
                return false;
        }
-       
+
        $img->destroy();
-       
+
        return true;
 }
 
+/**
+ * ImageMagick watermarking
+ *
+ * @param string $filename
+ */
 function tp_im_cmdline_watermark($filename) {
-       
+
        $watermark_text = get_plugin_setting('watermark_text', 'tidypics');
-       if (!$watermark_text)
+       if (!$watermark_text) {
                return;
-       
+       }
+
        // plugins can do their own watermark and return false to prevent this function from running
-       if (trigger_plugin_hook('tp_watermark', 'imagemagick', $filename, true) === false)
+       if (trigger_plugin_hook('tp_watermark', 'imagemagick', $filename, true) === false) {
                return;
-       
+       }
+
        $im_path = get_plugin_setting('im_path', 'tidypics');
        if (!$im_path) {
                $im_path = "/usr/bin/";
        }
-       
+
        // make sure end of path is /
-       if (substr($im_path, strlen($im_path)-1, 1) != "/") $im_path .= "/";
+       if (substr($im_path, strlen($im_path)-1, 1) != "/") {
+               $im_path .= "/";
+       }
+
 
-       
        $owner = get_loggedin_user();
 
        $watermark_text = tp_process_watermark_text($watermark_text, $owner);
-       
+
        $ext = ".png";
-       
+
        $user_stamp_base = tp_get_watermark_filename($watermark_text, $owner);
-       
-       
-       if ( !file_exists( $user_stamp_base . $ext )) { //create the watermark if it doesn't exist
+
+
+       if ( !file_exists( $user_stamp_base . $ext )) {
+               //create the watermark image if it doesn't exist
                $commands = array();
                $commands[] = $im_path . 'convert -size 300x50 xc:grey30 -pointsize 20 -gravity center -draw "fill grey70  text 0,0  \''. $watermark_text . '\'" "'. $user_stamp_base . '_fgnd' . $ext . '"';
                $commands[] = $im_path . 'convert -size 300x50 xc:black -pointsize 20 -gravity center -draw "fill white  text  1,1  \''. $watermark_text . '\' text  0,0  \''. $watermark_text . '\' fill black  text -1,-1 \''. $watermark_text . '\'" +matte ' . $user_stamp_base . '_mask' . $ext;
@@ -134,12 +177,12 @@ function tp_im_cmdline_watermark($filename) {
                $commands[] = $im_path . 'mogrify -trim +repage "' . $user_stamp_base . $ext . '"';
                $commands[] = 'rm "' . $user_stamp_base . '_mask' . $ext . '"';
                $commands[] = 'rm "' . $user_stamp_base . '_fgnd' . $ext . '"';
-               
+
                foreach( $commands as $command ) {
                        exec( $command );
                }
        }
-       
+
        //apply the watermark
        $commands = array();
        $commands[] = $im_path . 'composite -gravity south -geometry +0+10 "' . $user_stamp_base . $ext . '" "' . $filename . '" "' . $filename . '_watermarked"';
@@ -148,4 +191,3 @@ function tp_im_cmdline_watermark($filename) {
                exec( $command );
        }
 }
-?>
\ No newline at end of file