class Gallery
{
private $id;
private $event_id;
private $path;
private $thumbnail_path;
private $name;
private $desc;
private $type;
public function __construct($params=null)
{
if($params)
{
if($params['id'])
{
$this->id = $params['id'];
}
$this->event_id = $params['event_id'];
$this->path = $params['path'];
$this->name = $params['name'];
$this->desc = $params['desc'];
if($params['type'])
$this->type = $params['type'];
$this->thumbnail_path = $params['thumbnail_path'];
}
}
private function create_thumbnail()
{
global $web_root;
global $event_gallery_path;
global $event_gallery_thumbnail_path;
$userfile_type = $this->type;
echo $userfile_type . "= type
";
$size1 = 150;
$size2 = 260;
$upfile = "$web_root/$event_gallery_path/$this->path";
$size = GetImageSize($upfile);
echo $upfile . "
";
$path_parts = pathinfo(basename($upfile));
$fext = $path_parts['extension'];
// Wide Image
if($size[0] > $size[1])
{
$thumbnail_width = $size1;
$thumbnail_height = (int)($size1 * $size[1] / $size[0]);
}
else // Tall Image
{
$thumbnail_width = (int)($size1 * $size[0] / $size[1]);
$thumbnail_height = $size1;
}
$gd_function_suffix = array(
'image/pjpeg' => 'jpeg',
'image/jpeg' => 'jpeg',
'image/jpg' => 'jpg',
'image/gif' => 'gif',
'image/bmp' => 'wbmp',
'image/x-png' => 'png',
'image/png' => 'png'
);
// Get the Name Suffix on basis of the mime type
$function_suffix = $gd_function_suffix[$userfile_type];
// Build Function name for ImageCreateFromSUFFIX
$function_to_read = 'ImageCreateFrom' . $function_suffix;
// Build Function name for ImageSUFFIX
$function_to_write = 'Image' . $function_suffix;
$function_suffix = $gd_function_suffix[$userfile_type];
$function_to_read = 'ImageCreateFrom' . $function_suffix;
echo "ICF $function_to_read
";
$function_to_write = 'Image' . $function_suffix;
// Read the source file
$source_handle = $function_to_read($upfile);
if ($source_handle)
{
// Let's create a blank image for the thumbnail
$destination_handle = imagecreatetruecolor($thumbnail_width, $thumbnail_height) ;
if(! $destination_handle)
{
echo "Failed\n";
}
// Now we resize it
if(!ImageCopyResized($destination_handle, $source_handle,
0, 0, 0, 0, $thumbnail_width, $thumbnail_height,
$size[0], $size[1]))
{
echo "failed 2";
}
}
// Let's save the thumbnail
$function_to_write($destination_handle, "$web_root/$event_gallery_thumbnail_path/$this->path");
ImageDestroy($destination_handle);
$this->thumbnail_path = $this->path;
}
public static function find($id = null,$start)
{
if($id)
{
$sql = "SELECT * FROM event_gallery WHERE id=$id";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
return new Gallery($row);
}
else
{
$sql = "select * from event_gallery order by id limit $start,10";
$galleries = array();
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result))
{
$galleries[] = new Gallery($row);
}
}
return $galleries;
}
public static function find_gallery($event_id)
{
$sql = "SELECT * FROM event_gallery WHERE event_id = '".$event_id."' order by id ";
$galleries = array();
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result))
{
$galleries[] = new Gallery($row);
}
return $galleries;
}
public function save()
{
$this->create_thumbnail();
$sql = "INSERT INTO event_gallery(`name`,`event_id`,`path`,`thumbnail_path`,`desc`)VALUES('$this->name','$this->event_id','$this->path','$this->thumbnail_path','$this->desc')";
mysql_query($sql) or die(mysql_error());
}
public function edit($id)
{
$this->create_thumbnail();
$sql = "UPDATE event_gallery SET `name`='$this->name', `event_id`='$this->event_id', `path`='$this->path',`thumbnail_path`='$this->thumbnail_path',`desc`='$this->desc' WHERE `id`='$id'";
mysql_query($sql) or die(mysql_error());
}
public function delete()
{
global $web_root;
global $event_path;
global $event_thumbnail_path;
$sql = "DELETE FROM event_gallery where id=$this->id" ;
mysql_query($sql);
unlink("$web_root/$event_gallery_path/$this->path");
unlink("$web_root/$event_gallery_thumbnail_path/$this->path");
}
public static function check_gallery($id)
{
$sql = "SELECT * FROM event_gallery WHERE event_id=$id";
$result = mysql_query($sql);
if(mysql_fetch_assoc($result))
return true;
else
return false;
# return new Gallery($row);
}
public function id()
{
return $this->id;
}
public function path()
{
return "$web_root/$event_gallery_path/$this->path";
}
public function thumbnail_path()
{
return "$web_root/$event_gallery_thumbnail_path/$this->thumbnail_path";
}
public function thumbnail_url()
{
global $web_root;
global $event_gallery_thumbnail_path;
return $event_gallery_thumbnail_path."/".$this->thumbnail_path;
}
public function image_url()
{
global $web_root;
global $event_gallery_path;
return "$event_gallery_path/$this->thumbnail_path";
}
public function name()
{
return $this->name;
}
public function desc()
{
return $this->desc;
}
public static function count()
{
$sql = "SELECT count(id) AS gallery_count from event_gallery";
$result = mysql_query($sql);
echo mysql_error();
$row = mysql_fetch_assoc($result);
return $row['gallery_count'];
}
}
?>