Location: PHPKode > scripts > Photos > photos/objects/class.image.inc.php
<?php

// This file is part of Photos
// Copyright (c) 2001-2004 Alex King
// see LICENSE.txt for more information

class image {

/*******************************************************************
	* class image
	*
	* Methods:
	*	image() - initialize variables
	*	clean() - removes slashes, returns image
	*	store() - insert new image in database
	*	update($thumbnail = 0) - if $thumbnail = 1, create new thumbnail for the image and include it in the update 
	*	retrieve($id = $this->$id) - retrieve an image, returns image w/ all properties
	*	create_thumbnail($URL = $this->URL, $size = 160) - create a thumbnail, returns $thumbnail
	*	get_orientation($URL = $this->URL) - is photo vertical (1) or horizontal (0)
	*
	*/
	
	var $country;
	var $dateentered;
	var $datetaken;
	var $description;
	var $display_URL;			// used to display temp image
	var $event;
	var $id;
	var $indoors;
	var $location;
	var $keywords;
	var $needdetails;
	var $orientation;
	var $people;
	var $photographer;
	var $portfolio;
	var $state;
	var $thumbnail;
	var $title;
	var $URL;

// new in 3.5

	var $camera_make;
	var $camera_model;
	var $flash;
	var $fnumber;
	var $focal_length;
	var $height;
	var $ISO;
	var $shutter;
	var $width;
	
	function image() { // intializes variables
		$this->camera_make = "";
		$this->camera_model = "";
		$this->country = "";
		$this->dateentered = "";
		$this->datetaken = "";
		$this->description = "";
		$this->display_URL = "";
		$this->event = "";
		$this->flash = 0;
		$this->fnumber = "";
		$this->focal_length = "";
		$this->height = "";
		$this->id = "";
		$this->indoors = "";
		$this->ISO = "";
		$this->keywords = "";
		$this->location = "";
		$this->needdetails = 0;
		$this->orientation = 0;
		$this->people = "";
		$this->photographer = "";
		$this->portfolio = 0;
		$this->shutter = "";
		$this->state = "";
		$this->thumbnail = "";
		$this->title = "";
		$this->URL = "";
		$this->width = "";
	}
	
	function clean() { // removes slashes from text values
		$this->title = stripslashes($this->title);
		$this->description = stripslashes($this->description);
		$this->keywords = stripslashes($this->keywords);
		$this->people = stripslashes($this->people);
		$this->event = stripslashes($this->event);
		$this->location = stripslashes($this->location);
		$this->state = stripslashes($this->state);
		$this->photographer = stripslashes($this->photographer);
		$this->URL = stripslashes($this->URL);
		
		return $this;
	}

	function add_slashes() { // add slashes to text values
		$this->title = addslashes($this->title);
		$this->description = addslashes($this->description);
		$this->keywords = addslashes($this->keywords);
		$this->people = addslashes($this->people);
		$this->event = addslashes($this->event);
		$this->location = addslashes($this->location);
		$this->state = addslashes($this->state);
		$this->photographer = addslashes($this->photographer);
		$this->URL = addslashes($this->URL);
		
		return $this;
	}

	function convert_quotes_to_HTML() { 
		$this->title = str_replace('"', '&quot;', $this->title);
		$this->description = str_replace('"', '&quot;', $this->description);
		$this->keywords = str_replace('"', '&quot;', $this->keywords);
		$this->people = str_replace('"', '&quot;', $this->people);
		$this->event = str_replace('"', '&quot;', $this->event);
		$this->location = str_replace('"', '&quot;', $this->location);
		$this->state = str_replace('"', '&quot;', $this->state);
		$this->photographer = str_replace('"', '&quot;', $this->photographer);
		
		return $this;
	}

