...

View Full Version : stupid error



skmd
12-19-2007, 07:14 PM
Hi everyone:
I have this script and I wanna modify the path (the script reads the images file in a dir and create thmbnails for them then view them) it was written to read the images from the same dir that has this script but I dont want that so here is the script :

<?php

$columns = 5;
$thmb_width = 120;
$thmb_height = 80;

function resizeImage($originalImage,$toWidth,$toHeight){

// Get the original geometry and calculate scales
list($width, $height) = getimagesize($originalImage);
$xscale=$width/$toWidth;
$yscale=$height/$toHeight;

// Recalculate new size with default ratio
if ($yscale>$xscale){
$new_width = round($width * (1/$yscale));
$new_height = round($height * (1/$yscale));
}
else {
$new_width = round($width * (1/$xscale));
$new_height = round($height * (1/$xscale));
}
// Resize the original image
$imageResized = imagecreatetruecolor($new_width, $new_height);
$imageTmp = imagecreatefromjpeg ($originalImage);
imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

return $imageResized;
}

function generateThumbnails(){
global $thmb_width,$thmb_height;

// Open the actual directory
if ($handle = opendir("."))
{
// Read all file from the actual directory
while ($file = readdir($handle)) {
// Check whether tha actual item is a valid file
if (is_file($file)){
// Check whether the actual image is a thumbnail
if (strpos($file,'_th.jpg')){
$isThumb = true;
} else {
$isThumb = false;
}

if (!$isThumb) {
// Process the file string
$dirName = substr($file,0,strpos($file,basename($file)));
if (strlen($dirName) < 1) $dirName = '.';
$fileName = basename($file);
$fileMain = substr($fileName,0,strrpos($fileName,'.'));
$extName = substr($fileName,strrpos($fileName,'.'),
strlen($fileName)-strrpos($fileName,'.'));

// Check if the actual file is a jpeg image
if (($extName == '.jpg') || ($extName == '.jpeg')){
$thmbFile = $dirName.'/'.$fileMain.'_th.jpg';
// If a thumbnail dosn't exists tahn create a new one
if (!file_exists($thmbFile)){
imagejpeg(resizeImage($file,$thmb_width,$thmb_height),$thmbFile,80);
}
}
}
}
}
}

}

function getNormalImage($file){
$base = substr($file,0,strrpos($file,'_th.jpg'));
if (file_exists($base.'.jpg')) return $base.'.jpg';
elseif (file_exists($base.'.jpeg')) return $base.'.jpeg';
else return "";
}

