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 13 of 13
  1. #1
    Regular Coder
    Join Date
    Nov 2006
    Posts
    601
    Thanks
    1
    Thanked 2 Times in 2 Posts

    image upload only 1/2 working

    hey guys.. ok for some reason my images are been resized and inserted into folder only for tumbs but not mini even tho its the exact same code :S
    PHP Code:
    <?php



    session_start
    ();

    //load the config file

    include("config.php");

    require 
    '../settings.php';





    //if the for has submittedd

    if (isset($_POST['upForm'])){



           
    $file_type $_FILES['imgfile']['type'];

           
    $file_name $_FILES['imgfile']['name'];

           
    $file_size $_FILES['imgfile']['size'];

           
    $file_tmp $_FILES['imgfile']['tmp_name'];



           
    //check if you have selected a file.

           
    if(!is_uploaded_file($file_tmp)){

              echo 
    "Error: Please select a file to upload!. <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";

              exit(); 
    //exit the script and don't do anything else.

           
    }

           
    //check file extension

           
    $ext strrchr($file_name,'.');

           
    $ext strtolower($ext);

           if ((
    $extlimit == "yes") && (!in_array($ext,$limitedext))) {

              echo 
    "Wrong file extension.  <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";

              exit();

           }

           
    //get the file extension.

           
    $getExt explode ('.'$file_name);

           
    $file_ext $getExt[count($getExt)-1];



    //get users ID

        
    $id $_SESSION['user_id'];  

        





          

          
    //get the new width variable.

           
    $ThumbWidth $img_thumb_width;



           
    //keep image type

           
    if($file_size){

              if(
    $file_type == "image/pjpeg" || $file_type == "image/jpeg"){

                   
    $new_img imagecreatefromjpeg($file_tmp);

               }elseif(
    $file_type == "image/x-png" || $file_type == "image/png"){

                   
    $new_img imagecreatefrompng($file_tmp);

               }elseif(
    $file_type == "image/gif"){

                   
    $new_img imagecreatefromgif($file_tmp);

               }

               
    //list width and height and keep height ratio.

               
    list($width$height) = getimagesize($file_tmp);

               
    $imgratio=$width/$height;

               if (
    $imgratio>1){

                  
    $newwidth $ThumbWidth;

                  
    $newheight $ThumbWidth/$imgratio;

               }else{

                     
    $newheight $ThumbWidth;

                     
    $newwidth $ThumbWidth*$imgratio;

               }

               
    //function for resize image.

               
    if (function_exists(imagecreatetruecolor)){

               
    $resized_img imagecreatetruecolor($newwidth,$newheight);

               }else{

                     die(
    "Error: Please make sure you have GD library ver 2+");

               }

               
    imagecopyresampled($resized_img$new_img0000$newwidth$newheight$width$height);

               
    //save image

               
    ImageJpeg ($resized_img,"$path_thumbs/$id.$file_ext");

               
    ImageDestroy ($resized_img);

               
    ImageDestroy ($new_img);

               
    //print message

               //echo "<br>Image Thumb: <a href=\"$path_thumbs/$id.$file_ext\">$path_thumbs/$id.$file_ext</a>";

            
    }



           

    $miniWidth $img_mini_width;



           
    //keep image type

           
    if($file_size){

              if(
    $file_type == "image/pjpeg" || $file_type == "image/jpeg"){

                   
    $new_img imagecreatefromjpeg($file_tmp);

               }elseif(
    $file_type == "image/x-png" || $file_type == "image/png"){

                   
    $new_img imagecreatefrompng($file_tmp);

               }elseif(
    $file_type == "image/gif"){

                   
    $new_img imagecreatefromgif($file_tmp);

               }

               
    //list width and height and keep height ratio.

               
    list($width$height) = getimagesize($file_tmp);

               
    $imgratio=$width/$height;

               if (
    $imgratio>1){

                  
    $newwidth $miniWidth;

                  
    $newheight $miniWidth/$imgratio;

               }else{

                     
    $newheight $miniWidth;

                     
    $newwidth $miniWidth*$imgratio;

               }

               
    //function for resize image.

               
    if (function_exists(imagecreatetruecolor)){

               
    $resized_img imagecreatetruecolor($newwidth,$newheight);

               }else{

                     die(
    "Error: Please make sure you have GD library ver 2+");

               }

               
    imagecopyresampled($resized_img$new_img0000$newwidth$newheight$width$height);

               
    //save image

               
    ImageJpeg ($resized_img,"$path_mini/$id.$file_ext");

               
    ImageDestroy ($resized_img);

               
    ImageDestroy ($new_img);


            }



            
    //upload the big image

            
    move_uploaded_file ($file_tmp"$path_big/$id.$file_ext");

            
    //echo "<br>Image Big: <a href=\"$path_big/$id.$file_ext\">$path_big/$id.$file_ext</a>";



            
    echo "<br><br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";

            
    $id $_SESSION['user_id'];
            

    $sql "UPDATE `users` SET image = '".$id.".".$file_ext."' WHERE ID=$id";
    mysql_query($sql) or die(mysql_error());



    }else{ 
    //if the form hasn't been submitted.



          //print the form

          
    echo "<script>

          function view_img(img_name){

             document[img_name].src = upForm.imgfile.value;

                document[img_name].width = 150;

          }

          </script>\n\n

          <br><h3>:: Browse an Image to Upload:</h3>\n

          <form method=\"post\" name=\"upForm\" enctype=\"multipart/form-data\" action=\"$_SERVER[PHP_SELF]\">\n

          <input type=\"file\" name=\"imgfile\" > <img src='' name='img_vv' width='0'><br>\n

          Image width will resize to <b>$img_thumb_width</b> with height ratio.

          <br><input type=\"Submit\" name=\"upForm\" value=\"Upload & Resize\">\n

          </form>

          <a href=\"view_gallery.php\">View Images</a>"
    ;

          }

         



    ?>
    config.php

    PHP Code:
    <?php

    #######################################
    //path where to store images
    $path_thumbs "images/thumbs";
    $path_big "images/big";
    $path_mini "images/mini";
    //the new width of the resized image.
    $img_thumb_width 200// in pixcel
    $img_mini_width 80;

    $extlimit "yes"//Do you want to limit the extensions of files uploaded (yes/no)
    //allowed Extensions
    $limitedext = array(".gif",".jpg",".png",".jpeg",".bmp");


    //check if folders are Writable or not
    //please CHOMD them 777
    if (!is_writeable($path_thumbs)){
       die (
    "Error: The directory <b>($path_thumbs)</b> is NOT writable");
    }
    if (!
    is_writeable($path_big)){
        die (
    "Error: The directory <b>($path_big)</b> is NOT writable");
    }
    if (!
    is_writeable($path_mini)){
        die (
    "Error: The directory <b>($path_mini)</b> is NOT writable");
    }



    ?>

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Add error checking. Turn on error_reporting(E_ALL) and var_dump() everything.

  • #3
    Regular Coder
    Join Date
    Nov 2006
    Posts
    601
    Thanks
    1
    Thanked 2 Times in 2 Posts
    i get no data showing up at all

    PHP Code:
    <?php



    session_start
    ();

    //load the config file

    include("config.php");

    require 
    '../settings.php';




    //if the for has submittedd

    if (isset($_POST['upForm'])){



           
    $file_type $_FILES['imgfile']['type'];

           
    $file_name $_FILES['imgfile']['name'];

           
    $file_size $_FILES['imgfile']['size'];

           
    $file_tmp $_FILES['imgfile']['tmp_name'];



           
    //check if you have selected a file.

           
    if(!is_uploaded_file($file_tmp)){

              echo 
    "Error: Please select a file to upload!. <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";

              exit(); 
    //exit the script and don't do anything else.

           
    }

           
    //check file extension

           
    $ext strrchr($file_name,'.');

           
    $ext strtolower($ext);

           if ((
    $extlimit == "yes") && (!in_array($ext,$limitedext))) {

              echo 
    "Wrong file extension.  <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";

              exit();

           }

           
    //get the file extension.

           
    $getExt explode ('.'$file_name);

           
    $file_ext $getExt[count($getExt)-1];



    //get users ID

        
    $id $_SESSION['user_id'];  

        





          

          
    //get the new width variable.

           
    $ThumbWidth $img_thumb_width;



           
    //keep image type

           
    if($file_size){

              if(
    $file_type == "image/pjpeg" || $file_type == "image/jpeg"){

                   
    $new_img imagecreatefromjpeg($file_tmp);

               }elseif(
    $file_type == "image/x-png" || $file_type == "image/png"){

                   
    $new_img imagecreatefrompng($file_tmp);

               }elseif(
    $file_type == "image/gif"){

                   
    $new_img imagecreatefromgif($file_tmp);

               }

               
    //list width and height and keep height ratio.

               
    list($width$height) = getimagesize($file_tmp);

               
    $imgratio=$width/$height;

               if (
    $imgratio>1){

                  
    $newwidth $ThumbWidth;

                  
    $newheight $ThumbWidth/$imgratio;

               }else{

                     
    $newheight $ThumbWidth;

                     
    $newwidth $ThumbWidth*$imgratio;

               }

               
    //function for resize image.

               
    if (function_exists(imagecreatetruecolor)){

               
    $resized_img imagecreatetruecolor($newwidth,$newheight);

               }else{

                     die(
    "Error: Please make sure you have GD library ver 2+");

               }

               
    imagecopyresampled($resized_img$new_img0000$newwidth$newheight$width$height);

               
    //save image

               
    ImageJpeg ($resized_img,"$path_thumbs/$id.$file_ext");

               
    ImageDestroy ($resized_img);

               
    ImageDestroy ($new_img);

               
    //print message

               //echo "<br>Image Thumb: <a href=\"$path_thumbs/$id.$file_ext\">$path_thumbs/$id.$file_ext</a>";

            
    }



           

    $miniWidth $img_mini_width;



           
    //keep image type

           
    if($file_size){

              if(
    $file_type == "image/pjpeg" || $file_type == "image/jpeg"){

                   
    $new_img imagecreatefromjpeg($file_tmp);

               }elseif(
    $file_type == "image/x-png" || $file_type == "image/png"){

                   
    $new_img imagecreatefrompng($file_tmp);

               }elseif(
    $file_type == "image/gif"){

                   
    $new_img imagecreatefromgif($file_tmp);

               }

               
    //list width and height and keep height ratio.

               
    list($width$height) = getimagesize($file_tmp);

               
    $imgratio=$width/$height;

               if (
    $imgratio>1){

                  
    $newwidth $miniWidth;

                  
    $newheight $miniWidth/$imgratio;

               }else{

                     
    $newheight $miniWidth;

                     
    $newwidth $miniWidth*$imgratio;

               }

               
    //function for resize image.

               
    if (function_exists(imagecreatetruecolor)){

               
    $resized_img imagecreatetruecolor($newwidth,$newheight);

               }else{

                     die(
    "Error: Please make sure you have GD library ver 2+");

               }

               
    imagecopyresampled($resized_img$new_img0000$newwidth$newheight$width$height);

               
    //save image

               
    ImageJpeg ($resized_img,"$path_mini/$id.$file_ext");

               
    ImageDestroy ($resized_img);

               
    ImageDestroy ($new_img);

               
    //print message

               //echo "<br>Image Thumb: <a href=\"$path_thumbs/$id.$file_ext\">$path_thumbs/$id.$file_ext</a>";

            
    }



            
    //upload the big image

            
    move_uploaded_file ($file_tmp"$path_big/$id.$file_ext");

            
    //echo "<br>Image Big: <a href=\"$path_big/$id.$file_ext\">$path_big/$id.$file_ext</a>";

    error_reporting(E_ALL);  var_dump(E_ALL);

            echo 
    "<br><br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";

            
    $id $_SESSION['user_id'];
            

    $sql "UPDATE `users` SET image = '".$id.".".$file_ext."' WHERE ID=$id";
    mysql_query($sql) or die(mysql_error());



    }else{ 
    //if the form hasn't been submitted.



          //print the form

          
    echo "<script>

          function view_img(img_name){

             document[img_name].src = upForm.imgfile.value;

                document[img_name].width = 150;

          }

          </script>\n\n

          <br><h3>:: Browse an Image to Upload:</h3>\n

          <form method=\"post\" name=\"upForm\" enctype=\"multipart/form-data\" action=\"$_SERVER[PHP_SELF]\">\n

          <input type=\"file\" name=\"imgfile\" > <img src='' name='img_vv' width='0'><br>\n

          Image width will resize to <b>$img_thumb_width</b> with height ratio.

          <br><input type=\"Submit\" name=\"upForm\" value=\"Upload & Resize\">\n

          </form>

          <a href=\"view_gallery.php\">View Images</a>"
    ;

          }

         



    ?>
    line 230 is the error_reporting(E_ALL) and var_dump()

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    You.... misunderstood. The error_reporting function goes at the top, and you var_dump the returns from functions, or variables which you expect to be correct.

  • #5
    Regular Coder
    Join Date
    Nov 2006
    Posts
    601
    Thanks
    1
    Thanked 2 Times in 2 Posts
    ok i get this end

    Notice: Use of undefined constant imagecreatetruecolor - assumed 'imagecreatetruecolor' in /home/runningp/public_html/members/uploader.php on line 122

    Notice: Use of undefined constant imagecreatetruecolor - assumed 'imagecreatetruecolor' in /home/runningp/public_html/members/uploader.php on line 196

    Warning: Wrong parameter count for var_dump() in /home/runningp/public_html/members/uploader.php on line 230

  • #6
    Regular Coder
    Join Date
    Nov 2006
    Posts
    601
    Thanks
    1
    Thanked 2 Times in 2 Posts
    thing is it works for line 122 cos i get my tumb image uploaded :S

  • #7
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Are you saying your problem is solved? Post your current code if it isn't.

  • #8
    Regular Coder
    Join Date
    Nov 2006
    Posts
    601
    Thanks
    1
    Thanked 2 Times in 2 Posts
    no it not.... thing is my images get uploaded to tumbs folder

    but it does not get uploaded to mini folder

    PHP Code:
    <?php



    session_start
    ();

    //load the config file

    include("config.php");

    require 
    '../settings.php';




    //if the for has submittedd

    if (isset($_POST['upForm'])){
    error_reporting(E_ALL);  


           
    $file_type $_FILES['imgfile']['type'];

           
    $file_name $_FILES['imgfile']['name'];

           
    $file_size $_FILES['imgfile']['size'];

           
    $file_tmp $_FILES['imgfile']['tmp_name'];



           
    //check if you have selected a file.

           
    if(!is_uploaded_file($file_tmp)){

              echo 
    "Error: Please select a file to upload!. <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";

              exit(); 
    //exit the script and don't do anything else.

           
    }

           
    //check file extension

           
    $ext strrchr($file_name,'.');

           
    $ext strtolower($ext);

           if ((
    $extlimit == "yes") && (!in_array($ext,$limitedext))) {

              echo 
    "Wrong file extension.  <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";

              exit();

           }

           
    //get the file extension.

           
    $getExt explode ('.'$file_name);

           
    $file_ext $getExt[count($getExt)-1];



    //get users ID

        
    $id $_SESSION['user_id'];  

        





          

          
    //get the new width variable.

           
    $ThumbWidth $img_thumb_width;



           
    //keep image type

           
    if($file_size){

              if(
    $file_type == "image/pjpeg" || $file_type == "image/jpeg"){

                   
    $new_img imagecreatefromjpeg($file_tmp);

               }elseif(
    $file_type == "image/x-png" || $file_type == "image/png"){

                   
    $new_img imagecreatefrompng($file_tmp);

               }elseif(
    $file_type == "image/gif"){

                   
    $new_img imagecreatefromgif($file_tmp);

               }

               
    //list width and height and keep height ratio.

               
    list($width$height) = getimagesize($file_tmp);

               
    $imgratio=$width/$height;

               if (
    $imgratio>1){

                  
    $newwidth $ThumbWidth;

                  
    $newheight $ThumbWidth/$imgratio;

               }else{

                     
    $newheight $ThumbWidth;

                     
    $newwidth $ThumbWidth*$imgratio;

               }

               
    //function for resize image.

               
    if (function_exists(imagecreatetruecolor)){

               
    $resized_img imagecreatetruecolor($newwidth,$newheight);

               }else{

                     die(
    "Error: Please make sure you have GD library ver 2+");

               }

               
    imagecopyresampled($resized_img$new_img0000$newwidth$newheight$width$height);

               
    //save image

               
    ImageJpeg ($resized_img,"$path_thumbs/$id.$file_ext");

               
    ImageDestroy ($resized_img);

               
    ImageDestroy ($new_img);

               
    //print message

               //echo "<br>Image Thumb: <a href=\"$path_thumbs/$id.$file_ext\">$path_thumbs/$id.$file_ext</a>";

            
    }



           

    $miniWidth $img_mini_width;



           
    //keep image type

           
    if($file_size){

              if(
    $file_type == "image/pjpeg" || $file_type == "image/jpeg"){

                   
    $new_img imagecreatefromjpeg($file_tmp);

               }elseif(
    $file_type == "image/x-png" || $file_type == "image/png"){

                   
    $new_img imagecreatefrompng($file_tmp);

               }elseif(
    $file_type == "image/gif"){

                   
    $new_img imagecreatefromgif($file_tmp);

               }

               
    //list width and height and keep height ratio.

               
    list($width$height) = getimagesize($file_tmp);

               
    $imgratio=$width/$height;

               if (
    $imgratio>1){

                  
    $newwidth $miniWidth;

                  
    $newheight $miniWidth/$imgratio;

               }else{

                     
    $newheight $miniWidth;

                     
    $newwidth $miniWidth*$imgratio;

               }

               
    //function for resize image.

               
    if (function_exists(imagecreatetruecolor)){

               
    $resized_img imagecreatetruecolor($newwidth,$newheight);

               }else{

                     die(
    "Error: Please make sure you have GD library ver 2+");

               }

               
    imagecopyresampled($resized_img$new_img0000$newwidth$newheight$width$height);

               
    //save image

               
    ImageJpeg ($resized_img,"$path_mini/$id.$file_ext");

               
    ImageDestroy ($resized_img);

               
    ImageDestroy ($new_img);

               
    //print message

               //echo "<br>Image Thumb: <a href=\"$path_thumbs/$id.$file_ext\">$path_thumbs/$id.$file_ext</a>";

            
    }



            
    //upload the big image

            
    move_uploaded_file ($file_tmp"$path_big/$id.$file_ext");

            
    //echo "<br>Image Big: <a href=\"$path_big/$id.$file_ext\">$path_big/$id.$file_ext</a>";

    var_dump();

            echo 
    "<br><br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";

            
    $id $_SESSION['user_id'];
            

    $sql "UPDATE `users` SET image = '".$id.".".$file_ext."' WHERE ID=$id";
    mysql_query($sql) or die(mysql_error());



    }else{ 
    //if the form hasn't been submitted.



          //print the form

          
    echo "<script>

          function view_img(img_name){

             document[img_name].src = upForm.imgfile.value;

                document[img_name].width = 150;

          }

          </script>\n\n

          <br><h3>:: Browse an Image to Upload:</h3>\n

          <form method=\"post\" name=\"upForm\" enctype=\"multipart/form-data\" action=\"$_SERVER[PHP_SELF]\">\n

          <input type=\"file\" name=\"imgfile\" > <img src='' name='img_vv' width='0'><br>\n

          Image width will resize to <b>$img_thumb_width</b> with height ratio.

          <br><input type=\"Submit\" name=\"upForm\" value=\"Upload & Resize\">\n

          </form>

          <a href=\"view_gallery.php\">View Images</a>"
    ;

          }

         



    ?>

  • #9
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    PHP Code:
    if (function_exists(imagecreatetruecolor)){ 
    Should be:
    PHP Code:
    if (function_exists('imagecreatetruecolor')){ 
    That is what is causing those undefined constant notices. The warning is because you still aren't using var_dump() properly. Go to the point where you think you have a problem, and
    PHP Code:
    var_dump($the$variables$you$are$using$so$you$can$see$what$their$values$are); 
    You can also
    PHP Code:
    var_dump(functions_you_are_calling($with$their$params)); 

  • #10
    Regular Coder
    Join Date
    Nov 2006
    Posts
    601
    Thanks
    1
    Thanked 2 Times in 2 Posts
    thats the thing it could be any of this
    PHP Code:
    $miniWidth $img_mini_width;



           
    //keep image type

           
    if($file_size){

              if(
    $file_type == "image/pjpeg" || $file_type == "image/jpeg"){

                   
    $new_img imagecreatefromjpeg($file_tmp);

               }elseif(
    $file_type == "image/x-png" || $file_type == "image/png"){

                   
    $new_img imagecreatefrompng($file_tmp);

               }elseif(
    $file_type == "image/gif"){

                   
    $new_img imagecreatefromgif($file_tmp);

               }

               
    //list width and height and keep height ratio.

               
    list($width$height) = getimagesize($file_tmp);

               
    $imgratio=$width/$height;

               if (
    $imgratio>1){

                  
    $newwidth $miniWidth;

                  
    $newheight $miniWidth/$imgratio;

               }else{

                     
    $newheight $miniWidth;

                     
    $newwidth $miniWidth*$imgratio;

               }

               
    //function for resize image.

               
    if (function_exists(imagecreatetruecolor)){

               
    $resized_img imagecreatetruecolor($newwidth,$newheight);

               }else{

                     die(
    "Error: Please make sure you have GD library ver 2+");

               }

               
    imagecopyresampled($resized_img$new_img0000$newwidth$newheight$width$height);

               
    //save image

               
    ImageJpeg ($resized_img,"$path_mini/$id.$file_ext");

               
    ImageDestroy ($resized_img);

               
    ImageDestroy ($new_img); 

    but its exactly the same as the 1 that is working lol just has 2 things different... 1 being the size down to 80px and the 2nd been the path its sending it too

  • #11
    Regular Coder
    Join Date
    Nov 2006
    Posts
    601
    Thanks
    1
    Thanked 2 Times in 2 Posts
    ok is it because im trying to resize the same image 2 times???

    because it has worked yesterday and today it does not work... so i deleted my old picture in mini folder and when i ran the upload script it uploaded the picture i uploaded yesterday!

  • #12
    Regular Coder
    Join Date
    Nov 2006
    Posts
    601
    Thanks
    1
    Thanked 2 Times in 2 Posts
    ok new error lol..

    im trying to upload this code im my page by useing this 'index.php?page=upload' after the submit button is clicked yet i just get a blank page no errors or anything..anyone know why??
    PHP Code:
    <?php



    session_start
    ();

    //load the config file

    include("config.php");

    require 
    '../settings.php';




    //if the for has submittedd

    if (isset($_POST['upForm'])){
    error_reporting(E_ALL);  


           
    $file_type $_FILES['imgfile']['type'];

           
    $file_name $_FILES['imgfile']['name'];

           
    $file_size $_FILES['imgfile']['size'];

           
    $file_tmp $_FILES['imgfile']['tmp_name'];



           
    //check if you have selected a file.

           
    if(!is_uploaded_file($file_tmp)){

              echo 
    "Error: Please select a file to upload!";

              exit(); 
    //exit the script and don't do anything else.

           
    }

           
    //check file extension

           
    $ext strrchr($file_name,'.');

           
    $ext strtolower($ext);

           if ((
    $extlimit == "yes") && (!in_array($ext,$limitedext))) {

              echo 
    "Wrong file extension.  <br>";

              exit();

           }

           
    //get the file extension.

           
    $getExt explode ('.'$file_name);

           
    $file_ext $getExt[count($getExt)-1];



    //get users ID

        
    $id $_SESSION['user_id'];  

        





          

          
    //get the new width variable.

           
    $miniWidth $img_mini_width;



           
    //keep image type

           
    if($file_size){

              if(
    $file_type == "image/pjpeg" || $file_type == "image/jpeg"){

                   
    $new_img imagecreatefromjpeg($file_tmp);

               }elseif(
    $file_type == "image/x-png" || $file_type == "image/png"){

                   
    $new_img imagecreatefrompng($file_tmp);

               }elseif(
    $file_type == "image/gif"){

                   
    $new_img imagecreatefromgif($file_tmp);

               }

               
    //list width and height and keep height ratio.

               
    list($width$height) = getimagesize($file_tmp);

               
    $imgratio=$width/$height;

               if (
    $imgratio>1){

                  
    $newwidth $miniWidth;

                  
    $newheight $miniWidth/$imgratio;

               }else{

                     
    $newheight $miniWidth;

                     
    $newwidth $miniWidth*$imgratio;

               }

               
    //function for resize image.

               
    if (function_exists('imagecreatetruecolor')){  

               
    $resized_img imagecreatetruecolor($newwidth,$newheight);

               }else{

                     die(
    "Error: Please make sure you have GD library ver 2+");

               }

               
    imagecopyresampled($resized_img$new_img0000$newwidth$newheight$width$height);

               
    //save image

               
    ImageJpeg ($resized_img,"$path_mini/$id.$file_ext");

             

               
    //print message

               //echo "<br>Image Thumb: <a href=\"$path_thumbs/$id.$file_ext\">$path_thumbs/$id.$file_ext</a>";

            
    }


      
    $ThumbWidth $img_thumb_width;



           
    //keep image type

           
    if($file_size){

              if(
    $file_type == "image/pjpeg" || $file_type == "image/jpeg"){

                   
    $new_img imagecreatefromjpeg($file_tmp);

               }elseif(
    $file_type == "image/x-png" || $file_type == "image/png"){

                   
    $new_img imagecreatefrompng($file_tmp);

               }elseif(
    $file_type == "image/gif"){

                   
    $new_img imagecreatefromgif($file_tmp);

               }

               
    //list width and height and keep height ratio.

               
    list($width$height) = getimagesize($file_tmp);

               
    $imgratio=$width/$height;

               if (
    $imgratio>1){

                  
    $newwidth $ThumbWidth;

                  
    $newheight $ThumbWidth/$imgratio;

               }else{

                     
    $newheight $ThumbWidth;

                     
    $newwidth $ThumbWidth*$imgratio;

               }

               
    //function for resize image.

               
    if (function_exists('imagecreatetruecolor')){  

               
    $resized_img imagecreatetruecolor($newwidth,$newheight);

               }else{

                     die(
    "Error: Please make sure you have GD library ver 2+");

               }

               
    imagecopyresampled($resized_img$new_img0000$newwidth$newheight$width$height);

               
    //save image

               
    ImageJpeg ($resized_img,"$path_thumbs/$id.$file_ext");

               
    ImageDestroy ($resized_img);

               
    ImageDestroy ($new_img);
           


            }



            
    //upload the big image

            
    move_uploaded_file ($file_tmp"$path_big/$id.$file_ext");

            
    //echo "<br>Image Big: <a href=\"$path_big/$id.$file_ext\">$path_big/$id.$file_ext</a>";


            
    echo "<br><br>";

            
    $id $_SESSION['user_id'];
            

    $sql "UPDATE `users` SET image = '".$id.".".$file_ext."' WHERE ID=$id";
    mysql_query($sql) or die(mysql_error());

    }

       
    ?>

  • #13
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    841
    Thanks
    15
    Thanked 9 Times in 9 Posts

    Exclamation

    I have a script that does similar to this... it's a little more compact though. A basic run down of it is.

    1. I upload 1 file to a temp folder. This is the base image I use to create the thumbnails.
    2. I create the main image (which is a smaller version of the original).

    3. I create the thumbnail image, which is created from the original image.

    4. I unlink the temp base image to remove it from the server (as to conserve space).

    My Code... it's a little rough because of the mysql and various error checking, and type checkings but she works good. I added comments so you can understand it better.
    PHP Code:
    function addImage($id,$type="") {
        global 
    $_FILES$config;
        
    //Path to oringinal file.
        
    $start $config["path"]."examples/web_design/".$id.".jpg";
        
    //Upload original file
        
    copy($_FILES['image']['tmp_name'], $start);
        
    //Path to larger thumbnail
        
    $large $config["path"]."examples/web_design/".$id."_lg.jpg";
        
    //Path to smaller thumbnail
        
    $small $config["path"]."examples/web_design/".$id."_sm.jpg";
        
    //Path to even smaller thumbnail
        
    $featured $config["path"]."examples/web_design/featured/".$id.".jpg";
        
    //Call thumbnail function to make thumbnails.
        // thumbnail( original , width , newpath , leave empty)
        
    $copy thumbnail($start,350,"$large","");
        
    $copy thumbnail($start,250,"$small","");
        
    $copy thumbnail($start,151,"$featured","");
        
    //Remove the oringinal file to conserve space
        
    unlink($start);
        
    // return if successful for last thumbnail copy
        
    if($copy) {
            return 
    "true";
        }
        else {
            return 
    "false";
        }
    }
    //Thumbnail function
    //Takes original image and resizes it to specified size
    function thumbnail($i,$nw,$p,$nn) {  
        
    $img=imagecreatefromjpeg("$i");  
        
    $ow=imagesx($img);  
        
    $oh=imagesy($img);  
        
    $scale=$nw/$ow
        
    $nh=ceil($oh*$scale); 
        
    $newimg=imagecreatetruecolor($nw,$nh);  
        
    imagecopyresampled($newimg,$img,0,0,0,0,$nw,$nh,$ow,$oh); 
        
    imagejpeg($newimg$p.$nn);  
        return 
    true;  


    //Then to call the function
    //$id = the image id (I use mysql to keep information about the photo so the $id corresponds to my mysql
    //id for the image info).  You can use whatever you want as the $id will be the new file names,
    //with appropriate prefixs to distinguish the sizes of thumnails.
    $upload addImage($id);
    if(
    $upload == "true") {
      echo 
    "Success";
    }
    else {
      echo 
    "Failure";

    Last edited by dniwebdesign; 05-18-2008 at 09:14 PM. Reason: Added function call
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com


  •  

    Posting Permissions

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