Call to undefined function....

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
odie2828
Forum Commoner
Posts: 39
Joined: Tue Aug 05, 2008 4:40 pm

Call to undefined function....

Post by odie2828 »

i got this code from a website and i am not sure what this error means.

Fatal error: Call to undefined function: imagecreatetruecolor() in /home/clubsoho/public_html/image-gallery/library/functions.php on line 80


here is the code:

Code: Select all

 
<?php
[color=#BF8000]/*
    Upload an image and create the thumbnail. The thumbnail is stored 
    under the thumbnail sub-directory of $uploadDir.
    
    Return the uploaded image name and the thumbnail also.
*/[/color]
function uploadImage($inputName, $uploadDir)
{
    $image     = $_FILES[$inputName];
    $imagePath = [color=#FF0000]''[/color];
    $thumbnailPath = [color=#FF0000]''[/color];
    
   [color=#BF8000] // if a file is given[/color]
    if (trim($image[[color=#FF0000]'tmp_name'[/color]]) != [color=#FF0000]''[/color]) {
        $ext = substr(strrchr($image[[color=#FF0000]'name'[/color]], "[color=#FF0000].[/color]"), [color=#FF0000]1[/color]); 
 
        [color=#BF8000]// generate a random new file name to avoid name conflict
        // then save the image under the new file name[/color]        $imagePath = md5(rand() * time()) . [color=#FF0000]".$ext"[/color];
        $result    = move_uploaded_file($image[[color=#FF0000]'tmp_name'[/color]], $uploadDir . $imagePath);
            
        if ($result) {
            [color=#BF8000]// create thumbnail[/color]
            $thumbnailPath =  md5(rand() * time()) . [color=#FF0000]".$ext"[/color];
            $result = createThumbnail($uploadDir . $imagePath, $uploadDir . [color=#FF0000]'thumbnail/' [/color]. $thumbnailPath, THUMBNAIL_WIDTH);
            
            [color=#BF8000]// create thumbnail failed, delete the image[/color]
            if (!$result) {
                unlink($uploadDir . $imagePath);
                $imagePath = $thumbnailPath = '';
            } else {
                $thumbnailPath = $result;
            }   
        } else {
            [color=#BF8000]// the image cannot be uploaded[/color]
            $imagePath = $thumbnailPath = '';
        }
        
    }
 
    
    return array('image' => $imagePath, 'thumbnail' => $thumbnailPath);
}
 
[color=#BF8000]/*
    Create a thumbnail of $srcFile and save it to $destFile.
    The thumbnail will be $width pixels.
*/[/color]
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);
    }
    
 [color=#BF8000]   // return the thumbnail file name on sucess or blank on fail[/color]
    return basename($thumbnail);
}
 
[color=#BF8000]/*
    Copy an image to a destination file. The destination
    image size will be $w X $h pixels
*/[/color]
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);
       [color=#BF8000]//imageantialias($dest, TRUE);[/color]
    } elseif ($tmpDest['extension'] == "png") {
       $dest = imagecreatetruecolor($w, $h);
       [color=#BF8000]//imageantialias($dest, TRUE);[/color]
    } else {
      return false;
    }
 
    switch($size[2])
    {
       case 1:       [color=#BF8000]//GIF[/color]           
           $src = imagecreatefromgif($srcFile);
           break;
       case 2:       [color=#BF8000]//JPEG[/color]
           $src = imagecreatefromjpeg($srcFile);
           break;
       case 3:       [color=#BF8000]//PNG[/color]
           $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;
 
}
 
 
[color=#BF8000]/*
    Create the link for moving from one page to another
*/[/color]
function getPagingLink($totalResults, $pageNumber, $itemPerPage = 10, $strGet = '')
{
    $pagingLink    = '';
    $totalPages    = ceil($totalResults / $itemPerPage);
    
    [color=#BF8000]// how many link pages to show[/color]
    $numLinks      = 10;
 
    [color=#BF8000]// create the paging links only if we have more than one page of results[/color]
    if ($totalPages > 1) {
        $self = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] ;
 
       [color=#BF8000] // print 'previous' link only if we're not
        // on page one[/color]
        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  = ''; [color=#BF8000]// we're on page one, don't show 'previous' link[/color]            
            $first = ''; [color=#BF8000]// nor 'first page' link[/color]
        }
    
        [color=#BF8000]// print 'next' link only if we're not
        // on the last page[/color]
        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 = ''; [color=#BF8000]// we're on the last page, don't show 'next' link[/color]
            $last = ''; [color=#BF8000]// nor 'last page' link[/color]
        }
 
        $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 ";   [color=#BF8000]// no need to create a link to current page[/color]
            } else {
                if ($page == 1) {
                    $pagingLink[] = " <a href=\"$self?$strGet\">$page</a> ";
                } else {    
                    $pagingLink[] = " <a href=\"$self?pageNum=$page&$strGet\">$page</a> ";
                }   
            }
    
        }
        
        $pagingLink = implode(' | ', $pagingLink);
        
        // return the page navigation link
        $pagingLink = $first . $prev . $pagingLink . $next . $last;
    }
    
    return $pagingLink;
}
 
/*
    Display the breadcrumb navigation on top of the gallery page
*/
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>';
        }
    }
}
 
?>
 
User avatar
ghurtado
Forum Contributor
Posts: 334
Joined: Wed Jul 23, 2008 12:19 pm

Re: Call to undefined function....

Post by ghurtado »

What exact version of PHP are you running?
odie2828
Forum Commoner
Posts: 39
Joined: Tue Aug 05, 2008 4:40 pm

Re: Call to undefined function....

Post by odie2828 »

4.4.7
User avatar
ghurtado
Forum Contributor
Posts: 334
Joined: Wed Jul 23, 2008 12:19 pm

Re: Call to undefined function....

Post by ghurtado »

I went to the manual page for this function:

http://us.php.net/imagecreatetruecolor

And noticed the following
Depending on your PHP and GD versions this function is defined or not. With PHP 4.0.6 through 4.1.x this function always exists if the GD module is loaded, but calling it without GD2 being installed PHP will issue a fatal error and exit. With PHP 4.2.x this behaviour is different in issuing a warning instead of an error. Other versions only define this function, if the correct GD version is installed.
odie2828
Forum Commoner
Posts: 39
Joined: Tue Aug 05, 2008 4:40 pm

Re: Call to undefined function....

Post by odie2828 »

ok thank you. i have started to install the GD module

I will let you know the status when it is finished...
odie2828
Forum Commoner
Posts: 39
Joined: Tue Aug 05, 2008 4:40 pm

Re: Call to undefined function....

Post by odie2828 »

now i am getting this message:

Warning: move_uploaded_file(http://www.clubsohotan.com/image-galler ... 9d556e.jpg) [function.move-uploaded-file]: failed to open stream: HTTP wrapper does not support writeable connections in /home/clubsoho/public_html/image-gallery/library/functions.php on line 21

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phplSzJ3Z' to 'http://www.clubsohotan.com/image-galler ... 9d556e.jpg' in /home/clubsoho/public_html/image-gallery/library/functions.php on line 21
Error uploading file
User avatar
ghurtado
Forum Contributor
Posts: 334
Joined: Wed Jul 23, 2008 12:19 pm

Re: Call to undefined function....

Post by ghurtado »

You are trying to move the uploaded file to a web url, which you can't do, since you can't generally "write" to the web. If you want to move the uploaded file somewhere else in your browser, pass the function a path that does not start with "http".
Post Reply