Go Back   Free website templates > Web Template Help > PHP and MySQL
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


Join now to download all the free website templates or post on the forum. If you have never been on a forum before read the FAQ. It's quick, easy and free to join!
Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-18-2009, 03:27 PM
alafsiber's Avatar
Member
 
Join Date: Jun 2009
Posts: 57
alafsiber is on a distinguished road
Default Problem with function

Hi All,

Firstly sorry if this is a stupid question but dont knwo where to ask so here I am

Im a newbie in PHP and tryin to learn it so here the problem, I had a function(which i got from one website-dont remember where):

PHP Code:
function createThumbs($pathToImages,$pathToThumbs,$thumbWidth) {

//this one i had put to make sure the dir is there
if (!is_dir($pathToThumbs)) {
mkdir($pathToThumbs);
}
  
// open the directory
  
$dir = @opendir$pathToImages );

  
// loop through it, looking for any/all JPG files:
  
while (false !== ($fname readdir$dir ))) {
    
// parse path for the extension
    
$info pathinfo($pathToImages $fname);
    
// continue only if this is a JPEG image
    
if ( strtolower($info['extension']) == 'jpg' ) {

      
// load image and get image size
      
$img imagecreatefromjpeg("{$pathToImages}{$fname}" );
      
$width imagesx$img );
      
$height imagesy$img );

      
// calculate thumbnail size
      
$new_width $thumbWidth;
      
$new_height floor$height * ( $thumbWidth $width ) );

      
// create a new temporary image
      
$tmp_img imagecreatetruecolor$new_width$new_height );

      
// copy and resize old image into new image
      
imagecopyresized$tmp_img$img0000$new_width$new_height$width$height );

      
// save thumbnail into a file
      
imagejpeg$tmp_img"{$pathToThumbs}{$fname}",100);
    }
  }
  
// close the directory
  
closedir$dir );

Basically the function is to create a thumbnails. So when I call the function in a same file lets say index.php, its workin. But when I put the function into outside file(lets say: thumbnail.php) and try to call it in index.php, it didnt work?
I had try to use include('thumbnail.php'); at the 1st line and then called it using createThumbs(....) and its still didnt work...

Can anyone point me why is this happen and again I need to said Im completely newbie to this and still in learning process and tryin to understand it...

Thanks in advance

Cheers,

AlafSiber
__________________
**NOTE** DoNT CLiCK THiS!!!!
Reply With Quote
  #2 (permalink)  
Old 06-20-2009, 12:27 AM
ishkey's Avatar
Moderator
 
Join Date: Aug 2007
Location: North GA USA
Posts: 1,360
ishkey will become famous soon enough
Default

post the cde you tried, not the script
here is the link http://www.webcheatsheet.com/php/cre...ail_images.php
incase you want to look at again.
__________________
RIF= Reading Is Fun - Understanding Funner
cleandeck - lawn mower undercoating and wilmargraphite - graphite lubricants
Reply With Quote
  #3 (permalink)  
Old 06-20-2009, 06:36 AM
alafsiber's Avatar
Member
 
Join Date: Jun 2009
Posts: 57
alafsiber is on a distinguished road
Default

Thank ishkey for get the link(you need to teach me how to using Uncle Google :P).

So here the code in index.php:

PHP Code:
<?php

include('thumbnail_generator.php');

$filename "html/bm/index.htm";
if (
file_exists($filename)) {
    
$handle fopen($filename"r");
    
$contents fread($handlefilesize($filename));
    
fclose($handle);
}