	function store() { // inserts a new image into the database
		global $database;
    	$query = "INSERT INTO $database->table_name ( "
    	        ."title, "
    	        ."description, "
    	        ."keywords, "
    	        ."people, "
    	        ."event, "
    	        ."location, "
    	        ."state, "
    	        ."country, "
    	        ."indoors, "
    	        ."URL, "
    	        ."datetaken, "
    	        ."thumbnail, "
    	        ."needdetails, "
    	        ."portfolio, "
    	        ."orientation, "
    	        ."photographer, "
    	        ."camera_make, "
    	        ."camera_model, "
    	        ."flash, "
    	        ."fnumber, "
    	        ."focal_length, "
    	        ."height, "
    	        ."ISO, "
    	        ."width, "
    	        ."shutter "
    	        .") "
    	        ."VALUES ( "
    	        ."'$this->title', "
    	        ."'$this->description', "
    	        ."'$this->keywords', "
    	        ."'$this->people', "
    	        ."'$this->event', "
    	        ."'$this->location', "
    	        ."'$this->state', "
    	        ."'$this->country', "
    	        ."'$this->indoors', "
    	        ."'".addslashes($this->URL)."', "
    	        ."'$this->datetaken', "
    	        ."'$this->thumbnail', "
    	        ."'$this->needdetails', "
    	        ."'$this->portfolio', "
    	        ."'$this->orientation', "
    	        ."'$this->photographer', "
    	        ."'$this->camera_make', "
    	        ."'$this->camera_model', "
    	        ."'$this->flash', "
    	        ."'$this->fnumber', "
    	        ."'$this->focal_length', "
    	        ."'$this->height', "
    	        ."'$this->ISO', "
    	        ."'$this->width', "
    	        ."'$this->shutter' "
    	        .")";
		mysql_query($query)
			or die("error: ".mysql_error());
	}
	
	function update($thumbnail = 0) { // updates an existing image
		global $database;
		if ($thumbnail == 1) {
			$this->thumbnail = $this->create_thumbnail();
			$query = "UPDATE $database->table_name SET "
    	            ."title='$this->title', "
    	            ."description='$this->description', "
    	            ."keywords='$this->keywords', "
    	            ."people='$this->people', "
    	            ."event='$this->event', "
    	            ."location='$this->location', "
    	            ."state='$this->state', "
    	            ."country='$this->country', "
    	            ."indoors='$this->indoors', "
    	            ."URL='".addslashes($this->URL)."', "
    	            ."datetaken='$this->datetaken', "
    	            ."thumbnail='$this->thumbnail', "
    	            ."needdetails='$this->needdetails', "
    	            ."portfolio='$this->portfolio', "
    	            ."orientation='$this->orientation', "
    	            ."photographer='$this->photographer', "
					."camera_make='$this->camera_make', "
					."camera_model='$this->camera_model', "
					."flash='$this->flash', "
					."fnumber='$this->fnumber', "
					."focal_length='$this->focal_length', "
					."height='$this->height', "
					."ISO='$this->ISO', "
					."width='$this->width', "
					."shutter='$this->shutter' "
    	            ."WHERE ID = '$this->id'";
		}
		else {
			$query = "UPDATE $database->table_name SET "
    	            ."title='$this->title', "
    	            ."description='$this->description', "
    	            ."keywords='$this->keywords', "
    	            ."people='$this->people', "
    	            ."event='$this->event', "
    	            ."location='$this->location', "
    	            ."state='$this->state', "
    	            ."country='$this->country', "
    	            ."indoors='$this->indoors', "
    	            ."URL='".addslashes($this->URL)."', "
    	            ."datetaken='$this->datetaken', "
    	            ."needdetails='$this->needdetails', "
    	            ."portfolio='$this->portfolio', "
    	            ."orientation='$this->orientation', "
    	            ."photographer='$this->photographer', "
					."camera_make='$this->camera_make', "
					."camera_model='$this->camera_model', "
					."flash='$this->flash', "
					."fnumber='$this->fnumber', "
					."focal_length='$this->focal_length', "
					."height='$this->height', "
					."ISO='$this->ISO', "
					."width='$this->width', "
					."shutter='$this->shutter' "
    	            ."WHERE ID = '$this->id'";
		}
		mysql_query($query)
			or die("error: ".mysql_error());
	}
	
