PHP- createThumbnail() function error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • caveman
    New Member
    • Feb 2007
    • 6

    #1

    PHP- createThumbnail() function error

    Hi i am using PHP to upload picture files to a file in my server, but for some reason it will not create thumbnails of the upload, all that happens is that where the thumbnail is meant to be displayed is a square with an x in it. No error message is displayed, can anyone help me? I cant see where the error is... im trying to save the thumbnail to file 'thumbnail/'

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

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

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

    if ($result) {

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

    if (!$result) {
    unlink($uploadD ir . $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($s rcFile) && isset($destFile ))
    {
    $size = getimagesize($s rcFile);
    $w = number_format($ width, 0, ',', '');
    $h = number_format(( $size[1] / $size[0]) * $width, 0, ',', '');

    $thumbnail = copyImage($srcF ile, $destFile, $w, $h, $quality);
    }

    // return the thumbnail file name on sucess or blank on fail
    return basename($thumb nail);
    }


    function copyImage($srcF ile, $destFile, $w, $h, $quality = 75)
    {
    $tmpSrc = pathinfo(strtol ower($srcFile)) ;
    $tmpDest = pathinfo(strtol ower($destFile) );
    $size = getimagesize($s rcFile);

    if ($tmpDest['extension'] == "gif" || $tmpDest['extension'] == "jpg")
    {
    $destFile = substr_replace( $destFile, 'jpg', -3);
    $dest = imagecreatetrue color($w, $h);
    //imageantialias( $dest, TRUE);
    } elseif ($tmpDest['extension'] == "png") {
    $dest = imagecreatetrue color($w, $h);
    //imageantialias( $dest, TRUE);
    } else {
    return false;
    }

    switch($size[2])
    {
    case 1: //GIF
    $src = imagecreatefrom gif($srcFile);
    break;
    case 2: //JPEG
    $src = imagecreatefrom jpeg($srcFile);
    break;
    case 3: //PNG
    $src = imagecreatefrom png($srcFile);
    break;
    default:
    return false;
    break;
    }

    imagecopyresamp led($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($totalResu lts / $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?pa geNum=$page&$st rGet\">[Prev]</a> ";
    } else {
    $prev = " <a href=\"$self?$s trGet\">[Prev]</a> ";
    }

    $first = " <a href=\"$self?$s trGet\">[First]</a> ";
    } else {
    $prev = '';
    $first = '';
    }


    if ($pageNumber < $totalPages) {
    $page = $pageNumber + 1;
    $next = " <a href=\"$self?pa geNum=$page&$st rGet\">[Next]</a> ";
    $last = " <a href=\"$self?pa geNum=$totalPag es&$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?$s trGet\">$page</a> ";
    } else {
    $pagingLink[] = " <a href=\"$self?pa geNum=$page&$st rGet\">$page</a> ";
    }
    }

    }

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

    // return the page navigation link
    $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($sq l) or die('Error, get album name failed. ' . mysql_error());
    $row = mysql_fetch_ass oc($result);
    echo ' &gt; <a href="admin.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($sq l) or die('Error, get image name failed. ' . mysql_error());
    $row = mysql_fetch_ass oc($result);

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

    ?>
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    You know that code should be posted within the appropriate php, html or code tags..

    You cannot expect our members to wade throught a lot of unstructured code lines.

    You have been requested before and you obviously have chosen to ignore this request.

    moderator

    Comment

    • caveman
      New Member
      • Feb 2007
      • 6

      #3
      Originally posted by ronverdonk
      You know that code should be posted within the appropriate php, html or code tags..

      You cannot expect our members to wade throught a lot of unstructured code lines.

      You have been requested before and you obviously have chosen to ignore this request.

      moderator

      Sorry i forgot, its PHP code

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        Originally posted by caveman
        Sorry i forgot, its PHP code
        This has nothing to do with that. You must read the Posting Guidelines HERE and you'll understand what you need to do.

        moderator

        Comment

        Working...