Location: PHPKode > projects > CMSmelborp > CMSmelborp/includes/images.php
<?php
/*******************************************************************************
License:
	Copyright 2005 Ryan Morehart
	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at
	
	http://www.apache.org/licenses/LICENSE-2.0
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
	
Filename: /includes/images.php

Purpose: Holds image processing functions for both retrieving and saving/updating.

Processed parameters: (TYPE - NAME - DESCRIPTION - DEFAULT)
	None
*******************************************************************************/

function getImage(&$connection, $identifier)
{
	// Ensure the image name is valid
	if(!isValidImageName($connection, $identifier, true))
	{
		return false;
	}
	
	// Get the image data from database
	$image = mysql_query("SELECT * FROM images WHERE image_name='$identifier';", $connection);
	$image = mysql_fetch_array($image, MYSQL_ASSOC);
	
	if($image === false)
	{
		return false;
	}
	else
	{
		return $image;
	}
}

function getImageTag(&$connection, $identifier, $height = false, $width = false, $proportional = 'prop',
	$extraTags = '')
{
	if(!isValidImageName($connection, $identifier))
	{
		return false;
	}
	
	// Create tag
	$tag = "<img src='/showimage.php";
	
	// Image location
	$tag .= "?name=" . $identifier;
	
	// Height different from the actual?
	if($height != false && $height > 0)
		$tag .= "&amp;height=$height";
		
	// Width different from the actual?
	if($width != false && $width > 0)
		$tag .= "&amp;width=$width";
		
	// Proportional mode
	$tag .= "&amp;proportional=$proportional'";
	
	// Put out the height and width
	//$tag .= " height='{$height}' width='{$width}'";
	
	// The alt text
	// Get image info
	$image = getImage($connection, $identifier);
	if($image !== false)
	{
		$image = @mysql_fetch_array($image, MYSQL_ASSOC);
		$tag .= " alt='" . htmlspecialchars($image['description'], ENT_QUOTES) . "'";
	}
	
	// And any of the user supplied extras
	$tag .= "$extraTags />";
	
	return $tag;
}

function addImageToDatabase(&$connection, $imageName, $imageType, $description, $fileName)
{
	// Ensure the image name is valid
	if(!isValidImageName($connection, $imageName))
	{
		return false;
	}
	
	// Open image file
	$image = @fopen($fileName, 'rb');
	if($image !== false)
	{
		$imageData = fread($image, filesize($fileName));
	}
	else
	{
		return false;
	}
	
	// Insert into database
	$success = mysql_query("INSERT INTO images VALUES('$imageName', '$imageType', '"
		. addslashes($description) . "', '" . addslashes($imageData) . "');", $connection);
		
	if($success === false)
	{
		return false;
	}
	else
	{
		return true;
	}
}

function deleteImage(&$connection, $identifier)
{
	// Ensure the image name is valid
	if(!isValidImageName($connection, $identifier))
	{
		return false;
	}
	
	// Delete from database
	$success = mysql_query("DELETE FROM images WHERE image_name='$identifier';", $connection);
	if($success === false)
	{
		return false;
	}
	
	return true;
}

function setImageInfo(&$connection, $oldIdentifier, $newIdentifier, $description)
{
	// Ensure the image name is valid
	if(!isValidImageName($connection, $oldIdentifier) || ($newIdentifier != $oldIdentifier
		&& isValidImageName($connection, $newIdentifier) && imageExists($connection, $newIdentifier)))
	{
		return false;
	}
	
	// Update info
	$success = mysql_query("UPDATE images SET image_name='$newIdentifier', description='"
		. addslashes($description) . "' WHERE image_name='$oldIdentifier';", $connection);
	if($success === false)
	{
		return false;
	}

	return true;
}

function getImageList(&$connection)
{
	// Get list from database
	$images = mysql_query("SELECT * FROM images ORDER BY image_name;", $connection);
	$temp = array();
	while(($image = mysql_fetch_array($images, MYSQL_ASSOC)) !== false)
	{
		$temp[] = $image;
	}
	return $temp;
}

function isValidImageName(&$connection, $identifier, $isExistent = false)
{
	// Ensure it conains only valid characters
	if(!ereg('^[_a-zA-Z0-9]+$', $identifier))
	{
		return false;
	}
	
	// Check if this name is already in the database (if we are supposed to)
	if($isExistent)
	{
		return imageExists($connection, $identifier);
	}
	else
	{
		return true;
	}
}

function imageExists(&$connection, $identifier)
{
	$exists = mysql_query("SELECT * FROM images WHERE image_name='$identifier';", $connection);
	
	if(mysql_fetch_array($exists, MYSQL_ASSOC) !== false)
	{
		return true;
	}
	else
	{
		return false;
	}
}
?>
Return current item: CMSmelborp