if (@
preg_match("/&ads_nuffnang&/",$contents)) {
    
$filename 'html/ads_nuffnang.txt';
    if (
file_exists($filename)) {
        
$handle fopen($filename"r");
        
$ads_nuffnang fread($handlefilesize($filename));
        
fclose($handle);
    }
    
$contents preg_replace('/&ads_nuffnang&/',$ads_nuffnang,$contents);
}
if (@
preg_match("/&ads_google_adsense&/",$contents)) {
    
$filename 'html/ads_google_adsense.txt';
    if (
file_exists($filename)) {
        
$handle fopen($filename"r");
        
$ads_google_adsense fread($handlefilesize($filename));
        
fclose($handle);
    }
    
$contents preg_replace('/&ads_google_adsense&/',$ads_google_adsense ,$contents);
}
if (@
preg_match("/&ads_youthmedia&/",$contents)) {
    
$filename 'html/ads_youthmedia.txt';
    if (
file_exists($filename)) {
        
$handle fopen($filename"r");
        
$ads_youthmedia fread($handlefilesize($filename));
        
fclose($handle);
    }
    
$contents preg_replace('/&ads_youthmedia&/',$ads_youthmedia ,$contents);
}
if (@
preg_match("/&cartoon&/",$contents)) {
    
$dir 'images/cartoon';
    
$files = @scandir($dir,1);
    
$cur 0;
    
$i 0;

    while (
$files[$i]) {
    if (
$files[$i] != '.' && $files[$i]!= '..' && !is_dir($dir "/" $files[$i])) { 
        
$cur++;
        
$filename $dir '/thumbs/' $files[$i];
        
$filetitle ucwords(substr($files[$i],0,-4));
        
$cartoon $cartoon '<span class="cell"><a href="cartoon/?view=' $filetitle '"><img src="' $filename '" alt="Cartoon:' $filetitle '"><br><b>' date("h:i:sA"fileatime($filename)) . '<br>' date("d M Y"fileatime($filename)) . '<br>' $filetitle '</a></b></span>';
        if (!
is_file($filename)) {
        
createThumbs($dir "/",$dir "/thumbs/",100);
        }
    }
    if (
$cur >= 6) break;
    
$i++;
    }
    
$contents preg_replace('/&cartoon&/',$cartoon ,$contents);
}
if (@
preg_match("/&caricature&/",$contents)) {
    
$dir 'images/caricature';
    
$files = @scandir($dir,1);
    
$cur 0;
    
$i 0;

    while (
$files[$i]) {
    if (
$files[$i] != '.' && $files[$i]!= '..' && !is_dir($dir "/" $files[$i])) { 
        
$cur++;
        
$filename $dir '/thumbs/' $files[$i];
        
$filetitle ucwords(substr($files[$i],0,-4));
        
$caricature $caricature '<span class="cell"><a href="caricature/?view=' $filetitle '"><img src="' $filename '" alt="Caricature:' $filetitle '"><br><b>' date("h:i:sA"fileatime($filename)) . '<br>' date("d M Y"fileatime($filename)) . '<br>' $filetitle '</a></b></span>';
        if (!
is_file($filename)) {
        
createThumbs($dir "/",$dir "/thumbs/",100);
        }
    }
    if (
$cur >= 6) break;
    
$i++;
    }
    
$contents preg_replace('/&caricature&/',$caricature ,$contents);
}

if (@
preg_match("/&mascot&/",$contents)) {
    
$dir 'images/mascot';
    
$files = @scandir($dir,1);
    
$cur 0;
    
$i 0;

    while (
$files[$i]) {
    if (
$files[$i] != '.' && $files[$i]!= '..' && !is_dir($dir "/" $files[$i])) { 
        
$cur++;
        
$filename $dir '/thumbs/' $files[$i];
        
$filetitle ucwords(substr($files[$i],0,-4));
        
$mascot $mascot '<span class="cell"><a href="mascot/?view=' $filetitle '"><img src="' $filename '" alt="Mascot:' $filetitle '"><br><b>' date("h:i:sA"fileatime($filename)) . '<br>' date("d M Y"fileatime($filename)) . '<br>' $filetitle '</a></b></span>';
        if (!
is_file($filename)) {
        
createThumbs($dir "/",$dir "/thumbs/",100);
        }
    }
    if (
$cur >= 6) break;
    
$i++;
    }
    
$contents preg_replace('/&mascot&/',$mascot ,$contents);
}

if (@
preg_match("/&vector&/",$contents)) {
    
$dir 'images/vector';
    
$files = @scandir($dir,1);
    
$cur 0;
    
$i 0;

    while (
$files[$i]) {
    if (
$files[$i] != '.' && $files[$i]!= '..' && !is_dir($dir "/" $files[$i])) { 
        
$cur++;
        
$filename $dir '/thumbs/' $files[$i];
        
$filetitle ucwords(substr($files[$i],0,-4));
        
$vector $vector '<span class="cell"><a href="vector/?view=' $filetitle '"><img src="' $filename '" alt="Vector:' $filetitle '"><br><b>' date("h:i:sA"fileatime($filename)) . '<br>' date("d M Y"fileatime($filename)) . '<br>' $filetitle '</a></b></span>';
        if (!
is_file($filename)) {
        
createThumbs($dir "/",$dir "/thumbs/",100);
        }
    }
    if (
$cur >= 6) break;
    
$i++;
    }
    
$contents preg_replace('/&vector&/',$vector ,$contents);
}


