SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist
    Join Date
    Dec 2006
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Making a thumbnail code

    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$imageTmp0000$new_width$new_height$width$height);

         return 
    $imageResized;
         
         } 
    thanks for any help guys

    Graham

  2. #2
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A quick glance doesn't seem to show anything weird. At what point is it "failing"?

    btw, quick note, if you're going to test image types, exif_imagetype is a better idea, as it actually parses the image headers to figure out what it is. This is mainly because:

    $_FILES['userfile']['type']

    The mime type of the file, if the browser provided this information. An example would be "image/gif". This mime type is however not checked on the PHP side and therefore don't take its value for granted.
    So people could be uploading something that's not a jpg by spoofing the mime type.

  3. #3
    SitePoint Evangelist
    Join Date
    Dec 2006
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Mate,

    thanks for the help i was having trouble with .gif's now you mention it, also could you advise at what part in the script should i create the thumbnail?

    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;
        
    $uploaddirectory_thumbs "thumbs/".$save;
        
        
    //move_uploaded_file($save, $uploaddirectory_thumbs);
        
        ## 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;      
        
        }
      
      } 
    ?>
    thanks for your help

    Graham

  4. #4
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem shouldn't be the location of the function call, it looks fine where it is. I actually think you're issue lies in the usage of imagecreatefromjpeg. You'll probably need to pass in the image type in the thumbnail call function, then do a switch/if statement to indicate which function to call (imagecreatefromjpeg / imagecreatefromgif). Hope that helps.

  5. #5
    SitePoint Member
    Join Date
    Nov 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for this post. I need it for me too
    http://www.letraspedia.com letras de canciones
    free lyrics - espanol

  6. #6
    SitePoint Evangelist
    Join Date
    Dec 2006
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    excellent, thanks mate that's given me something to work on

    Graham


Bookmarks

Posting Permissions

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