Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jul 2007
    Location
    Scotland
    Posts
    134
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Creating thumbnail not working

    Hi There guys,

    i have got this code down correctly, i cant see where the problem is, a quick question first, when you getimagesize using php does the file need to be in the temp directory first or can it be on the server prior to a thumbnail being created?

    form:

    PHP Code:
    <?php   ## See if it's a first time user   $query_user = "SELECT `photo` FROM `users` WHERE `id`='$var_loggedinuserid'";   $results_user = mysql_query($query_user) or die ("Error getting photo 1");   $row = mysql_fetch_array($results_user) or die ("Error getting the photo array");   $photouploaded = $row['photo'];      ## HTML form     echo ('<br /><div style="border: 1px solid black;padding:10px; background: yellow; color: #000000; font-size: 12px;"><b>Upload your main profile headshot here.</b></div><br />');   echo ("<form action=\"uploadphoto.php\" method=\"post\" enctype=\"multipart/form-data\" />");   echo ("<table class=\"sub_table\" width=\"500\" border=\"1\" align=\"center\" cellpadding=\"5\" cellspacing=\"0\">");   echo ("<tr>");   echo ("<td colspan=\"2\" class=\"edit\" align=\"left\"><img src=\"images/upload_headshot.jpg\"></td>");   echo ("</tr>");   echo ("<tr>");   echo ("<td align=\"center\"><b>Select a photo to upload</b></td><td align=\"center\"><input name=\"usersphoto\" type=\"file\" size=\"50\" /></td>");   echo ("</tr>");   echo ("<tr>");   echo ("<td colspan=\"2\" align=\"right\"><input type=\"submit\" name=\"submit\" value=\"Upload Photo\" /></td>");   echo ("</tr>");   echo ("</table>");   ## HTML form               ## Deal with the submission   if($_POST['submit']) {        $filesize = $_FILES['usersphoto']['size'];     $filetype = $_FILES['usersphoto']['type'];     $filetemp = $_FILES['usersphoto']['tmp_name'];     $filename = $_FILES['usersphoto']['name'];          ## vars     $maxheight = 500;     $maxwidth = 500;     $randomnumber = $var_loggedinuser. "-" .time();          ## Allowed file types     $allowed_types = array('image/pjpeg','image/gif','image/png','image/jpeg');          if($filesize == 0) {           stderr("Upload Failed","No file was uploaded.");      include("includes/footer.php");      exit;             }          if(!in_array($filetype, $allowed_types)) {           stderr("Upload Failed","The file you uploaded is not one of the allowed types only .gif and .jpg are allowed.");      include("includes/footer.php");      exit;               }          $imagedimensions = getimagesize($filetemp);          $width = $imagedimensions[0];     $height = $imagedimensions[1];          if($width > $maxwidth || $maxheight > 500) {           stderr("Upload Failed","Images cannot be larger than $maxwidth  x $maxheight, yours was ($width) width and ($height) height, please try again.");      include("includes/footer.php");      exit;             }          ## Rename the file     $renamedimage = $randomnumber.".".substr($_FILES["usersphoto"]["name"],strtolower(strlen($_FILES["usersphoto"]["name"]))-3,3);          ## Call thumbanil function     create_thumbnail($renamedimage);          $uploaddirectory = "uploads/".$renamedimage;          ## Upload code     if(move_uploaded_file($filetemp, $uploaddirectory)) {          ## insert the photo in the database     $photoquery = mysql_query("UPDATE `users` SET `photo`='$renamedimage' WHERE `id`='$var_loggedinuserid'");          stderr("Upload Successful","Your image has been uploaded successfully.");     include("includes/footer.php");     exit;                }      } ?>
    function:

    PHP Code:
     ## Create thumbnail      function create_thumbnail($filetemp) {            ## Get the original geometry and calculate scales      list($width, $height) = getimagesize($filetemp);      $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($filetemp);      imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);       return $imageResized;            } 
    thanks for any help guys

    Graham

  • #2
    Regular Coder mlse's Avatar
    Join Date
    Mar 2005
    Posts
    624
    Thanks
    20
    Thanked 19 Times in 18 Posts
    Hi Graham,

    Could you format your code a little more readably?

    Can you elaborate a little more on what exactly is going wrong? Perhaps some trace from your code?

    Looking at the resources you are using, it could be a MySQL problem or a file permissions problem or an image problem ... or another problem!

    Mike.

  • #3
    Regular Coder
    Join Date
    Jul 2007
    Location
    Scotland
    Posts
    134
    Thanks
    12
    Thanked 0 Times in 0 Posts
    ahh darn i'm not sure what happened with the tags there i put them in [php] ones aswell, i have this so far:

    PHP Code:
    <?php
      
    ## See if it's a first time user
      
    $query_user "SELECT `photo` FROM `users` WHERE `id`='$var_loggedinuserid'";
      
    $results_user mysql_query($query_user) or die ("Error getting photo 1"); 
      
    $row mysql_fetch_array($results_user) or die ("Error getting the photo array");
      
    $photouploaded $row['photo'];
      
      
    ## HTML form  
      
    echo ('<div style="border: 1px solid black;padding:10px; background: yellow; color: #000000; font-size: 12px;"><b>Upload your main profile headshot here.</b></div><br />');
      echo (
    "<form action=\"uploadphoto.php\" method=\"post\" enctype=\"multipart/form-data\" />"); 
      echo (
    "<table class=\"sub_table\" width=\"500\" border=\"1\" align=\"center\" cellpadding=\"5\" cellspacing=\"0\">");
      echo (
    "<tr>");
      echo (
    "<td colspan=\"2\" class=\"edit\" align=\"left\"><img src=\"images/upload_headshot.jpg\"></td>");
      echo (
    "</tr>");
      echo (
    "<tr>");
      echo (
    "<td align=\"center\"><b>Select a photo to upload</b></td><td align=\"center\"><input name=\"usersphoto\" type=\"file\" size=\"50\" /></td>");
      echo (
    "</tr>");
      echo (
    "<tr>");
      echo (
    "<td colspan=\"2\" align=\"right\"><input type=\"submit\" name=\"submit\" value=\"Upload Photo\" /></td>");
      echo (
    "</tr>");
      echo (
    "</table>");
      
    ## HTML form  
             
      ## Deal with the submission
      
    if($_POST['submit']) {
      
        
    $filesize $_FILES['usersphoto']['size'];
        
    $filetype $_FILES['usersphoto']['type'];
        
    $filetemp $_FILES['usersphoto']['tmp_name'];
        
    $filename $_FILES['usersphoto']['name'];
        
        
    ## vars
        
    $maxheight 500;
        
    $maxwidth 500;
        
    $randomnumber $var_loggedinuser"-" .time();
        
        
    ## Allowed file types
        
    $allowed_types = array('image/pjpeg','image/gif','image/png','image/jpeg'); 
        
        if(
    $filesize == 0) {
        
         
    stderr("Upload Failed","No file was uploaded.");
         include(
    "includes/footer.php");
         exit;   
        
        }
        
        if(!
    in_array($filetype$allowed_types)) {
        
         
    stderr("Upload Failed","The file you uploaded is not one of the allowed types only .gif and .jpg are allowed.");
         include(
    "includes/footer.php");
         exit;     
        
        }
        
        
    $imagedimensions getimagesize($filetemp);
        
        
    $width $imagedimensions[0];
        
    $height $imagedimensions[1];
        
        if(
    $width $maxwidth || $maxheight 500) {
        
         
    stderr("Upload Failed","Images cannot be larger than $maxwidth  x $maxheight, yours was ($width) width and ($height) height, please try again.");
         include(
    "includes/footer.php");
         exit;   
        
        }
        
        
    ## Rename the file
        
    $renamedimage $randomnumber.".".substr($_FILES["usersphoto"]["name"],strtolower(strlen($_FILES["usersphoto"]["name"]))-3,3); 
        
        
    $uploaddirectory "uploads/".$renamedimage;
        
        
    ## Upload code
        
    if(move_uploaded_file($filetemp$uploaddirectory)) {
        
        
    ## insert the photo in the database
        
    $photoquery mysql_query("UPDATE `users` SET `photo`='$renamedimage' WHERE `id`='$var_loggedinuserid'");
        
        
    stderr("Upload Successful","Your image has been uploaded successfully.");
        
        
    #############################################
        # Thumbnail
        #############################################
        
    $add "uploads/$renamedimage";
        
        
    ///////// Start the thumbnail generation//////////////
        
    $n_width 300;          // Fix the width of the thumb nail images
        
    $n_height 300;         // Fix the height of the thumb nail imaage

        
    $tsrc="thumbs/$renamedimage";   // Path where thumb nail image will be stored
        
        /////////////////////////////////////////////// Starting of GIF thumb nail creation///////////
        
    if (@$userfile_type=="image/gif") {
        
    $im=ImageCreateFromGIF($add);
        
    $width=ImageSx($im);              // Original picture width is stored
        
    $height=ImageSy($im);             // Original picture height is stored
        
    $newimage=imagecreatetruecolor($n_width,$n_height);
        
    imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
        if (
    function_exists("imagegif")) {
        
    Header("Content-type: image/gif");
        
    ImageGIF($newimage,$tsrc);
        }
        elseif (
    function_exists("imagejpeg")) {
        
    Header("Content-type: image/jpeg");
        
    ImageJPEG($newimage,$tsrc);
        }
        
    chmod("$tsrc",0777);
        }
        
    ////////// end of gif file thumb nail creation//////////////

        ////////////// starting of JPG thumb nail creation//////////
        
    if($userfile_type == "image/pjpeg"){
        
    $im=ImageCreateFromJPEG($add); 
        
    $width=ImageSx($im);              // Original picture width is stored
        
    $height=ImageSy($im);             // Original picture height is stored
        
    $newimage=imagecreatetruecolor($n_width,$n_height);                 
        
    imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
        
    ImageJpeg($newimage,$tsrc);
        
    chmod("$tsrc",0777);
        }
        
    ////////////////  End of JPG thumb nail creation //////////
        #############################################
        # Thumbnail
        #############################################

        
    include("includes/footer.php");
        exit;      
        
        }
      
      } 
    ?>
    i get the "upload successful" message but still no thumbnail created, can anyone see where i have went wrong at all?

    cheers

    Graham

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,072
    Thanks
    2
    Thanked 320 Times in 312 Posts
    Your upload code allows these types -
    PHP Code:
    $allowed_types = array('image/pjpeg','image/gif','image/png','image/jpeg'); 
    However, your thumbnail code only processes -
    PHP Code:
    if (@$userfile_type=="image/gif") { 
    ...
    if(
    $userfile_type == "image/pjpeg"){ 
    My guess is that your image type is actually 'image/jpeg' and you currently don't have any code that is being executed when it is.

    Make sure you have code for each possible type you allow and echo the type of the uploaded file to make sure what it actually is.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •