[PHP] upload image

2006-08-24 Thread Sonja

Hi,

I have problems with uploading image, here is the code

if(isset($_POST['txtTitle']))
{
$albumId   = $_POST['cboAlbum'];
$imgTitle  = $_POST['txtTitle'];
$imgDesc   = $_POST['mtxDesc'];

$images= uploadImage('fleImage', GALLERY_IMG_DIR);

if ($images['image'] == '' && $images['thumbnail'] == '') {
echo "Error uploading file";
exit;
}

$image = $images['image'];
$thumbnail = $images['thumbnail'];

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

$sql = "INSERT INTO tbl_image (im_album_id, im_title, im_description,
im_image, im_thumbnail, im_date) 
VALUES ($albumId, '$imgTitle', '$imgDesc', '$image', 
'$thumbnail',
NOW())";

mysql_query($sql) or die('Error, add image failed : ' . mysql_error()); 
   

echo
"window.location.href='index.php?page=list-image&album=$albumId';";
exit;
} 

when I upload a picture return me error uploadin image.

Thanks  

-- 
View this message in context: 
http://www.nabble.com/upload-image-tf2157181.html#a5959218
Sent from the PHP - General forum at Nabble.com.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] upload image

2006-08-24 Thread Sonja

Hi,
It looks like:

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);
}


Mourad Boulahboub wrote:
> 
> Hi sonja,
> 
> 
> 
> Sonja schrieb am 24.08.2006 09:38:
>> Hi,
>> 
>> I have problems with uploading image, here is the code
>> 
>> if(isset($_POST['txtTitle']))
>> {
>>  $albumId   = $_POST['cboAlbum'];
>>  $imgTitle  = $_POST['txtTitle'];
>>  $imgDesc   = $_POST['mtxDesc'];
>> 
>>  $images= uploadImage('fleImage', GALLERY_IMG_DIR);
> 
> how does the function uploadImage looks like?
> 
> 
>>  if ($images['image'] == '' && $images['thumbnail'] == '') {
>>  echo "Error uploading file";
>>  exit;
>>  }
> 
> the error comes from this if-clause that is depending from the function
> above.
> 
> regards
> Mourad
> 
> -- 
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 
> 
> 

-- 
View this message in context: 
http://www.nabble.com/upload-image-tf2157181.html#a5961434
Sent from the PHP - General forum at Nabble.com.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] upload image

2006-08-25 Thread Sonja

Hi, 

Thanks for help, I find the error, here is the code of functions.php

 $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, $tmpSrc,  $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;
}

 // This is were not allow me to write thumbnail in uploadDirectory,
problems with switch, case2:
// how to resolve this
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;

}

/*
  Check if the user is logged in or not
*/
function checkLogin()
{
  if (!isset($_SESSION['isLogin']) || $_SESSION['isLogin'] == false) {
header('Location: login.php');
exit;
  }
}

/*
  Create the link for moving from one page to another
*/
function getPagingLink($totalResults, $pageNumber, $itemPerPage = 10,
$strGet = '')
{
  $pagingLink= '';
  $totalPages= ceil($totalResults / $itemPerPage);

  // how many link pages to show
  $numLinks  = 10;

  // create the paging links only if we have more than one page of results
  if ($totalPages > 1) {
$self = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] ;

// print 'previous' link only if we're not
// on page one
if ($pageNumber > 1) {
  $page = $pageNumber - 1;
  if ($page > 1) {
$prev = "  \"$self?pageNum=$page&$strGet\" [Prev]  ";
  } else {
$prev = "  \"$self?$strGet\" [Prev]  ";
  }

  $first = "  \"$self?$strGet\" [First]  ";
} else {
  $prev  = ''; // we're on page one, don't show 'previous' link
  $first = ''; // nor 'first page' link
}

// print 'next' link only if we're not
// on the last page
if ($pageNumber < $totalPages) {
  $page = $pageNumber + 1;
  $next = "  \"$self?pageNum=$page&$strGet\" [Next]  ";
  $last = "  \"$self?pageNum=$totalPages&$strGet\" [Last]  ";
} else {
  $next = ''; // we're on the last page, don't show 'next' link
  $last = ''; // nor 'last page' link
}

$start = $pageNumber - ($pageNumber % $numLinks) + 1;
$end   = $start + $numLinks - 1;

$end   = min($totalPages, $end);

$pagingLink = array();
for($page = $start; $page <= $end; $page++)  {
  if ($page == $pageNumber) {
$pagingLink[] = " $page ";   // no need to create a link to current
page
  } else {
if ($page == 1) {
  $pagingLink[] = "  \"$self?$strGet\" $page  ";
} else {
  $pagingLink[] = "  \"$self?pageNum=$page&$strGet\" $page  ";
}
  }

}

$pagingLink = implode(' | ', $pagingLink);

// return the page navigation link
$pagingLink = $first . $prev . $pagingLink . $next . $last;
  }

  return $pagingLink;
}

/*
  Display the breadcrumb navigation on top of the gallery page
*/
function showBreadcrumb()
{
  if (isset($_GET['album'])) {
$album = $_GET['album'];
$sql  = "SELECT al_name
 FROM tbl_album
 WHERE al_id = $album";

$result = mysql_query($sql) or die('Error, get album name failed. ' .
mysql_error());
$row = mysql_fetch_assoc($result);
echo ' >  index.php?page=list-image&album=' . $album . ' ' .
$row['al_name'] . ' ';

if (isset($_GET['image'])) {
  $image = $_GET['image'];
  $sql  = "SELECT im_titl