	function retrieve($id = "") {
		global $database;
		if ($id == "") {
			$id = $this->id;
		}
		else {
			$this->id = $id;
		}
		$query = "SELECT * FROM $database->table_name WHERE ID = '$id'";
		$get_image = mysql_query($query)
			or die("error: ".mysql_error());
		$image = mysql_fetch_object($get_image);
// set variables
		$this->id = $image->ID;
		$this->title = $image->title;
		$this->description = $image->description;
		$this->keywords = $image->keywords;
		$this->people = $image->people;
		$this->event = $image->event;
		$this->location = $image->location;
		$this->state = $image->state;
		$this->country = $image->country;
		$this->indoors = $image->indoors;
		$this->URL = $image->URL;
		$this->thumbnail = $image->thumbnail;
		$this->datetaken = $image->datetaken;
		$this->dateentered = $image->dateentered;
		$this->needdetails = $image->needdetails;
		$this->portfolio = $image->portfolio;
		$this->orientation = $image->orientation;
		$this->photographer = $image->photographer;

		$this->camera_make = $image->camera_make;
		$this->camera_model = $image->camera_model;
		$this->flash = $image->flash;
		$this->fnumber = $image->fnumber;
		$this->focal_length = $image->focal_length;
		$this->height = $image->height;
		$this->ISO = $image->ISO;
		$this->width = $image->width;
		$this->shutter = $image->shutter;

		return $this;
	}
	
	function create_thumbnail($URL = "", $size = 160, $fresh = 0) {
		if ($URL == "") {
			$URL = $this->URL;
		}
		if (isset($_SESSION["load_thumbs"]) && 
		    array_key_exists($URL, $_SESSION["load_thumbs"]) && 
			file_exists("temp/".$_SESSION["load_thumbs"][$URL]) && 
			$fresh == 0) {
			$filename = "temp/".$_SESSION["load_thumbs"][$URL];
		}
		else {
			$filename = save_JPEG_file_from_JPEG($URL, 160, "temp/", get_unique_id());
			$_SESSION["load_thumbs"][$URL] = $filename;
		}
		$thumbnail = addslashes(fread(fopen($filename, "r"), filesize($filename)));
	
		return $thumbnail;
	}

	function get_orientation($URL = "") {
		if ($URL == "") {
			$URL = $this->URL;
		}
		$height = $this->height;
		$width = $this->width;
		if (empty($this->height) || empty($this->width)) {
			if (!empty($this->thumbnail)) {	
				$original = @ImageCreateFromString(stripslashes($this->thumbnail));
			}
			elseif (isset($_SESSION["load_thumbs"]) &&
			        array_key_exists($URL, $_SESSION["load_thumbs"])) {
				$original = @ImageCreateFromJPEG($_SESSION["load_thumbs"][$URL]);
			}
			else {
				$original = @ImageCreateFromJPEG($URL);
			}
			if (!$original) {
				return false;
			}
			else {
				$height = imagesy($original);
				$width = imagesx($original);
			}
		}
		if ($width > $height) {
			$orientation = 0;
		}
		else {
			$orientation = 1;
		}
		return $orientation;
	}
	
	function get_EXIF($URL = "") {
		if (empty($URL)) {
			$URL = $this->URL;
		}
		$exif = read_exif_data_quick($URL);
		if (!empty($exif["DateTimeOriginal"]) && 
			($this->datetaken == '' || $this->datetaken == '0000-00-00')) {
				$this->datetaken = substr($exif["DateTimeOriginal"], 0, 4)
                                  ."-"
                                  .substr($exif["DateTimeOriginal"], 5, 2)
                                  ."-"
                                  .substr($exif["DateTimeOriginal"], 8, 2);
		}
		if (!empty($exif["Flash"])) {
			if (strtolower($exif["Flash"]) == "on") {
				$this->flash = 1;
			}
			else if (strtolower($exif["Flash"]) == "off") {
				$this->flash = 0;
			}
			else {
				$this->flash = $exif["Flash"];
			}
		}
		if (!empty($exif["ApertureValue"])) {
			$this->fnumber = $exif["ApertureValue"];
		}
		else if (!empty($exif["FNumber"])) {
			$this->fnumber = $exif["FNumber"];
		}
		if (!empty($exif["ExposureTime"])) {
			$this->shutter = $exif["ExposureTime"];
		}
		if (!empty($exif["FocalLength"])) {
			$this->focal_length = $exif["FocalLength"];
			if ($this->focal_length == "INF") {
				$this->focal_length = 999;
			}
		}
		if (!empty($exif["ISOSpeedRatings"])) {
			$this->ISO = $exif["ISOSpeedRatings"];
		}
		else if (!empty($exif["ISO"])) {
			$this->ISO = $exif["ISO"];
		}
		else if (!empty($exif["ISOSetting"])) {
			$this->ISO = $exif["ISOSetting"];
		}
		if (!empty($exif["Make"])) {
			$this->camera_make = $exif["Make"];
		}
		if (!empty($exif["Model"])) {
			$this->camera_model = $exif["Model"];
		}
		if (!empty($exif["ExifImageHeight"])) {
			$this->height = $exif["ExifImageHeight"];
		}
		if (!empty($exif["ExifImageWidth"])) {
			$this->width = $exif["ExifImageWidth"];
		}
		if (empty($this->height) || empty($this->width)) {
			$image = imageCreateFromString(fread(fopen($URL, "r"), filesize($URL)));
			$this->height = imagesy($image);
			$this->width = imagesx($image);
		}
		else {
			if ($this->orientation == 1 && $this->height < $this->width) {
				$temp = $this->height;
				$this->height = $this->width;
				$this->width = $temp;
			}
		}
	}
	