if (@
preg_match("/&footer&/",$contents)) {
    
$filename 'html/bm/footer.txt';
    if (
file_exists($filename)) {
        
$handle fopen($filename"r");
        
$footertext fread($handlefilesize($filename));
        
fclose($handle);
    }
    
$contents preg_replace('/&footer&/',$footertext,$contents);
}

echo 
preg_replace('/.htm"/','.php"',$contents);



?>
I dont know which one to paste, so I just copy all of them.
The function is reside on thumbnail_generator.php and it return error:

Warning: fileatime() [function.fileatime]: stat failed for images/caricature/thumbs/bollywood dancing.jpg in /...../public_html/index.php on line 71

Warning: fileatime() [function.fileatime]: stat failed for images/caricature/thumbs/bollywood dancing.jpg in /...../public_html/index.php on line 71

Fatal error: Call to undefined function createthumbs() in /...../public_html/index.php on line 73

But when I put the function into index.php, it workin.

thanks in advance ishkey...

Cheers,

AlafSiber
__________________
**NOTE** DoNT CLiCK THiS!!!!
Reply With Quote
  #4 (permalink)  
Old 06-20-2009, 10:22 AM
ishkey's Avatar
Moderator
 
Join Date: Aug 2007
Location: North GA USA
Posts: 1,360
ishkey will become famous soon enough
Default

Could you supply your "Declarations for the variables"
$pathToThumbs = "";
$pathToImages = "";
$thumbWidth = "";

The reason I ask...
Quote:
Below we will create a PHP script that contains two functions. The first one scans a provided directory for any .JPG images and, for each one, creates a thumbnail in the specified folder using the GD image functions.
__________________
RIF= Reading Is Fun - Understanding Funner
cleandeck - lawn mower undercoating and wilmargraphite - graphite lubricants
Reply With Quote
  #5 (permalink)  
Old 06-20-2009, 11:02 AM
alafsiber's Avatar
Member
 
Join Date: Jun 2009
Posts: 57
alafsiber is on a distinguished road
Default

Thanks again ishkey already configured it since I tried to echo the path inside the function since you last post make me thinking it might be becoz theres no dir been supply... actually the dir parameter have been supply but a different one(doesnt exists in the server lol) so now already edit and it solved. Also I change the script to make the thumbnail for specified file only instead of checking all the files in that folder.

Again thank man, you point me into right direction

Love ya mate

Cheers,

AlafSiber
__________________
**NOTE** DoNT CLiCK THiS!!!!

Last edited by alafsiber; 06-20-2009 at 06:16 PM.
Reply With Quote
  #6 (permalink)  
Old 06-21-2009, 12:18 PM
ishkey's Avatar
Moderator
 
Join Date: Aug 2007
Location: North GA USA
Posts: 1,360
ishkey will become famous soon enough
Default

Got to watch those virtual paths...

Check which verson of GD is installed on your server, if GD2 then change in the code imagecopyresized to imagecopyresampled
imagecopyresampled will give you a higher quality graphic/thumbnail

Quote:
imagecopyresampled( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
A good explaination
http://www.tuxradar.com/practicalphp/11/2/12
__________________
RIF= Reading Is Fun - Understanding Funner
cleandeck - lawn mower undercoating and wilmargraphite - graphite lubricants
Reply With Quote
  #7 (permalink)  
Old 06-21-2009, 02:25 PM
alafsiber's Avatar
Member
 
Join Date: Jun 2009
Posts: 57
alafsiber is on a distinguished road
Default

Yups I have changed that and thanks again Ishkey...your my man... No doubt the quality is much better than using imagecopyresized now the thumbnail is much better quality...
Gonna give you a rep here ... err how actually to give it can you teach me

Cheers,

AlafSiber

__________________
**NOTE** DoNT CLiCK THiS!!!!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 10:45 AM.



mouseover mouseover mouseover