[ PHPXref.com ] [ Generated: Sun Jul 20 21:17:54 2008 ] [ Zoph 0.5.1 ]
[ Index ]     [ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

/php/ -> photo.inc.php (source)

   1  <?php
   2  
   3  /*
   4   * A class corresponding to the photos table.
   5   *
   6   * This file is part of Zoph.
   7   *
   8   * Zoph is free software; you can redistribute it and/or modify
   9   * it under the terms of the GNU General Public License as published by
  10   * the Free Software Foundation; either version 2 of the License, or
  11   * (at your option) any later version.
  12   * 
  13   * Zoph is distributed in the hope that it will be useful,
  14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16   * GNU General Public License for more details.
  17   * You should have received a copy of the GNU General Public License
  18   * along with Zoph; if not, write to the Free Software
  19   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20   */
  21  
  22  class photo extends zoph_table {
  23  
  24      var $photographer;
  25      var $location;
  26  
  27      function photo($id = 0) {
  28          parent::zoph_table("photos", array("photo_id"), array(""));
  29          $this->set("photo_id",$id);
  30      }
  31  
  32      function lookup($user = null) {
  33  
  34          if (!$this->get("photo_id")) { return; }
  35  
  36          if ($user && !$user->is_admin()) {
  37              $sql =
  38                  "select p.* from " .
  39                  DB_PREFIX . "photos as p, " .
  40                  DB_PREFIX . "photo_albums as pa, " .
  41                  DB_PREFIX . "album_permissions as ap " .
  42                  "where p.photo_id = '" . escape_string($this->get("photo_id")) . "'" .
  43                  " and p.photo_id = pa.photo_id" .
  44                  " and pa.album_id = ap.album_id" .
  45                  " and ap.user_id = '" . escape_string($user->get("user_id")) . "'" .
  46                  " and ap.access_level >= p.level " .
  47                  "limit 0, 1";
  48          }
  49          else {
  50              $sql =
  51                  "select * from " . DB_PREFIX . "photos " .
  52                  "where photo_id = '" . escape_string($this->get("photo_id")) . "'";
  53          }
  54  
  55          $success = parent::lookup($sql);
  56  
  57          if ($success) {
  58              $this->lookup_photographer();
  59              $this->lookup_location();
  60          }
  61  
  62          return $success;
  63      }
  64  
  65      function lookup_photographer() {
  66          if ($this->get("photographer_id") > 0) {
  67              $this->photographer = new person($this->get("photographer_id"));
  68              $this->photographer->lookup();
  69          }
  70      }
  71  
  72      function lookup_location() {
  73          if ($this->get("location_id") > 0) {
  74              $this->location = new place($this->get("location_id"));
  75              $this->location->lookup();
  76          }
  77      }
  78  
  79      function delete() {
  80          parent::delete(null, array("photo_people", "photo_categories",
  81              "photo_albums"));
  82      }
  83  
  84      function update($vars = null, $suffix = '') {
  85          parent::update();
  86  
  87          if (!$vars) { return; }
  88  
  89          $this->update_relations($vars, $suffix);
  90      }
  91  
  92      function update_relations($vars, $suffix = '') {
  93          if ($vars["_album$suffix"]) {
  94              $this->add_to_album($vars["_album$suffix"]);
  95          }
  96  
  97          if ($vars["_remove_album$suffix"]) {
  98              $this->remove_from_album($vars["_remove_album$suffix"]);
  99          }
 100  
 101          if ($vars["_category$suffix"]) {
 102              $this->add_to_category($vars["_category$suffix"]);
 103          }
 104  
 105          if ($vars["_remove_category$suffix"]) {
 106              $this->remove_from_category($vars["_remove_category$suffix"]);
 107          }
 108  
 109          if ($vars["_person$suffix"]) {
 110              $this->add_to_person($vars["_person$suffix"], $vars["_position$suffix"]);
 111          }
 112          for ($i = 0; $i < MAX_PEOPLE_SLOTS; $i++) {
 113             if ($vars["_person_" . $i . $suffix]) {
 114                 $this->add_to_person($vars["_person_" . $i . $suffix], 
 115                    $vars["_position_" . $i . $suffix]);
 116             }
 117          }
 118      
 119          if ($vars["_remove_person$suffix"]) {
 120              $this->remove_from_person($vars["_remove_person$suffix"]);
 121          }
 122      }
 123  
 124      function add_to_album($album_id) {
 125          $sql =
 126              "insert into " . DB_PREFIX . "photo_albums " .
 127              "(photo_id, album_id) values ('" .
 128              escape_string($this->get("photo_id")) . "', '" .
 129              escape_string($album_id) . "')";
 130          execute_query($sql, 1);
 131      }
 132  
 133      function remove_from_album($album_ids) {
 134          if (!is_array($album_ids)) {
 135              $album_ids = array($album_ids);
 136          }
 137  
 138          foreach ($album_ids as $album_id) {
 139              $sql =
 140                  "delete from " . DB_PREFIX . "photo_albums " .
 141                  "where photo_id = '" . escape_string($this->get("photo_id")) . "'" .
 142                  " and album_id = '" . escape_string($album_id) . "'";
 143              execute_query($sql, 1);
 144          }
 145      }
 146  
 147      function add_to_category($category_id) {
 148          $sql =
 149              "insert into " . DB_PREFIX . "photo_categories " .
 150              "(photo_id, category_id) values ('" .
 151              escape_string($this->get("photo_id")) . "', '" .
 152              escape_string($category_id) . "')";
 153          execute_query($sql, 1);
 154      }
 155  
 156      function remove_from_category($category_ids) {
 157          if (!is_array($category_ids)) {
 158              $category_ids = array($category_ids);
 159          }
 160  
 161          foreach ($category_ids as $category_id) {
 162              $sql =
 163                  "delete from " . DB_PREFIX . "photo_categories " .
 164                  "where photo_id = '" . escape_string($this->get("photo_id")) . "'" .
 165                  " and category_id = '" . escape_string($category_id) . "'";
 166              execute_query($sql, 1);
 167          }
 168      }
 169  
 170      function add_to_person($person_id, $position = "null") {
 171          if ($position && $position != "null") {
 172              $position = "'" . escape_string($position) . "'";
 173          }
 174  
 175          $sql =
 176              "insert into " . DB_PREFIX . "photo_people " .
 177              "(photo_id, person_id, position) " .
 178              "values ('" . escape_string($this->get("photo_id")) . "', '" .
 179              escape_string($person_id) . "', $position)";
 180          execute_query($sql);
 181      }
 182  
 183      function remove_from_person($person_ids) {
 184          if (!is_array($person_ids)) {
 185              $person_ids = array($person_ids);
 186          }
 187  
 188          foreach ($person_ids as $person_id) {
 189              $sql =
 190                  "delete from " . DB_PREFIX . "photo_people " .
 191                  "where photo_id = '" . escape_string($this->get("photo_id")) . "'" .
 192                  " and person_id = '" . escape_string($person_id) . "'";
 193              execute_query($sql);
 194          }
 195      }
 196  
 197      function lookup_albums($user = null) {
 198  
 199          if ($user && !$user->is_admin()) {
 200              $sql =
 201                  "select al.album_id, al.parent_album_id, al.album from " .
 202                  DB_PREFIX . "photo_albums as pa, " .
 203                  DB_PREFIX . "albums as al, " .
 204                  DB_PREFIX . "album_permissions as ap " .
 205                  "where pa.photo_id = '" .
 206                  escape_string($this->get("photo_id")) . "'" .
 207                  " and pa.album_id = al.album_id" .
 208                  " and al.album_id = ap.album_id" .
 209                  " and ap.user_id = '" .
 210                  escape_string($user->get("user_id")) . "' " .
 211                  " and ap.access_level >= " .
 212                  escape_string($this->get("level")) . " " .
 213                  "order by al.album";
 214          }
 215          else {
 216              $sql =
 217                  "select al.album_id, al.parent_album_id, al.album from " .
 218                  DB_PREFIX . "photo_albums as pa, " .
 219                  DB_PREFIX . "albums as al " .
 220                  "where pa.photo_id = '" .
 221                  escape_string($this->get("photo_id")) . "'" .
 222                  " and pa.album_id = al.album_id order by al.album";
 223          }
 224  
 225          return get_records_from_query("album", $sql);
 226      }
 227  
 228      function lookup_categories($user = null) {
 229          $sql =
 230              "select cat.category_id, cat.parent_category_id, cat.category from " .
 231              DB_PREFIX . "photo_categories as pc, " .
 232              DB_PREFIX . "categories as cat " .
 233              "where pc.photo_id = '" . escape_string($this->get("photo_id")) . "'" .
 234              " and pc.category_id = cat.category_id order by cat.category";
 235  
 236          return get_records_from_query("category", $sql);
 237      }
 238  
 239      function lookup_people() {
 240          $sql =
 241              "select psn.person_id, psn.last_name, " .
 242              "psn.first_name, psn.called from " .
 243              DB_PREFIX . "photo_people as pp, " .
 244              DB_PREFIX . "people as psn " .
 245              "where pp.photo_id = '" .
 246              escape_string($this->get("photo_id")) . "'" .
 247              " and pp.person_id = psn.person_id order by pp.position";
 248  
 249          return get_records_from_query("person", $sql);
 250      }
 251  
 252      function get_file_path() {
 253          return IMAGE_DIR . $this->get("path") . "/" . $this->get("name");
 254      }
 255  
 256      function get_midsize_img() {
 257          return $this->get_image_tag(MID_PREFIX);
 258      }
 259  
 260      function get_thumbnail_link($link = null) {
 261          if (!$link) {
 262              $link = "photo.php?photo_id=" . $this->get("photo_id");
 263          }
 264          return "            <a href=\"$link\">" . $this->get_image_tag(THUMB_PREFIX) . "</a>";
 265      }
 266  
 267      function get_fullsize_link($title, $FULLSIZE_NEW_WIN) {
 268          $image = $this->get_image_href();
 269          $newwin = ($FULLSIZE_NEW_WIN ? "target=\"_blank\"" : "");
 270          return "<a href=\"$image\" $newwin>$title</a>";
 271      }
 272  
 273      function get_image_href($type = null, $use_file = 0) {
 274  
 275          if (USE_IMAGE_SERVICE && !$use_file) {
 276              $image_href = "image_service.php?photo_id=" . $this->get("photo_id");
 277              if ($type) {
 278                  $image_href .= "&amp;type=" . $type;
 279              }
 280  
 281              if (SID) {
 282                  $image_href .= "&amp;" . SID;
 283              }
 284          }
 285          else {
 286              if ($use_file) {
 287                  $dir = IMAGE_DIR;
 288              }
 289              else {
 290                  $dir = WEB_IMAGE_DIR;
 291              }
 292  
 293              $image_href = $dir . $this->get("path") . "/";
 294  
 295              if ($type) {
 296                  $image_href .= $type . "/" . $type . "_" .
 297                      get_converted_image_name($this->get("name"));
 298              }
 299              else {
 300                  $image_href .= $this->get("name");
 301              }
 302  
 303              $image_href = encode_href($image_href);
 304          }
 305  
 306          return $image_href;
 307      }
 308  
 309      function get_image_tag($type = null) {
 310  
 311          $image_href = $this->get_image_href($type);
 312  
 313          if (!$image_href) {
 314              return "";
 315          }
 316  
 317          $size_string = "";
 318  
 319          $width = $this->get("width");
 320          $height = $this->get("height");
 321  
 322          if ($type) {
 323              if ($type == THUMB_PREFIX) {
 324                  $max_side = THUMB_SIZE;
 325              }
 326              else if ($type == MID_PREFIX) {
 327                  $max_side = MID_SIZE;
 328              }
 329  
 330              if ($max_side) {
 331                  if (!$width || !$height) {
 332                      // pick some reasonable values
 333                      $width = $max_side;
 334                      $height = (int)round(0.75 * $width);
 335                  }
 336                  else if ($width >= $height) {
 337                      $height = (int)round(($max_side/$width) * $height);
 338                      $width = $max_side;
 339                  }
 340                  else {
 341                      $width = (int)round(($max_side/$height) * $width);
 342                      $height = $max_side;
 343                  }
 344              }
 345          }
 346  
 347          $size_string = " width=\"$width\" height=\"$height\"";
 348          $alt = htmlentities($this->get("title"));
 349  return "<img src=\"$image_href\" class=\"" . $type . "\" " . $size_string . "alt=\"$alt\"" . ">";
 350  }
 351  
 352      function get_rating($user_id) {
 353  
 354          $photo_id = $this->get("photo_id");
 355  
 356          $query =
 357              "select rating from " . DB_PREFIX . "photo_ratings " .
 358              "where user_id = '" . escape_string($user_id) . "'" .
 359              " and photo_id = '". escape_string($this->get("photo_id")) . "'";
 360  
 361          if (DEBUG > 1) { echo "$query<br>\n"; }
 362  
 363          $result = mysql_query($query)
 364              or die_with_mysql_error("Rating lookup failed");
 365  
 366          $rating = null;
 367          if ($row = mysql_fetch_array($result)) {
 368              $rating = $row[0];
 369          }
 370  
 371          return $rating;
 372      }
 373  
 374      /*
 375       * Stores the rating of a photo for a user and updates the
 376       * average rating.
 377       *
 378       * This function from Jan Miczaika
 379       */
 380      function rate($user_id, $rating) {
 381  
 382          if (!$user_id || !$rating) {
 383              return null;
 384          }
 385  
 386          $photo_id = $this->get("photo_id");
 387  
 388          $query =
 389              "select * from " . DB_PREFIX . "photo_ratings " .
 390              "where user_id = '" . escape_string($user_id) . "'" .
 391              " and photo_id = '". escape_string($photo_id) . "'";
 392  
 393          if (DEBUG > 1) { echo "$query<br>\n"; }
 394  
 395          $result = mysql_query($query)
 396              or die_with_mysql_error("Rating lookup failed");
 397  
 398          //if the user has already voted, update the vote, else insert a new one
 399  
 400          if (mysql_num_rows($result) > 0) {
 401              $query =
 402                  "update " . DB_PREFIX . "photo_ratings " .
 403                  "set rating = '" . escape_string($rating) . "' " .
 404                  "where user_id = '" . escape_string($user_id) . "'" .
 405                  " and photo_id = '". escape_string($photo_id) . "'";
 406          }
 407          else {
 408              $query =
 409                  "insert into " . DB_PREFIX . "photo_ratings " .
 410                  "(photo_id, user_id, rating) values " .
 411                  " ('" . escape_string($photo_id) . "', '" .
 412                  escape_string($user_id) . "', '" .
 413                  escape_string($rating) . "')";
 414          }
 415  
 416          if (DEBUG > 1) { echo "$query<br>\n"; }
 417  
 418          $result = mysql_query($query)
 419              or die_with_mysql_error("Rating input failed");
 420  
 421          //now recalculate the average, and input it in the photo table
 422  
 423          $query = "select avg(rating) from " . DB_PREFIX . "photo_ratings ".
 424              " where photo_id = '" . escape_string($photo_id) . "'";
 425  
 426          if (DEBUG > 1) { echo "$query<br>\n"; }
 427  
 428          $result = mysql_query($query)
 429              or die_with_mysql_error("Rating recalculation failed");
 430  
 431          $row = mysql_fetch_array($result);
 432  
 433          $avg = (round(100 * $row[0])) / 100.0;
 434  
 435          $query = "update " . DB_PREFIX . "photos set rating = $avg" .
 436              " where photo_id = '" . escape_string($photo_id) . "'";
 437  
 438          if (DEBUG > 1) { echo "$query<br>\n"; }
 439  
 440          $result = mysql_query($query)
 441              or die_with_mysql_error("Inserting new rating failed");
 442  
 443          return $avg;
 444      }
 445  
 446      function get_image_resource() {
 447          $file = $this->get_file_path();
 448          $img_src = null;
 449          $image_info = getimagesize($file);
 450          switch ($image_info[2]) {
 451              case 1:
 452                  $img_src = imagecreatefromgif($file);
 453                  break;
 454              case 2:
 455                  $img_src = imagecreatefromjpeg($file);
 456                  break;
 457              case 3:
 458                  $img_src = imagecreatefrompng($file);
 459                  break;
 460              default:
 461                  break;
 462          }
 463  
 464          return $img_src;
 465      }
 466  
 467      function thumbnail($img_src = null) {
 468          return
 469              $this->create_thumbnail(THUMB_PREFIX, THUMB_SIZE, $img_src) &&
 470              $this->create_thumbnail(MID_PREFIX, MID_SIZE, $img_src);
 471      }
 472  
 473      function create_thumbnail($prefix, $size, $img_src) {
 474          $destroy = false;
 475          if ($img_src == null) {
 476              $img_src = $this->get_image_resource();
 477              $destroy = true;
 478          }
 479  
 480          $image_info = getimagesize($this->get_file_path());
 481          $width = $image_info[0];
 482          $height = $image_info[1];
 483  
 484          if ($width >= $height) {
 485              $new_width = $size;
 486              $new_height = round(($new_width / $width) * $height);
 487          }
 488          else {
 489              $new_height = $size;
 490              $new_width = round(($new_height / $height) * $width);
 491          }
 492  
 493          $img_dst = imagecreatetruecolor($new_width, $new_height);
 494          imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0,
 495              $new_width, $new_height, $width, $height);
 496  
 497          $new_image = IMAGE_DIR . $this->get("path") . '/' . $prefix . '/' .
 498              $prefix . '_' .  get_converted_image_name($this->get("name"));
 499  
 500          $image_type = get_image_type($new_image);
 501  
 502          // a little fast a loose but usually ok
 503          $func = "image" . substr($image_type, strpos($image_type, '/') + 1);
 504  
 505          $return = 1;
 506          if (!$func($img_dst, $new_image)) {
 507              $return = 0;
 508          }
 509  
 510          imagedestroy($img_dst);
 511  
 512          if ($destroy) {
 513              imagedestroy($img_src);
 514          }
 515  
 516          return $return;
 517      }
 518  
 519      function rotate($deg) {
 520  /*
 521          This line breaks things if dated-dirs are not used: in that case the path field is empty...
 522          if (!ALLOW_ROTATIONS || !$this->get('name') || !$this->get('path')) {
 523              return;
 524          }
 525  */
 526          if (!ALLOW_ROTATIONS || !$this->get('name')) {
 527              return;
 528          }
 529  
 530          $dir = IMAGE_DIR . $this->get("path") . "/";
 531          $name = $this->get('name');
 532          $converted_name = get_converted_image_name($name);
 533  
 534          $images[$dir . THUMB_PREFIX . '/' . THUMB_PREFIX . '_' . 
 535              $converted_name] = 
 536              $dir . THUMB_PREFIX . '/rot_' . THUMB_PREFIX . '_' . 
 537              $converted_name;
 538  
 539          $images[$dir . MID_PREFIX . '/' . MID_PREFIX . '_' . $converted_name] =
 540              $dir . MID_PREFIX . '/rot_' . MID_PREFIX . '_' . $converted_name;
 541  
 542          $images[$dir . $name] = $dir . 'rot_' . $name;
 543  
 544          if (BACKUP_ORIGINAL) {
 545              $backup_name = BACKUP_PREFIX . $name;
 546  
 547              // file_exists() check from From Michael Hanke:
 548              // Once a rotation had occurred, the backup file won't be
 549              // overwritten by future rotations and the original file
 550              // is always preserved.
 551              if (!file_exists($dir . $backup_name)) {
 552                  if (!copy($dir . $name, $dir . $backup_name)) {
 553                      echo sprintf(translate("Could not copy %s to %s."), $name, $backup_name) . "<br>\n";
 554                      return;
 555                  }
 556              }
 557          }
 558  
 559          // make a system call to convert or jpegtran to do the rotation.
 560          // in the future, use PHP's imagerotate() function,
 561          // but it only appears >= 4.3.0 (and is buggy at the moment)
 562          while (list($file, $tmp_file) = each($images)) {
 563  
 564              /*
 565                From Michael Hanke:
 566                This is buggy, because non-quadratic images are truncated
 567                The function goodrotate checks if images are nonquadratic
 568  
 569                This is not being used because, as Michael says,
 570  
 571                "I haven't found a reasonable way to preserve the exif-data
 572                 stored in the original jpeg file. imagejpeg() (the gd
 573                 function) doesn't write it into the exported image file.
 574                 ... I propose to stick to 'convert' which keeps the exif
 575                 metadata as it is."
 576  
 577                $imrot = @imagecreatefromjpeg($file);
 578                $new_image = $this->goodrotate($imrot, $deg);
 579                imagejpeg($new_image, $tmp_file, 95);
 580              */
 581  
 582              $cmd = ROTATE_CMD;
 583              if (strpos(" $cmd", 'jpegtran')) {
 584                  $cmd .= ' -copy all -rotate ' .  escapeshellarg($deg) .
 585                      ' -outfile ' .  escapeshellarg($tmp_file) . ' ' .
 586                      escapeshellarg($file);
 587              }
 588              else if (strpos(" $cmd", 'convert')) {
 589                  $cmd .= ' -rotate ' . escapeshellarg($deg) . ' ' .
 590                      escapeshellarg($file) . ' ' . escapeshellarg($tmp_file);
 591              }
 592  
 593              $cmd .= ' 2>&1';
 594  
 595              //echo "$cmd<br>\n";
 596              $output = system($cmd);
 597  
 598              if ($output) { // error
 599                  echo translate("An error occurred.") . " $output<br>\n";
 600                  continue; // or return;
 601              }
 602  
 603              if (!rename($tmp_file, $file)) {
 604                  echo sprintf(translate("Could not rename %s to %s."), $tmp_file, $file) . "<br>\n";
 605                  continue; // or return;
 606              }
 607  
 608          }
 609  
 610          // update the size and dimensions
 611          // (only if original was rotated)
 612          $file = $dir . $name;
 613          $size = filesize($file);
 614          $dimensions = getimagesize($file);
 615          $this->set('size', $size);
 616          $this->set('width', $dimensions[0]);
 617          $this->set('height', $dimensions[1]);
 618          $this->update();
 619  
 620          return 1;
 621      }
 622  
 623      /*
 624       * Creates a jpeg photo with
 625       * text annotation at the bottom.
 626       *
 627       * Copyright 2003, Nixon P. Childs
 628       * License: The same as the rest of Zoph.
 629       */
 630      function annotate($vars, $user, $size = 'mid') {
 631          if ($vars['_size']) {
 632              $size = $vars['_size'];
 633          }
 634  
 635          if ($size == 'mid') {
 636              $font = 4;
 637              $padding = 2;
 638              $indent = 8;
 639          }
 640          else if ($size == 'full') {
 641              $font = 5;
 642              $padding = 2;
 643              $indent = 8;
 644          }
 645          else {
 646              return '';
 647          }
 648  
 649          /* ********************************
 650           *  Read in original image.
 651           *  Need to do now so we know
 652           *  the width of the text lines.
 653           * ********************************/
 654  
 655          $image_path = IMAGE_DIR . $this->get("path");
 656          if ($size == 'full') {
 657              $image_path .= "/" . $this->get("name");
 658          }
 659          else {
 660              $image_path .= "/" . $size . "/" . $size . "_" . $this->get("name");
 661          }
 662  
 663          $image_info = getimagesize($image_path);
 664          switch ($image_info[2]) {
 665              case 1:
 666                  $orig_image = imagecreatefromgif($image_path);
 667                  break;
 668              case 2:
 669                  $orig_image = imagecreatefromjpeg($image_path);
 670                  break;
 671              case 3:
 672                  $orig_image = imagecreatefrompng($image_path);
 673                  break;
 674              default:
 675                  if (DEBUG) { echo "Unsupported image type."; }
 676                  return '';
 677          }
 678  
 679          $row = ImageSY($orig_image) + ($padding/2);
 680          $maxWidthPixels = ImageSX($orig_image) - (2 * $indent);
 681          $maxWidthChars = floor($maxWidthPixels / ImageFontWidth($font)) - 1;
 682  
 683          /*
 684           * Sets fields from the given array.  Can be used to set vars
 685           * directly from a GET or POST.
 686           */
 687          reset($vars);
 688          $lines = 0;
 689          while (list($key, $val) = each($vars)) {
 690  
 691              // ignore empty keys or values
 692              if (empty($key) || $val == "") { continue; }
 693  
 694              if (strcmp(Substr($key, strlen($key) - 3), "_cb") == 0) {
 695  
 696                  /* *****************************************
 697                   *  Everthing else uses the checkbox name
 698                   *  as the "get" key.
 699                   * *****************************************/
 700  
 701                  $real_key = Substr($key, 0, strlen($key) - 3);
 702                  $real_val = $vars[$real_key];
 703                  remove_magic_quotes($real_val);
 704  
 705                  /* *****************************************
 706                   *  Have to handle title separately because
 707                   *  breadcrumbs.inc.php assumes title is
 708                   *  the page title.
 709                   * *****************************************/
 710  
 711                  if ($real_key == "photo_title") {
 712                     $real_key = "title";
 713                  }
 714                  else if ($real_key == "extra") {
 715                     $real_key = $vars["extra_name"];
 716                     remove_magic_quotes($real_key);
 717                  }
 718  
 719                  $out_array[$real_key] = translate($real_key, 0) . ": " .
 720                      $real_val;
 721                  $lines += ceil(strlen($out_array[$real_key]) / $maxWidthChars);
 722              }
 723          }
 724  
 725          /* **********************************************
 726           *  Create Image
 727           *  In order to create the text area, we must
 728           *  first create the text and determine how much
 729           *  space it requires.
 730           *
 731           *  I tried implode;wordwrap;explode, but
 732           *  wordwrap doesn't respect \n's in the text.
 733           *  To complicate things, ImageString just
 734           *  renders \n as an upside-down Y.
 735           *
 736           *  So the current solution is a little awkward,
 737           *  but it works.  The only (known) problem is
 738           *  that wrapped lines don't have the same
 739           *  right margin as non-wrapped lines.  This is
 740           *  because wordwrap doesn't take into account
 741           *  the line separation string.
 742           * **********************************************/
 743  
 744  
 745          /*
 746          $tmpString = implode("\n", $out_array);
 747  echo ("tmpString:<br>\n" . $tmpString);
 748          $out_string = wordwrap($tmpString, floor($maxWidthPixels / ImageFontWidth($font)) - 1, "\n     ");
 749  echo ("<br>\noutString:<br>\n" . $out_string);
 750          $formatted_array = explode("\n", $out_string);
 751          $lines = sizeof($formatted_array);
 752          */
 753  
 754          $count = 0;
 755          array($final_array);
 756          if ($out_array) {
 757              while (list($key, $val) = each($out_array)) {
 758                  $tmp_array = explode("\n", wordwrap($val, $maxWidthChars, "\n   "));
 759                  while (list($key1, $val1) = each($tmp_array)) {
 760                      $final_array[$count++] = $val1;
 761                  }
 762              }
 763          }
 764  
 765          $noted_image = ImageCreateTrueColor (ImageSX($orig_image), ImageSY($orig_image) + ((ImageFontHeight($font) + $padding) * $count));
 766          $white = ImageColorAllocate($noted_image, 255,255, 255);
 767  
 768          /* ********************************
 769           *  Use a light grey background to
 770           *  hide the jpeg artifacts caused
 771           *  by the sharp edges in text.
 772           * ******************************/
 773  
 774          $offwhite = ImageColorAllocate($noted_image, 240,240, 240);
 775          ImageFill($noted_image, 0, ImageSY($orig_image) +1, $offwhite);
 776          $black = ImageColorAllocate($noted_image, 0, 0, 0);
 777          ImageColorTransparent($noted_image, $black);
 778  
 779          ImageCopy($noted_image, $orig_image, 0, 0, 0, 0, ImageSX($orig_image), ImageSY($orig_image));
 780  
 781          if ($final_array) {
 782              while (list($key, $val) = each($final_array)) {
 783                  ImageString ($noted_image, $font, $indent, $row, $val, $black);
 784                  $row += ImageFontHeight($font) + $padding;
 785              }
 786          }
 787  
 788          /*
 789          while (list($key, $val) = each($out_array)) {
 790              ImageStringWrap ($noted_image, $font, $padding, $row, $val, $black, $maxWidthPixels);
 791              $row += ImageFontHeight($font) + $padding;
 792              //echo ($val . "<br>");
 793          }
 794          */
 795  
 796          //$rnd_name = rand(1, 10000);
 797          //$temp_name = "zoph" . $user->get("user_id") . "_" . $rnd_name . $photo->get("name");
 798  
 799          $temp_name = $this->get_annotated_file_name($user);
 800          ImageJPEG($noted_image, ANNOTATE_TEMP_DIR . "/" . $temp_name);
 801          ImageDestroy($orig_image);
 802          ImageDestroy($noted_image);
 803  
 804          return $temp_name;
 805      }
 806  
 807      function get_annotated_file_name($user) {
 808          return ANNOTATE_TEMP_PREFIX . $user->get("user_id") . "_" . $this->get("name");
 809      }
 810  
 811      function get_display_array() {
 812          return array(
 813              translate("title") => $this->get("title"),
 814              translate("location") => $this->location
 815                  ? $this->location->get_link() : "",
 816              translate("view") => $this->get("view"),
 817              translate("date") => create_date_link($this->get("date")),
 818              translate("time") => $this->get("time"),
 819              translate("photographer") => $this->photographer
 820                  ? $this->photographer->get_link() : ""
 821          );
 822      }
 823  
 824      function get_email_array() {
 825          return array(
 826              translate("title") => $this->get("title"),
 827              translate("location") => $this->location
 828                  ? $this->location->get("title") : "",
 829              translate("view") => $this->get("view"),
 830              translate("date") => $this->get("date"),
 831              translate("time") => $this->get("time"),
 832              translate("photographer") => $this->photographer
 833                  ? $this->photographer->get_name() : "",
 834              translate("description") => $this->get("description")
 835          );
 836      }
 837  
 838      function get_camera_display_array() {
 839          return array(
 840              translate("camera make") => $this->get("camera_make"),
 841              translate("camera model") => $this->get("camera_model"),
 842              translate("flash used") => $this->get("flash_used"),
 843              translate("focal length") => $this->get("focal_length"),
 844              translate("exposure") => $this->get("exposure"),
 845              translate("aperture") => $this->get("aperture"),
 846              translate("compression") => $this->get("compression"),
 847              translate("iso equiv") => $this->get("iso_equiv"),
 848              translate("metering mode") => $this->get("metering_mode"),
 849              translate("focus distance") => $this->get("focus_dist"),
 850              translate("ccd width") => $this->get("ccd_width"),
 851              translate("comment") => $this->get("comment"));
 852      }
 853  
 854      function get_edit_array() {
 855          return array(
 856              "Title" => create_text_input("title", $this->title),
 857              "Date" => create_text_input("date", $this->date_taken),
 858              "Photographer" => create_text_input("photographer",
 859                  $this->photographer ? $this->photographer->get_name() : ""),
 860              "Location" => create_text_input("location",
 861                  $this->location ? $this->location->get_name() : ""),
 862              "View" => create_text_input("view", $this->view),
 863              "Level" => create_text_input("level", $this->level, 4, 2));
 864      }
 865  
 866  }
 867  
 868  function get_photo_sizes_sum() {
 869      $sql = "select sum(size) from " . DB_PREFIX . "photos";
 870      return get_count_from_query($sql);
 871  }
 872  
 873  function create_rating_graph($user) {
 874  
 875      if ($user && !$user->is_admin()) {
 876          $query =
 877              "select round(ph.rating), count(distinct ph.photo_id) as count from " .
 878              DB_PREFIX . "photos as ph, " .
 879              DB_PREFIX . "photo_albums as pa, " .
 880              DB_PREFIX . "album_permissions as ap " .
 881              "where ap.user_id = '" . escape_string($user->get("user_id")) . "'" .
 882              " and ap.album_id = pa.album_id" .
 883              " and pa.photo_id = ph.photo_id" .
 884              " and ap.access_level >= ph.level " .
 885              "group by round(rating) order by round(rating)";
 886      }
 887      else {
 888          $query =
 889              "select round(rating), count(*) from " . DB_PREFIX . "photos " .
 890              "group by round(rating) order by round(rating)";
 891      }
 892  
 893      if (DEBUG) { echo "$query<br>\n"; }
 894  
 895      $result = mysql_query($query)
 896          or die_with_mysql_error("Rating grouping failed");
 897  
 898      $max_count = 0;
 899      while ($row = mysql_fetch_array($result)) {
 900          $max_count = max($max_count, $row[1]);    
 901          $ratings[($row[0] ? $row[0] : translate("Not rated"))]=$row[1];
 902      }
 903  
 904      if ($max_count) { 
 905      $table =
 906          "<table class=\"ratings\">\n  <tr>\n    <th colspan=\"3\"><h3>" .
 907          translate("photo ratings") . "</h3></th>\n  </tr>\n  <tr>\n    <th>" .
 908          translate("rating") . "</th>\n    <th>&nbsp;</th>\n    " .
 909          "<th>" . translate("count") . "</th>\n  </tr>\n";
 910  
 911      $scale = 150.0 / $max_count;
 912      
 913      while (list($range, $count) = each($ratings)) {
 914          if($range>0) {
 915         $min_rating=$range-0.5;
 916         $max_rating=$range+0.5;
 917             $qs =
 918                "search.php?rating%5B0%5D=" . $min_rating . "&_rating_op%5B0%5D=%3E%3D" .
 919                "&rating%5B1%5D=" . $max_rating . "&_rating_op%5B1%5D=%3C&_action=search";
 920          } else {
 921             $qs = "photos.php?rating=null";
 922          }  
 923          $table .=
 924              "  <tr>\n    <td>\n" .
 925              "      <a href=\"$qs\">$range</a></td>\n" .
 926              "    <td>&nbsp;</td>\n    <td>\n";
 927  
 928      $table .= "<div class=\"ratings\" style=\"width: " . ceil($scale * $count) . "px;\">&nbsp;</div>";
 929          $table .= "[$count]\n    </td>\n  </tr>\n";
 930      }
 931  
 932      $table .="</table>\n";
 933  
 934      }
 935      else {
 936          $table .=
 937              "  <tr>\n    <td colspan=\"2\" class=\"center\">\n" .
 938              translate("No photo was found.") . "\n    </td>\n  </tr>\n";
 939      }
 940  
 941      return $table;
 942  }
 943  
 944  /*
 945   * Rotates (non-quadratic) images correctly using imagerotate().
 946   * It is currently not being used because it apparently does not
 947   * preserve exif info.
 948   *
 949   * This function provided by Michael Hanke, who found it on php.net.
 950   *
 951   *
 952   * (c) 2002 php at laer dot nu
 953   * Function to rotate an image
 954   */
 955  function goodrotate($src_img, $degrees = 90) {
 956      // angles = 0°
 957      $degrees %= 360;
 958      if($degrees == 0) {
 959          $dst_img = $src_image;
 960      } Elseif ($degrees == 180) {
 961          $dst_img = imagerotate($src_img, $degrees, 0);
 962      } Else {
 963          $width = imagesx($src_img);
 964          $height = imagesy($src_img);
 965          if ($width > $height) {
 966             $size = $width;
 967          } Else {
 968             $size = $height;
 969          }
 970          $dst_img = imagecreatetruecolor($size, $size);
 971          imagecopy($dst_img, $src_img, 0, 0, 0, 0, $width, $height);
 972          $dst_img = imagerotate($dst_img, $degrees, 0);
 973          $src_img = $dst_img;
 974          $dst_img = imagecreatetruecolor($height, $width);
 975          if ((($degrees == 90) && ($width > $height)) || (($degrees == 270) && ($width < $height))) {
 976              imagecopy($dst_img, $src_img, 0, 0, 0, 0, $size, $size);
 977          }
 978          if ((($degrees == 270) && ($width > $height)) || (($degrees == 90) && ($width < $height))) {
 979              imagecopy($dst_img, $src_img, 0, 0, $size - $height, $size - $width, $size, $size);
 980          }
 981      }
 982      return $dst_img;
 983  }
 984  
 985  /*
 986   * For Nixon Childs' annotate function.
 987   *
 988   * Shamelessly stolen from the php.net comment board.
 989   */
 990  function ImageStringWrap($image, $font, $x, $y, $text, $color, $maxwidth) {
 991      $fontwidth = ImageFontWidth($font);
 992      $fontheight = ImageFontHeight($font);
 993  
 994      if ($maxwidth != NULL) {
 995          $maxcharsperline = floor($maxwidth / $fontwidth);
 996          $text = wordwrap($text, $maxcharsperline, "\n", 1);
 997      }
 998  
 999      $lines = explode("\n", $text);
1000      while (list($numl, $line) = each($lines)) {
1001          ImageString($image, $font, $x, $y, $line, $color);
1002          $y += $fontheight;
1003      }
1004  }
1005  
1006  ?>


[ Powered by PHPXref - Served by Debian GNU/Linux ]