0

just explain what the files is all about. this is a photo gallery code in php.. it will display thmubnails of images from 'thumbs' folder and when click it has it will be zoomed by displaying full image in root folder from lightbox.js

it also has a file name description.txt which has names of the image and 2 lines about the image. example:- 1.jpg Peacock 2.jpg Icicles 3.jpg Large Waterfall and so on.

this description is displayed below the image.

now i have some errors Can anyone help me? I m having this error

Notice: Undefined index:

@mickmackusa i edited and added the var_export($titles); as you said and below is error in the imageenter image description here

here is my full code

<?php
 # SETTINGS
 $max_width = 100;
 $max_height = 100;
 $per_page = 5;
 
  $page = $_GET['page'];
 
 $has_previous = false;
 $has_next = false;
 
 function getInfo() {
  $file = fopen('description.txt', 'r');
 
  $arr = array();
  while( !feof( $file ) ) {
   $arr[ str_replace( "\n", '', fgets( $file ) ) ] = str_replace( "\n", '', fgets( $file ) );
  }
  
  fclose( $file );
 
  return $arr;
 }
 
 function getPictures() {
  global $page, $per_page, $has_previous, $has_next;
  if ( $handle = opendir(".") ) {
   $lightbox = rand();
   echo '<ul id="pictures">';
   
   $count = 0;
   $skip = $page * $per_page;
   
   if ( $skip != 0 )
    $has_previous = true;
   
   while ( $count < $skip && ($file = readdir($handle)) !== false ) {
    if ( !is_dir($file) && ($type = getPictureType($file)) != '' )
     $count++;
   }
   $count = 0;
   $titles = getInfo();
            var_export($titles);
   while ( $count < $per_page && ($file = readdir($handle)) !== false ) {
    if ( !is_dir($file) && ($type = getPictureType($file)) != '' ) {
     if ( ! is_dir('thumbs') ) {
      mkdir('thumbs');
     }
     if ( ! file_exists('thumbs/'.$file) ) {
      makeThumb( $file, $type );
     }
     echo '<li><a href="'.$file.'" title="'.$titles[$file].'" rel="lightbox['.$lightbox.']">';
     echo '<img src="thumbs/'.$file.'" alt="" />';
     echo '</a></li>';
     $count++;
    }
   }
   echo '</ul>';
   
   while ( ($file = readdir($handle)) !== false ) {
    if ( !is_dir($file) && ($type = getPictureType($file)) != '' ) {
     $has_next = true;
     break;
    }
   }
  }
 }

 function getPictureType($file) {
  $split = explode('.', $file); 
  $ext = $split[count($split) - 1];
  if ( preg_match('/jpg|jpeg/i', $ext) ) {
   return 'jpg';
  } else if ( preg_match('/png/i', $ext) ) {
   return 'png';
  } else if ( preg_match('/gif/i', $ext) ) {
   return 'gif';
  } else {
   return '';
  }
 }
 
 function makeThumb( $file, $type ) {
  global $max_width, $max_height;
  if ( $type == 'jpg' ) {
   $src = imagecreatefromjpeg($file);
  } else if ( $type == 'png' ) {
   $src = imagecreatefrompng($file);
  } else if ( $type == 'gif' ) {
   $src = imagecreatefromgif($file);
  }
  if ( ($oldW = imagesx($src)) < ($oldH = imagesy($src)) ) {
   $newW = $oldW * ($max_width / $oldH);
   $newH = $max_height;
  } else {
   $newW = $max_width;
   $newH = $oldH * ($max_height / $oldW);
  }
  $new = imagecreatetruecolor($newW, $newH);
  imagecopyresampled($new, $src, 0, 0, 0, 0, $newW, $newH, $oldW, $oldH);
  if ( $type == 'jpg' ) {
   imagejpeg($new, 'thumbs/'.$file);
  } else if ( $type == 'png' ) {
   imagepng($new, 'thumbs/'.$file);
  } else if ( $type == 'gif' ) {
   imagegif($new, 'thumbs/'.$file);
  }
  imagedestroy($new);
  imagedestroy($src);
 }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UFT-8" />
<title>Pictures</title>
<link rel="stylesheet" href="css/lightbox.css" type="text/css" media="screen" />
<script type="text/javascript" src="js/prototype.js"></script>
<script type="text/javascript" src="js/scriptaculous.js?load=effects,builder"></script>
<script type="text/javascript" src="js/lightbox.js"></script>
<style type="text/css">
body {
 width:780px;
 margin:0 auto;
}
#pictures li {
 float:left;
 height:<?php echo ($max_height + 10); ?>px;
 list-style:none outside;
 width:<?php echo ($max_width + 10); ?>px;
 text-align:center;
}
img {
 border:0;
 outline:none;
}
.prev {
 float:left;
}
.next {
 float:right;
}
</style>
</head>
<body>

<?php getPictures(); ?>

<div style="clear:both"></div>

<?php
 if ( $has_previous )
  echo '<p class="prev"><a href="?page='.($page - 1).'">← Previous Page</a></p>';

 if ( $has_next )
  echo '<p class="next"><a href="?page='.($page + 1).'">Next Page →</a></p>';
?>

<div style="clear:both"></div>


</body>
</html>
Yuguyo
  • 21
  • 4
  • its not duplicate, that previous question couldnt solve my issue .. – Yuguyo Feb 14 '18 at 18:54
  • Is this coming from `$titles[$file]`? Please call `var_export($titles)` before the loop. – mickmackusa Feb 14 '18 at 21:08
  • where do i exactly put it ? var_export($titles); it says Undefined variable: titles in C:\xampp\htdocs\my\immg\info.php on line 7 NULL where ever i put this it shows this error – Yuguyo Feb 15 '18 at 08:34
  • After `$titles = getInfo();` inside of the `getPictures()` function – mickmackusa Feb 15 '18 at 08:36
  • You may add it to your question via an Edit. – mickmackusa Feb 15 '18 at 08:37
  • hello, mickmackusa, this is the link of complete files..can you check, i really want to work well and its really close of getting fixed.. http://ragnagfx.com/mygallery.rar – Yuguyo Feb 15 '18 at 10:09
  • I don't think I want to click that. You can use webservices to dump the `var_export()` data that I have requested. Or better, you can truncate the data and post it in your question. Keep in mind, after solving your issue, future readers will be seeking clear, education information from this page. For this reason, questions must be clear, complete, and minimal so that answers can be concise and accurate. – mickmackusa Feb 15 '18 at 10:16
  • If you ever decide to improve your question, you can alert me using @mickmackusa in a comment – mickmackusa Feb 15 '18 at 11:03
  • @mickmackusa i edited the post and added var_export($titles); and the photo of error.. please check and let me know the fix for it – Yuguyo Feb 15 '18 at 18:08
  • Looking at your new question - where you are generating the $titles array, there seems to be some whitespace after the file name (it's '1.jpg ' and not '1.jpg'), so when building that array - use `trim(fgets( $file ))` instead of `str_replace( "\n", '', fgets( $file ))` in `$arr[ str_replace( "\n", '', fgets( $file ) ) ]` – Nigel Ren Feb 19 '18 at 08:22
  • @NigelRen hey buddy , that really solved the error.. i have 1 more question for you to ask..at the moment it shows the description of when i hover on the image.. what if i want it below the image ? – Yuguyo Feb 19 '18 at 08:29
  • Probably better to remove the other question about this and add this other problem as a new question as I can't add proper answers here as it's closed. – Nigel Ren Feb 19 '18 at 08:31
  • it says i have reached questions limit and asked me to wait for 4 days :-( – Yuguyo Feb 19 '18 at 08:52

0 Answers0