The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

ou8125150 (1) [Avatar] Offline
#1
My gallery works (classic) but I have a strange error

Notice: Undefined offset: -1 in

it says it is in line 39 of images.inc.php

here is my images.inc.php

<?php <br /> /* <br /> scan through a directory, listing out all the subfolders and image files <br /> */
function list_dir($dir,$path){
$dirs=array();
$imgs=array();
$others=array();
$d=dir($dir);
while (false != ($entry = $d->read())){
if (is_dir(implode('/',array($dir,$entry)))){
if (($entry!='.') && ($entry!='..')){
$dirs[]=$entry;
}
}else if (is_image_file($entry,$path)){
$bits=explode('.',$entry);
$imgs[]=$bits[0];
}else{
$others[]=$entry;
}
}
$results=array(
'dirs' => $dirs,
'imgs' => $imgs,
'others' => $others
);
return $results;
}

/*
decide whether a file is an image, based on it's name
*/
function is_image_file($entry,$path){
$is_image=false;
$bits=explode('.',$entry);
$last=count($bits)-1;
if ($bits[$last]=='jpg'){
//ignore the thumbnails we've already made!
$is_image=($bits[$last-1]!='thumb' && $bits[$last-2]!='thumb');
if ($is_image){
ensure_thumbnail($bits[0],$path);
}
}
return $is_image;
}

/*
make sure the image has a thumbnail
*/
function ensure_thumbnail($base_name,$path){
global $basedir,$thumb_max;
$thumb_name=join('/',array($basedir,$path,$base_name.'.thumb.jpg'));
if (!file_exists($thumb_name)){
$source_name=join('/',array($basedir,$path,$base_name.'.jpg'));
$source_img=imagecreatefromjpeg($source_name);
$source_x=imageSX($source_img);
$source_y=imageSY($source_img);
$thumb_x=($source_x > $source_y) ?
$thumb_max :
$thumb_max*($source_x/$source_y);
$thumb_y=($source_x < $source_y) ?
$thumb_max :
$thumb_max*($source_y/$source_x);
$thumb_img=ImageCreateTrueColor($thumb_x,$thumb_y);
imagecopyresampled(
$thumb_img,$source_img,
0,0,0,0,
$thumb_x,$thumb_y,
$source_x,$source_y
);
imagejpeg($thumb_img,$thumb_name);
imagedestroy($source_img);
imagedestroy($thumb_img);
}
}

/*
build up breadcrumb trail
*/
function get_breadcrumbs($path){
$bits=split('/',$path);
$crumbs=array();
$tmp_path='/';
$crumbs[]=array(
'name' => 'home',
'path' => $tmp_path
);
foreach ($bits as $i => $value){
if (strlen($value) > 0){
$tmp_path.=$value.'/';
$crumbs[]=array(
'name' => $value,
'path' => $tmp_path
);
}
}
return $crumbs;
}

/*
work out whether we're giving close-up of image, or view of thumbnails
*/
function is_close_up($name){
$result=false;
$bits=explode('.',$name);
$last=$bits[count($bits)-1];
if ($last=='jpg'){ $result=true; }
return $result;
}

if (isset($_GET['path'])){
$path=$_GET['path'];
$fulldir=implode('/',array($basedir,$path));
}else{
$path="";
$fulldir=$basedir;
}
$crumbs=get_breadcrumbs($path);
$cc=count($crumbs);
if ($cc > 0){
$closeup=is_close_up($crumbs[$cc-1]['name']);
}else{
$closeup=false;
}
if ($closeup==false){
$listings=list_dir($fulldir,$path);
$subdirs=$listings['dirs'];
$imgs=$listings['imgs'];
$others=$listings['others'];
}
?>


I looked on line and it said to suppress errors in my php.ini file. This is not an acceptable solution.

Any help is much appreciated.

Message was edited by:
ou8125150
davecrane (149) [Avatar] Offline
#2
Re: Quick Gallery
Hi,

Looks like this line is the culprit:

$is_image=($bits[$last-1]!='thumb' && $bits[$last-2]!='thumb');

$bits is the image filename split on the '.' delimiter, so we're checking that the image isn't already a thumbnail by looking for the ".thumb.jpg" ending. If $last-1 or $last-2 evaluates to less than zero, you'd get the notice that you're seeing, so to tighten up the code just add a bit of bounds checking.

PHP5 has a pretty flexible error reporting mechanism compared to PHP4, and those Notice messages can be spat out in some volume, in my experience. I usually like to turn them off when I'm playing around with PHP in order to get an Ajax app running smoothly. I don't see why turning off notices on a test server running the exaples from the book is unacceptable, but I'm open to explanations. If you don't want to turn them off, that's fine, just add in some bounds checking to make them go away.

If this was a book about PHP programming (or if I was writing something for production!) I'd take a bit more care over the back-end code, but this code is just a bit of back-end support for the JavaScript. If it really is preventing you from running QuickGallery, then get back to me, and I'll look into it some more.

Regards,

Dave