1. This website uses cookies. By continuing to use this website you are giving consent to cookies being used.
    For information on cookies and how you can disable them visit our Cookie Usage page.
    Dismiss Notice

PHP Problem with function

Discussion in 'Web Development' started by alafsiber, Jun 18, 2009.

  1. alafsiber

    alafsiber New Member

    Hi All,

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

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

    PHP:
    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
     
  2. ishkey

    ishkey Moderator, Logos, Sports Crests Staff Member Verified Member

  3. alafsiber

    alafsiber New Member

    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:
    <?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
     
  4. ishkey

    ishkey Moderator, Logos, Sports Crests Staff Member Verified Member

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

    The reason I ask...
     
  5. alafsiber

    alafsiber New Member

    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
     
  6. ishkey

    ishkey Moderator, Logos, Sports Crests Staff Member Verified Member

    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

    A good explaination
    http://www.tuxradar.com/practicalphp/11/2/12
     
  7. alafsiber

    alafsiber New Member

    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