function displayPhotos(){
global $columns;

generateThumbnails();
$act = 0;
// Open the actual directory
if ($handle = opendir(".")) {

//im trying here to change the path to /home/mysite/public_html/test/


// Read all file from the actual directory
while ($file = readdir($handle)) {
// Check whether tha actual item is a valid file
if (is_file($file)){
// Check whether the actual image is a thumbnail
if (strpos($file,'_th.jpg')){
++$act;
if ($act > $columns) {
echo '</tr><tr><td class="photo"><a href="'.getNormalImage($file).'"><img src="'.$file.'" alt="'.$file.'"/></a></td>';
$act = 1;
} else {
echo '<td class="photo"><a href="'.getNormalImage($file).'"><img src="'.$file.'" alt="'.$file.'"/></a></td>';
}

}
}
}
}
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Micro Photo Gallery</title>
<link href="style/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="main">
<div class="caption">Micro Photo Gallery</div>
<table align="center"><tr>
<?php displayPhotos(); ?>
</table>

<div id="source">Micro Photo Gallery 1.0</div>
</div>
</body>


im trying to change the path in displayPhotos() to /home/mysite/public_html/test/ but it doesnt work
can anybody help me cuz i still noop in php

oesxyl
12-19-2007, 07:24 PM
function generateThumbnails(){
global $thmb_width,$thmb_height;

// Open the actual directory
if ($handle = opendir(".")


Replace "." with your relative path
I don't read all the code and I don't test it, but I beleave that's work.

best regards

skmd
12-19-2007, 07:30 PM
I did it before but it didnt work can u check the path (trailing slashes or spmething)

oesxyl
12-19-2007, 07:47 PM
I did it before but it didnt work can u check the path (trailing slashes or spmething)



while ($file = readdir($handle)) {
// Check whether tha actual item is a valid file
if (is_file($file)){
// where we are
echo $file;
// Check whether the actual image is a thumbnail


comment the line after you see where you are until the script work and then you could remove it.



function generateThumbnails(){
global $thmb_width,$thmb_height;

// Open the actual directory
if ($handle = opendir("./test/")


I presume that your script is in public_html and your pictures in public_html/test/

best regards

skmd
12-19-2007, 07:56 PM
I dont have a problem with generateThumbnails() cuz im not gonna use it
the problem is in displayPhotos()

skmd
12-19-2007, 08:17 PM
it didnt work too

oesxyl
12-19-2007, 09:19 PM
it didnt work too

the thumbnail must be generatated before otherwise displayPhoto could not find any.

you must call generateThumbnaill somewere once, could be for example just before html section of the file at the end of php section..

The path must be modified in both function.

best regards

oesxyl
12-19-2007, 09:28 PM
<?php

$columns = 5;
$thmb_width = 120;
$thmb_height = 80;
$photodir = "./test";

function resizeImage($originalImage,$toWidth,$toHeight){

// Get the original geometry and calculate scales
list($width, $height) = getimagesize($originalImage);
$xscale=$width/$toWidth;
$yscale=$height/$toHeight;

// Recalculate new size with default ratio
if ($yscale>$xscale){
$new_width = round($width * (1/$yscale));
$new_height = round($height * (1/$yscale));
}
else {
$new_width = round($width * (1/$xscale));
$new_height = round($height * (1/$xscale));
}
// Resize the original image
$imageResized = imagecreatetruecolor($new_width, $new_height);
$imageTmp = imagecreatefromjpeg ($originalImage);
imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

return $imageResized;
}

function generateThumbnails(){
global $thmb_width,$thmb_height;

// Open the actual directory
if ($handle = opendir($photodir))
{
// Read all file from the actual directory
while ($file = readdir($handle)) {
// Check whether tha actual item is a valid file
if (is_file($file)){
// Check whether the actual image is a thumbnail
if (strpos($file,'_th.jpg')){
$isThumb = true;
} else {
$isThumb = false;
}

if (!$isThumb) {
// Process the file string
$dirName = substr($file,0,strpos($file,basename($file)));
if (strlen($dirName) < 1) $dirName = $photodir;
$fileName = basename($file);
$fileMain = substr($fileName,0,strrpos($fileName,$photodir));
$extName = substr($fileName,strrpos($fileName,$photodir),
strlen($fileName)-strrpos($fileName,$photodir));

// Check if the actual file is a jpeg image
if (($extName == '.jpg') || ($extName == '.jpeg')){
$thmbFile = $dirName.'/'.$fileMain.'_th.jpg';
// If a thumbnail dosn't exists tahn create a new one
if (!file_exists($thmbFile)){
imagejpeg(resizeImage($file,$thmb_width,$thmb_height),$thmbFile,80);
}
}
}
}
}
}

}

function getNormalImage($file){
$base = substr($file,0,strrpos($file,'_th.jpg'));
if (file_exists($base.'.jpg')) return $base.'.jpg';
elseif (file_exists($base.'.jpeg')) return $base.'.jpeg';
else return "";
}

function displayPhotos(){
global $columns;

generateThumbnails();
$act = 0;
// Open the actual directory
if ($handle = opendir($photodir)) {

//im trying here to change the path to /home/mysite/public_html/test/


// Read all file from the actual directory
while ($file = readdir($handle)) {
// Check whether tha actual item is a valid file
if (is_file($file)){
// Check whether the actual image is a thumbnail
if (strpos($file,'_th.jpg')){
++$act;
if ($act > $columns) {
echo '</tr><tr><td class="photo"><a href="'.getNormalImage($file).'"><img src="'.$file.'" alt="'.$file.'"/></a></td>';
$act = 1;
} else {
echo '<td class="photo"><a href="'.getNormalImage($file).'"><img src="'.$file.'" alt="'.$file.'"/></a></td>';
}

}
}
}
}
}

generateThumbnails();

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Micro Photo Gallery</title>
<link href="style/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="main">
<div class="caption">Micro Photo Gallery</div>
<table align="center"><tr>
<?php displayPhotos(); ?>
</table>

<div id="source">Micro Photo Gallery 1.0</div>
</div>
</body>


Try this, I don't test it. I replace all occurence of "." related with directory path with $photodir, which is declared at the begining of the file. Replaceing the path in photodir must propagate along the code.
Also I call generateThumbnails() at th end of php section.

I hope that's work.

best regards

skmd
12-19-2007, 10:49 PM
the thumbnail must be generatated before otherwise displayPhoto could not find any.

I know but i didnt need it cuz I already generate them before

second, It didnt work it displayed nothing

oesxyl
12-19-2007, 11:02 PM
I know but i didnt need it cuz I already generate them before

second, It didnt work it displayed nothing

use same method, echo $file;, in displayPhoto() to see if find it some picture, and adjust the path in $photodir.

Check the html code generated, I tink is incorect:



<table align="center"><tr>
<?php displayPhotos(); ?>
</table>




if ($act > $columns) {
echo '</tr><tr><td class="photo"><a href="'.getNormalImage($file).'"><img src="'.$file.'" alt="'.$file.'"/></a></td>';
$act = 1;
} else {
echo '<td class="photo"><a href="'.getNormalImage($file).'"><img src="'.$file.'" alt="'.$file.'"/></a></td>';
}


check if tr tags ar proprely closed.

skmd
12-19-2007, 11:36 PM
I didnt get it, noop :D
can u please write it !!

oesxyl
12-19-2007, 11:45 PM
I didnt get it, noop :D
can u please write it !!

Ok, I be back when I finish and test.

best regards

skmd
12-20-2007, 12:00 AM
Thank u so much :)

oesxyl
12-20-2007, 01:18 AM
<?php

$columns = 5;
$thmb_width = 120;
$thmb_height = 80;
$imgdir = "test/"; // add a '/' before 'test' if DOCUMENT_ROOT don't end in '/'
$photodir = $_SERVER['DOCUMENT_ROOT'].$imgdir;

function resizeImage($originalImage,$toWidth,$toHeight){

// Get the original geometry and calculate scales
list($width, $height) = getimagesize($originalImage);
$xscale=$width/$toWidth;
$yscale=$height/$toHeight;

// Recalculate new size with default ratio
if ($yscale>$xscale){
$new_width = round($width * (1/$yscale));
$new_height = round($height * (1/$yscale));
}
else {
$new_width = round($width * (1/$xscale));
$new_height = round($height * (1/$xscale));
}
// Resize the original image
$imageResized = imagecreatetruecolor($new_width, $new_height);
$imageTmp = imagecreatefromjpeg ($originalImage);
imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

return $imageResized;
}

function generateThumbnails(){
global $thmb_width,$thmb_height,$photodir;

// Open the actual directory
$handle = opendir($photodir);
if ($handle) {
// Read all file from the actual directory
while ($file = readdir($handle)) {
// full path
$filepath = $photodir . $file;
// Check whether tha actual item is a valid file
if (is_file($filepath)){
// Check whether the actual image is a thumbnail
if(!preg_match("/_th\.jpg$/",$filepath)){
$thumb = preg_replace("/\.jpg$/","_th.jpg",$filepath);
if(!file_exists($thumb)){
$img = imagejpeg(resizeImage($filepath,$thmb_width,$thmb_height),$thumb,80);
}
}
}
}
}
}

function getNormalImage($file){
$base = substr($file,0,strrpos($file,'_th.jpg'));
if (file_exists($base.'.jpg'))
return $base.'.jpg';
elseif(file_exists($base.'.jpeg'))
return $base.'.jpeg';
else return "";
}

function displayPhotos(){
global $columns, $photodir, $imgdir;

$act = 0;
// Open the actual directory
$handle = opendir($photodir);
if ($handle) {

//im trying here to change the path to /home/mysite/public_html/test/

// Read all file from the actual directory
while ($file = readdir($handle)) {
// Check whether tha actual item is a valid file
$filepath = $photodir . $file;
if (is_file($filepath)){
// Check whether the actual image is a thumbnail
if(preg_match("/_th.jpg$/",$filepath)){
$nimg = preg_replace("/_th\.jpg$/",".jpg",$file);
++$act;
if($act > $columns){
echo "</tr><tr><td>";
$act = 1;
echo '<div class="photo">';
if($nimg !== $file){
echo '<a href="'.$imgdir . $nimg.'">';
}
echo '<img src="'.$imgdir . $file.'" alt="'.$file.'"/>';
if($nimg !== $file){
echo '</a>';
}
echo '</div>';
} else {
echo '<div class="photo">';
if($nimg !== $file){
echo '<a href="'.$imgdir . $nimg.'">';
}
echo '<img src="'.$imgdir . $file.'" alt="'.$file.'"/>';
if($nimg !== $file){
echo '</a>';
}
echo '</div>';
}

}
}
}
}
}

generateThumbnails();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Micro Photo Gallery</title>

<link href="style/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="main">
<div class="caption">Micro Photo Gallery</div>
<table align="center"><tr><td>
<?php displayPhotos(); ?>
</table>

<div id="source">Micro Photo Gallery 1.0</div>

</div>
</body>
</html>


It work, I test it, you must ajust the path in $imgdir and clean the code.

best regards

skmd
12-20-2007, 01:33 AM
yes it worked man and thank u soooo much but the pictures are showing in in the same columns although the column veriable is set to 5??

oesxyl
12-20-2007, 01:46 AM
yes it worked man and thank u soooo much but the pictures are showing in in the same columns although the column veriable is set to 5??

I didn't tuch column variable.

I must modify the html part to avoid unclosed tags, so I simple make a single td cell and put each thumbnail into a div.
I change td to div because you can have different number of pictures along table.
You have to solution:
1. if you know that se column is same on all rows, change div with td, and all is ok.
2. make a table for each row.
All this is inside of displayPhoto into the while loop, you can modify as you wish, only the html part. Only be sure that the tag are closed.

best regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum