Page 1 of 1

PHP Image Gallery keeps displaying code or image not showing

Posted: Wed Jul 30, 2008 2:36 pm
by slaterino
I've posted regarding this link before but have tried everything and am now looking to provide more code and see if anyone can pinpoint the issue. Basically, I have created an image gallery with link:

http://www.thedaffodilsociety.com/gallery/

When going onto this site and navigating through the gallery, sometimes an image will not show and sometimes all that will appear is the code for the page prefaced by something like this:

///

HTTP/1.1 200 OK
Date: Wed, 30 Jul 2008 19:26:10 GMT
Server: Apache/2
X-Powered-By: PHP/4.4.8
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Keep-Alive: timeout=15, max=92
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html

1088
///

I've restarted the Apache server which has not helped. I have noticed that some of my album images are saved with an extra full stop. For instance, they end in ..jpg. Can anyone spot why this is happening?

I've included all the code for my config.php and functions.php which I think are where the problem is most likely to be. If someone can help point in the right direction with this that would be amazing!!!!

Thanks so much,
Russ

functions.php
///

<?php
function uploadImage($inputName, $uploadDir)
{
$image = $_FILES[$inputName];
$imagePath = '';
$thumbnailPath = '';

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

$imagePath = strtolower(md5(rand() * time()) . ".$ext");
$result = move_uploaded_file($image['tmp_name'], $uploadDir . $imagePath);

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

if (!$result) {
unlink($uploadDir . $imagePath);
$imagePath = $thumbnailPath = '';
} else {
$thumbnailPath = $result;
}
} else {
$imagePath = $thumbnailPath = '';
}

}


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

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

}

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;

}

function getPagingLink($totalResults, $pageNumber, $itemPerPage = 10, $strGet = '')
{
$pagingLink = '';
$totalPages = ceil($totalResults / $itemPerPage);

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

if ($totalPages > 1) {
$self = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] ;

if ($pageNumber > 1) {
$page = $pageNumber - 1;
if ($page > 1) {
$prev = " <a href=\"$self?pageNum=$page&$strGet\">[Prev]</a> ";
} else {
$prev = " <a href=\"$self?$strGet\">[Prev]</a> ";
}

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

if ($pageNumber < $totalPages) {
$page = $pageNumber + 1;
$next = " <a href=\"$self?pageNum=$page&$strGet\">[Next]</a> ";
$last = " <a href=\"$self?pageNum=$totalPages&$strGet\">[Last]</a> ";
} 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[] = " <a href=\"$self?$strGet\">$page</a> ";
} else {
$pagingLink[] = " <a href=\"$self?pageNum=$page&$strGet\">$page</a> ";
}
}

}

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

$pagingLink = $first . $prev . $pagingLink . $next . $last;
}

return $pagingLink;
}

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 ' > <a href="index.php?page=list-image&album=' . $album . '">' . $row['al_name'] . '</a>';

if (isset($_GET['image'])) {
$image = $_GET['image'];
$sql = "SELECT im_title
FROM tbl_image
WHERE im_id = $image";

$result = mysql_query($sql) or die('Error, get image name failed. ' . mysql_error());
$row = mysql_fetch_assoc($result);

echo ' > <a href="index.php?page=image-detail&album=' . $album . '&image=' . $image . '">' . $row['im_title'] . '</a>';
}
}
}

?>

///


config.php
///
<?php
session_start();

$dbhost = 'xxxxxxxx';
$dbuser = 'xxxxxxxx';
$dbpass = 'xxxxxxxx';
$dbname = 'xxxxxxxx';

define('ALBUM_IMG_DIR', '/home/sites/thedaffodilsociety.com/public_html/gallery/images/album/');

define('GALLERY_IMG_DIR', '/home/sites/thedaffodilsociety.com/public_html/gallery/images/gallery/');

define('THUMBNAIL_WIDTH', 100);

define('FullImage_WIDTH', 450);

$conn = mysql_connect ($dbhost, $dbuser, $dbpass) or die ("I cannot connect to the database because: " . mysql_error());
mysql_select_db ($dbname) or die ("I cannot select the database '$dbname' because: " . mysql_error());
?>

///

Re: PHP Image Gallery keeps displaying code or image not showing

Posted: Thu Jul 31, 2008 2:16 pm
by infolock
I've tried this numerous time and not seen the issue once. I would recommend that instead of resizing the imag every single load, to instead just write the thumbnail out to the file server and do a check. Check to see if a thumbnail exists for hte image and if so, just use that as your SRC. If not, create the thumbnail and then still us the newly created thumbnail as the src.

You'll watn to do something like this:
orig_file1.jpg
orig_file1_tn.jpg

Checking to see if orig_file$x_tn.jpg exists and if so, using that, else creating it. Should solve your issues.

Re: PHP Image Gallery keeps displaying code or image not showing

Posted: Tue Aug 05, 2008 12:58 pm
by slaterino
hey,
cheers for the help. how would i go about changing the php code to do this. i'm a relatively newbie so would really appreciate some pointers. is there any tutorials or examples you can point me to that might be able to help, or you can you elaborate some further? that would be amazing!

thanks again,
russ

Re: PHP Image Gallery keeps displaying code or image not showing

Posted: Tue Aug 05, 2008 12:59 pm
by slaterino
oh, and also, i've realised that this problem only occurs in Firefox! hence why some people are having the problem and not others. can anyone think of a reason why this might be happening?

russ

Re: PHP Image Gallery keeps displaying code or image not showing

Posted: Tue Aug 05, 2008 2:34 pm
by infolock
It looks like your copyImage function is already doing this. You just need to reference the TN that is generated.

Re: PHP Image Gallery keeps displaying code or image not showing

Posted: Tue Aug 05, 2008 2:44 pm
by slaterino
um, what's the TN?

Re: PHP Image Gallery keeps displaying code or image not showing

Posted: Wed Aug 06, 2008 3:44 pm
by infolock
TN = thumbnail =]

Re: PHP Image Gallery keeps displaying code or image not showing

Posted: Thu Aug 07, 2008 4:14 pm
by slaterino
infolock,
Thanks so much for trying to help. I am slightly confused though. At the moment all images are resized when uploaded. The album images is resized to thumbnail and saved to the server. The gallery images are resized and a thumbnail is also created, which is the same size as the album image. Now I might be missing something because I am really inexperienced at php, so please tell me if this is not the case and let me know if i've misunderstood you. This is the code for showing images:

Code: Select all

 
<?php
if (!isset($_GET['type']) || !isset($_GET['name'])) {   
    exit;
}
 
$type = $_GET['type'];
 
$name = $_GET['name'];
include 'library/config.php';
 
if ($type == 'album') {
    $filePath = ALBUM_IMG_DIR . $name;
} else if ($type == 'glimage') {
    $filePath = GALLERY_IMG_DIR . $name;
} else if ($type == 'glthumbnail') {
    $filePath = GALLERY_IMG_DIR . 'thumbnail/' . $name;
} else {
    exit;
}
 
header("Content-length: " . filesize($filePath));
 
header("Content-type: image/" . substr($name, strpos($name, '.') + 1));
 
readfile($filePath);
?>
 
Is it possibly something in this code that is causing the problem. It would seem like an obvious candidate.

Thanks again!
Russ

Re: PHP Image Gallery keeps displaying code or image not showing

Posted: Mon Aug 11, 2008 10:37 am
by infolock
I wouldn't necessarily be doing a "readfile" and altering the header information to display the image.. I would instead just use an img tag (<img src="blah">) and point straight to the thumbnail I wanted to display. Hope that helps.