	function append_IPTC($URL = "", $addslashes = 0) {
		if (empty($URL)) {
			$URL = $this->URL;
		}
		$info = '';
		$temp = getimagesize($URL, $info); 
		$iptc = iptcparse($info["APP13"]);
		if ($addslashes == 1) {
			$iptc = addslashes_array($iptc);
		}
		$this->title = append($this->title, $iptc["2#005"][0]);
		$this->description = append($this->description, $iptc["2#120"][0]);
		$temp = $iptc["2#015"][0];
		if (is_array($iptc["2#020"])) {
			foreach ($iptc["2#020"] as $v) {
				$temp = append($temp, $v);
			}
		}
		else {
			$temp = append($temp, $iptc["2#020"]);
		}
		if (is_array($iptc["2#025"])) {
			foreach ($iptc["2#025"] as $v) {
				$temp = append($temp, $v);
			}
		}
		else {
			$temp = append($temp, $iptc["2#025"]);
		}
		$this->keywords = append($this->keywords, $temp);
		if (!empty($iptc["2#080"][0])) {
			$this->photographer = $iptc["2#080"][0];
		}
		$this->location = append($this->location, $iptc["2#090"][0]);
		$this->state = append($this->state, $iptc["2#095"][0]);
		$this->country = append($this->country, $iptc["2#101"][0]);
		$temp = '';
		if (!empty($iptc["2#010"][0])) {
			$temp = append($temp, 'Urgency: '.$iptc["2#010"][0]."\n", '');
		}
		if (!empty($iptc["2#040"][0])) {
			$temp = append($temp, 'Special Instructions: '.$iptc["2#040"][0]."\n", '');
		}
		if (!empty($iptc["2#055"][0])) {
			$temp = append($temp, 'Creation Date: '.$iptc["2#055"][0]."\n", '');
		}
		if (!empty($iptc["2#085"][0])) {
			$temp = append($temp, 'Position: '.$iptc["2#085"][0]."\n", '');
		}
		if (!empty($iptc["2#103"][0])) {
			$temp = append($temp, 'OTR: '.$iptc["2#103"][0]."\n", '');
		}
		if (!empty($iptc["2#105"][0])) {
			$temp = append($temp, 'Headline: '.$iptc["2#105"][0]."\n", '');
		}
		if (!empty($iptc["2#110"][0])) {
			$temp = append($temp, 'Credit: '.$iptc["2#110"][0]."\n", '');
		}
		if (!empty($iptc["2#115"][0])) {
			$temp = append($temp, 'Source: '.$iptc["2#115"][0]."\n", '');
		}
		if (!empty($iptc["2#116"][0])) {
			$temp = append($temp, 'Copyright Notice: '.$iptc["2#116"][0]."\n", '');
		}
		if (!empty($temp)) {
			$temp = "\n".'-- IPTC Data ------------------'."\n".$temp;
		}
		$this->description = append($this->description, $temp);
	}
}

?>
Return current item: Photos