Jump to content

Resizing images


graham23s

Recommended Posts

Hi Guys,

 

i have pieced to gether some code for making thumbnails of uploaded images

 

code:

 

        #############################################
        # Thumbnail code                            #
        #############################################    
        ## Find out the files extension
    	$ext = explode(".", $filename);
     	$ext = $ext[count($ext)-1];
     	
     	## random numbers
     	$randomnumber = rand(0,999999999);
     	
      	if($ext == "jpg" || $ext == "jpeg")	
      	
      		$image = imagecreatefromjpeg($filetemp);
      		
      	elseif($ext == "png")
      	
      		$image = imagecreatefrompng($filetemp);
      		
      	elseif($ext == "gif")
      	
      		$image = imagecreatefromgif($filetemp);
      		
      	## save the file in %	
        $size = 0.50;
        
        ## rename the thumbnail
        $newimagename_thumb = $var_loggedinuser. "-" .time();
        $save = "thumbs/$newimagename_thumb.$ext";
      		
        ## get the files dimensions
        list($width,$height) = getimagesize($filetemp);
        
        $modwidth = $width * $size; 
        $modheight = $height * $size; 
        $thumbnail = imagecreatetruecolor($modwidth, $modheight) ; 
        imagecopyresampled($thumbnail, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);        
        ## imagejpeg($thumbnail, $save, 100); 
        
        if($ext == '.jpg' || $ext == '.jpeg')
     
        imagejpeg($thumbnail, $save, 100);
     
        if($ext == '.gif')
     
        imagegif($thumbnail, $save, 100);
     
        if($ext == '.png')
     
        imagepng($thumbnail, $save, 100);
        #############################################
        # Thumbnail code                            #
        ############################################# 

 

the full 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;
    $newimagename = $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;     
    
    }
    
    ## Rename the file
    $renamedimage = $newimagename.".".substr($_FILES["usersphoto"]["name"],strtolower(strlen($_FILES["usersphoto"]["name"]))-3,3); 
    
        #############################################
        # Thumbnail code                            #
        #############################################    
        ## Find out the files extension
    	$ext = explode(".", $filename);
     	$ext = $ext[count($ext)-1];
     	
     	## random numbers
     	$randomnumber = rand(0,999999999);
     	
      	if($ext == "jpg" || $ext == "jpeg")	
      	
      		$image = imagecreatefromjpeg($filetemp);
      		
      	elseif($ext == "png")
      	
      		$image = imagecreatefrompng($filetemp);
      		
      	elseif($ext == "gif")
      	
      		$image = imagecreatefromgif($filetemp);
      		
      	## save the file in %	
        $size = 0.50;
        
        ## rename the thumbnail
        $newimagename_thumb = $var_loggedinuser. "-" .time();
        $save = "thumbs/$newimagename_thumb.$ext";
      		
        ## get the files dimensions
        list($width,$height) = getimagesize($filetemp);
        
        $modwidth = $width * $size; 
        $modheight = $height * $size; 
        $thumbnail = imagecreatetruecolor($modwidth, $modheight) ; 
        imagecopyresampled($thumbnail, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);        
        ## imagejpeg($thumbnail, $save, 100); 
        
        if($ext == '.jpg' || $ext == '.jpeg')
     
        imagejpeg($thumbnail, $save, 100);
     
        if($ext == '.gif')
     
        imagegif($thumbnail, $save, 100);
     
        if($ext == '.png')
     
        imagepng($thumbnail, $save, 100);
        #############################################
        # Thumbnail code                            #
        ############################################# 
      
    $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',`thumbnail`='$save' WHERE `id`='$var_loggedinuserid'");
    
    stderr("Upload Successful","Your image has been uploaded successfully.");

    include("includes/footer.php");
    exit;      
    
    }
  
  } 
?>

 

the thing i si get no errors, just no thumbnail is created, can anyone see what i could do to fix this?

 

thanks guys

 

Graham

Link to comment
https://forums.phpfreaks.com/topic/76415-resizing-images/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.