Jump to content

Image gallery upload


wrathican

Recommended Posts

hey peeps,

 

im in the process of making an image gallery and im stuck on the album creation. im using a database to store the information but the problem does not lie within that part. the problem is with the image upload. i have a hunch that i have defined GALLERY_IMG_DIR and ALBUM_IMG_DIR incorrectly.

 

here are the definitions:

<?php

$host="host";
$user="db";
$pass="pass";
$dbname="dbname";

// an album can have an image used as thumbnail
// we save the album image here
define('ALBUM_IMG_DIR', '/images/gallery/album/');

// all images inside an album are stored here
define('GALLERY_IMG_DIR', '/images/gallery/'); 

// When we upload an image the thumbnail is created on the fly
// here we set the thumbnail width in pixel. The height will
// be adjusted proportionally
define('THUMBNAIL_WIDTH', 150);

?>

 

this is my image upload and album creation script:

<?php
//Included files
include "../includes/misc.inc";
include "../includes/opendb.inc";
include "../includes/functions.inc";

//start session
session_start();

//array & variable declaration
$errorarray = array();	//used to store error messages

//check if member
if (($_SESSION['loggedin'] != "yes") || ($_SESSION['level'] == 0)){
header('Location: ../login.php');
}

//users add an album
case "addalbum":
$albumName = $_POST['txtName'];
$albumDesc = $_POST['mtxDesc'];
$userid = $_SESSION['userid'];

if ($albumName == "") {
	$_SESSION['errorstate'] = 1;
	$errorarray[] = "Album name is empty. Please enter an album name.";
}

if ($albumDesc == "") {
	$_SESSION['errorstate'] = 1;
	$errorarray[] = "Album description is empty. Please enter a description.";
}


if ($_SESSION['errorstate'] != 1) {

	$imgName   = $_FILES['fleImage']['name'];
	$tmpName   = $_FILES['fleImage']['tmp_name'];

	// we need to rename the image name just to avoid
	// duplicate file names
	// first get the file extension
	$ext = strrchr($imgName, ".");

	// then create a new random name
	$newName = md5(rand() * time()) . $ext;

    // the album image will be saved here
    $imgPath = ALBUM_IMG_DIR . $newName;

	// resize all album image
	$result = createThumbnail($tmpName, $imgPath, THUMBNAIL_WIDTH);

	if (!$result) {
		$_SESSION['errorstate'] = 1;
		$errorarray[] = "Image thumbnail could not be created. Please try again later.";
		header('location: ../members/pictures.php?page=addalbum');
	}

    	if (!get_magic_quotes_gpc()) {
    	    $albumName  = addslashes($albumName);
    	    $albumDesc  = addslashes($albumDesc);
    	}  

	$query = "INSERT INTO ls12_album (album_name, album_description, album_image, album_ownerid, album_date) 
			  VALUES ('$albumName', '$albumDesc', '$newName', '$userid', NOW())";

    query($query);                    

    // the album is saved, go to the album list 
	$_SESSION['errorstate'] = 1;
	$errorarray[] = "Thank you. The album has been created. Please add some images using the form below.";
	$_SESSION['errormessage'] = $errorarray;

	header('location: ../members/pictures.php?page=addpicture');

}else{
	$_SESSION['errormessage'] = $errorarray;
	header('location: ../members/pictures.php?page=addalbum');
}	

include "../includes/close.inc";

?>

 

here is the functions.inc:

<?php
/*
Upload an image and create the thumbnail. The thumbnail is stored 
under the thumbnail sub-directory of $uploadDir.

Return the uploaded image name and the thumbnail also.
*/
function uploadImage($inputName, $uploadDir)
{
$image     = $_FILES[$inputName];
$imagePath = '';
$thumbnailPath = '';

// if a file is given
if (trim($image['tmp_name']) != '') {
	$ext = substr(strrchr($image['name'], "."), 1); 

	// generate a random new file name to avoid name conflict
	// then save the image under the new file name
	$imagePath = md5(rand() * time()) . ".$ext";
	$result    = move_uploaded_file($image['tmp_name'], $uploadDir . $imagePath);

	if ($result) {
		// create thumbnail
		$thumbnailPath =  md5(rand() * time()) . ".$ext";
		$result = createThumbnail($uploadDir . $imagePath, $uploadDir . 'thumbnail/' . $thumbnailPath, THUMBNAIL_WIDTH);

		// create thumbnail failed, delete the image
		if (!$result) {
			unlink($uploadDir . $imagePath);
			$imagePath = $thumbnailPath = '';
		} else {
			$thumbnailPath = $result;
		}	
	} else {
		// the image cannot be uploaded
		$imagePath = $thumbnailPath = '';
	}

}


return array('image' => $imagePath, 'thumbnail' => $thumbnailPath);
}

/*
Create a thumbnail of $srcFile and save it to $destFile.
The thumbnail will be $width pixels.
*/
function createThumbnail($srcFile, $destFile, $width, $quality = 75)
{
$thumbnail = '';

if (file_exists($srcFile)  && isset($destFile))
{
	$size        = getimagesize($srcFile);
	$w           = number_format($width, 0, ',', '');
	$h           = number_format(($size[1] / $size[0]) * $width, 0, ',', '');

	$thumbnail =  copyImage($srcFile, $destFile, $w, $h, $quality);
}

// return the thumbnail file name on sucess or blank on fail
return basename($thumbnail);
}

/*
Copy an image to a destination file. The destination
image size will be $w X $h pixels
*/
function copyImage($srcFile, $destFile, $w, $h, $quality = 75)
{
    $tmpSrc     = pathinfo(strtolower($srcFile));
    $tmpDest    = pathinfo(strtolower($destFile));
    $size       = getimagesize($srcFile);

    if ($tmpDest['extension'] == "gif" || $tmpDest['extension'] == "jpg")
    {
       $destFile  = substr_replace($destFile, 'jpg', -3);
       $dest      = imagecreatetruecolor($w, $h);
       //imageantialias($dest, TRUE);
    } elseif ($tmpDest['extension'] == "png") {
       $dest = imagecreatetruecolor($w, $h);
       //imageantialias($dest, TRUE);
    } else {
      return false;
    }

    switch($size[2])
    {
       case 1:       //GIF
           $src = imagecreatefromgif($srcFile);
           break;
       case 2:       //JPEG
           $src = imagecreatefromjpeg($srcFile);
           break;
       case 3:       //PNG
           $src = imagecreatefrompng($srcFile);
           break;
       default:
           return false;
           break;
    }

    imagecopyresampled($dest, $src, 0, 0, 0, 0, $w, $h, $size[0], $size[1]);

    switch($size[2])
    {
       case 1:
       case 2:
           imagejpeg($dest,$destFile, $quality);
           break;
       case 3:
           imagepng($dest,$destFile);
    }
    return $destFile;

}
?>

Any ideas whats going wrong, or whether it is the definitions?

 

Thanks in advanced

 

i guess a directory listing would be a good idea too:

project

-includes

--misc.inc //definitions are here

-functions

--memberfunctions.php //the process file

-images

--album //album thumbnails go here

--galley //all other images go here except image thumbnails

---thumbnail //normal image thumbnails go here

-admin

-members

 

 

Link to comment
https://forums.phpfreaks.com/topic/102745-image-gallery-upload/
Share on other sites

so, if im not the host of the server it would be :

define('ALBUM_IMG_DIR', 'http://www.ls12style.co.uk/projects/LS12style/images/gallery/album/');

// all images inside an album are stored here
define('GALLERY_IMG_DIR', 'http://www.ls12style.co.uk/projects/LS12style/images/gallery/');

 

Link to comment
https://forums.phpfreaks.com/topic/102745-image-gallery-upload/#findComment-526244
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.