Location: PHPKode > projects > Jarida > jarida_1.0/classes/Photo.php
<?php
                                                                                                                       
/*
                                                                                                                             
-------------------------------------------------------------------------
                                                                                                                             
Photo Class
      A class to be used to handle Photos.
                                                                                                                             
-------------------------------------------------------------------------
                                                                                                                             
Developer
      Name -- Haddad Said.
      Date -- 30-07-2005
      Version -- 1.0
                                                                                                                             
-------------------------------------------------------------------------
                                                                                                                             
Member Functions
      2.edit
      3.delete
      5.setTitle
      6.getTitle
      7.setFile
      8.getFile
      11.setDescription
      12.getDescription
      13.setThumb
      14.getThumb
      15.getNumComments
      16.getComments
    

-------------------------------------------------------------------------
                                                                                                                             
*/


class Photo extends Entry
{

  //Vars to hold attributes of the pic

  var $title;
  var $file;
  var $description;
  var $thumb;


  /*

-------------------------------------------------------------------------
Class Constructor
      
Parameters:
      &db
      id
     

Notes:
      

Example Usage:
     
-------------------------------------------------------------------------

  */

  function Photo(&$db, $id)
  {
  
    parent::Entry($db, $id);
   
    $query = "SELECT photo_title, photo_description, photo_filename FROM tblPhoto WHERE photo_id = " . $this->getId();

    $this->result = $this->db->doQuery($query);

    $row = $this->result->getArray();

    $this->setTitle($row['photo_title']);
    $this->setDescription($row['photo_description']);
    $this->setFile($row['photo_filename']);
      
  }


  /*

-------------------------------------------------------------------------
Function edit
      
Parameters:
      

Notes:
      

Example Usage:

-------------------------------------------------------------------------

  */

  function edit()
  {
    $query = "UPDATE tblPhoto SET photo_title = '" . $this->getTitle() . "', photo_description = '" . $this->getDescription() . "'
              WHERE photo_id = '" . $this->getId() . "'";

    if($this->result = $this->db->doQuery($query))
      {
		return true;
      }
    else
      {
		return false;
      }
  }


  /*

-------------------------------------------------------------------------
Function delete
      
Parameters:
      

Notes:
      

Example Usage:

-------------------------------------------------------------------------

  */

  function delete()
  {
    unlink("../img/upload/" . $this->getFile());
    unlink("../img/upload/" . $this->getThumbnail());

    return(parent::delete("tblPhoto", "photo_id"));
  }



  /*

-------------------------------------------------------------------------
Function setTitle
      
Parameters:
      title

Notes:
      

Example Usage:

-------------------------------------------------------------------------

  */

  function setTitle($title)
  {
    $this->title = $title;
  }


  /*

-------------------------------------------------------------------------
Function getTitle
      
Parameters:
      

Notes:
      

Example Usage:

-------------------------------------------------------------------------

  */

  function getTitle()
  {
    return $this->title;
  }



  /*

-------------------------------------------------------------------------
Function setDescription
      
Parameters:
      description

Notes:
      

Example Usage:

-------------------------------------------------------------------------

  */

  function setDescription($description)
  {
    $this->description = $description;
  }



  /*

-------------------------------------------------------------------------
Function getDescription
      
Parameters:
      

Notes:
      

Example Usage:

-------------------------------------------------------------------------

  */


  function getDescription()
  {
    return $this->description;
  }




  /*

-------------------------------------------------------------------------
Function setFile
      
Parameters:
      file

Notes:
      

Example Usage:

-------------------------------------------------------------------------

  */

  function setFile($file)
  {
    $this->file = $file;
  }



  /*

-------------------------------------------------------------------------
Function getFile
      
Parameters:
      

Notes:
      

Example Usage:

-------------------------------------------------------------------------

  */


  function getFile()
  {
    return $this->file;
  }



  /*

-------------------------------------------------------------------------
Function setThumbnail
      
Parameters:
      

Notes:
      

Example Usage:

-------------------------------------------------------------------------

  */


  function setThumbnail()
  {

    //Create an array of accepted image files
    $img_types = array('image/pjpeg' => 'jpeg',
		       'image/jpeg' => 'jpeg',
		       'image/gif' => 'gif',
		       'image/bmp' => 'wbmp',
		       'image/x-png' => 'png',
		       'image/png' => 'png');

    $size = getimagesize("../img/upload/" . $this->getFile());

    $thumbnail_width = 100;  

    $thumbnail_height = (int)(100 * $size[1] / $size[0]);  

    $filetype = $_FILES['file']['type'];
    $function_suffix = $img_types[$filetype]; 
    $function_to_read = 'imagecreatefrom' . $function_suffix; 
    $function_to_write = 'image' . $function_suffix;

    // Read the source file
    $source_handle = $function_to_read("../img/upload/" . $this->getFile());

    if($source_handle) 
      {
	// Let's create a blank image for the thumbnail
	$destination_handle = imagecreatetruecolor($thumbnail_width, $thumbnail_height);

	// Now we resize it
	imagecopyresampled($destination_handle, $source_handle, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $size[0], $size[1]);

      }

    // Let's save the thumbnail
    $function_to_write($destination_handle, "../img/upload/tb_" . $this->getFile());

  }



  /*

-------------------------------------------------------------------------
Function getThumbnail
      
Parameters:
      

Notes:
      

Example Usage:

-------------------------------------------------------------------------

  */


  function getThumbnail()
  {
    return "tb_" . $this->getFile();
  }




  /*

-------------------------------------------------------------------------
Function getNumComments
      
Parameters:
      

Notes:
      

Example Usage:

-------------------------------------------------------------------------

  */


  function getNumComments()
  {
    $query = "SELECT comment_id FROM tblPhotoComment WHERE 
               comment_photo_id = " . $this->getId();

    $this->result = $this->db->doQuery($query);

    return $this->result->getNumRows();
  }



  /*

-------------------------------------------------------------------------
Function getComments
      
Parameters:
      

Notes:
      

Example Usage:

-------------------------------------------------------------------------

  */


  function getComments()
  {
    $query = "SELECT comment_id FROM tblPhotoComment WHERE 
               comment_photo_id = " . $this->getId() . " ORDER BY comment_id ASC";

    $this->result = $this->db->doQuery($query);
    while($row = $this->result->getArray())
      {
	$comments[] = new PhotoComment($this->db, $row['comment_id']);
      }

    return $comments;
  }




}



?>
Return current item: Jarida