Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-04-2007, 08:20 PM   PM User | #1
graham23s
Regular Coder

 
Join Date: Jul 2007
Location: Scotland
Posts: 134
Thanks: 12
Thanked 0 Times in 0 Posts
graham23s is an unknown quantity at this point
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
graham23s is offline   Reply With Quote
Old 11-05-2007, 10:44 AM   PM User | #2
mlse
Regular Coder

 
mlse's Avatar
 
Join Date: Mar 2005
Posts: 624
Thanks: 20
Thanked 19 Times in 18 Posts
mlse is on a distinguished road
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.
mlse is offline   Reply With Quote
Old 11-05-2007, 07:47 PM   PM User | #3
graham23s
Regular Coder

 
Join Date: Jul 2007
Location: Scotland
Posts: 134
Thanks: 12
Thanked 0 Times in 0 Posts
graham23s is an unknown quantity at this point
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
graham23s is offline   Reply With Quote
Old 11-05-2007, 08:11 PM   PM User | #4
CFMaBiSmAd
Senior Coder

 
CFMaBiSmAd's Avatar
 
Join Date: Oct 2006
Location: Denver, Colorado USA
Posts: 2,711
Thanks: 2
Thanked 251 Times in 243 Posts
CFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the rough
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.
CFMaBiSmAd is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:59 AM.


Advertisement
Log in to turn off these ads.