Jump to content

Where to put code ?


phpbeginner

Recommended Posts

I have a form and the action goes to a commit page to update my db. I am storing basic info an also image names which works fine. I am trying to resize the images upon upload and my question is can I add in the resize code on the form or do I have to add this on the commit page ?

 

I've tried both but to no avail, so I will concentrate on for or commit page if I knew which one.

Link to comment
Share on other sites

Please excuse my ignorance in this matter but I am trying  ???

 

Okay what I have is a form, on save it goes to commit page to save my data, including filename for an image. All works fine so far. Now to actually upload and resize the image using my form can I use the include script (posted below) within my form to upload this image ? If so, do I just have to include this in the head of my page or within the form itself.

 

Any help would be appreciated.

 

<?php 
   if(isset($_POST['Save'])) 
   { 
       $size = 150; // the thumbnail height 
       $filedir = 'test/'; // the directory for the original image 
       $thumbdir = 'test/'; // the directory for the thumbnail image 
       $prefix = ''; // the prefix to be added to the original name 
       $maxfile = '2000000'; 
       $mode = '0666'; 
       $userfile_name = $_FILES['image']['name']; 
       $userfile_tmp = $_FILES['image']['tmp_name']; 
       $userfile_size = $_FILES['image']['size']; 
       $userfile_type = $_FILES['image']['type']; 
       if (isset($_FILES['image']['name']))  
       { 
           $prod_img = $filedir.$userfile_name; 
           $prod_img_thumb = $thumbdir.$prefix.$userfile_name; 
          move_uploaded_file($userfile_tmp, $prod_img); 
           chmod ($prod_img, octdec($mode)); 
           $sizes = getimagesize($prod_img); 
           //$aspect_ratio = $sizes[1]/$sizes[0];  
           //if ($sizes[1] <= $size) 
         { 
               $new_width = 150; 
               $new_height = 150; 
           //}//else{ 
               //$new_height = $size; 
               //$new_width = abs($new_height/$aspect_ratio); 
           //} 
           $destimg=ImageCreateTrueColor($new_width,$new_height) 
               or die('Problem In Creating image'); 
           $srcimg=ImageCreateFromJPEG($prod_img) 
               or die('Problem In opening Source Image'); 
		   }
           if(function_exists('imagecopyresampled')) 
           { 
               imagecopyresampled($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg)) 
               or die('Problem In resizing'); 
           }else{ 
               Imagecopyresized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg)) 
               or die('Problem In resizing'); 
           } 
           ImageJPEG($destimg,$prod_img_thumb,90) 
               or die('Problem In saving'); 
           imagedestroy($destimg); 
      
   }
   ?> 

Link to comment
Share on other sites

Okay, this is what I have added to my action page.....

 

 if (is_uploaded_file ($flFile))
	include('upload.php');
	{

 

On my resize include script (upload.php), I would not think I would need any of this

if(isset($_POST['Submit'])) 

as it has already been submitted and gone to an action script......Am I right on this or wrong ? SO here is the upload.php which still is not uploading the files to the directory.

 

<?php 

   { 
       $size = 150; // the thumbnail height 
       $filedir = 'teampics/thumbs'; // the directory for the original image 
       $thumbdir = 'teampics/'; // the directory for the thumbnail image 
       $prefix = ''; // the prefix to be added to the original name 
       $maxfile = '2000000'; 
       $mode = '0666'; 
       $userfile_name = $_FILES['image']['name']; 
       $userfile_tmp = $_FILES['image']['tmp_name']; 
       $userfile_size = $_FILES['image']['size']; 
       $userfile_type = $_FILES['image']['type']; 
       if (isset($_FILES['image']['name']))  
       { 
           $prod_img = $filedir.$userfile_name; 
           $prod_img_thumb = $thumbdir.$prefix.$userfile_name; 
          move_uploaded_file($userfile_tmp, $prod_img); 
           chmod ($prod_img, octdec($mode)); 
           $sizes = getimagesize($prod_img); 
           //$aspect_ratio = $sizes[1]/$sizes[0];  
           //if ($sizes[1] <= $size) 
         { 
               $new_width = 150; 
               $new_height = 150; 
           //}//else{ 
               //$new_height = $size; 
               //$new_width = abs($new_height/$aspect_ratio); 
           //} 
           $destimg=ImageCreateTrueColor($new_width,$new_height) 
               or die('Problem In Creating image'); 
           $srcimg=ImageCreateFromJPEG($prod_img) 
               or die('Problem In opening Source Image'); 
		   }
           if(function_exists('imagecopyresampled')) 
           { 
               imagecopyresampled($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg)) 
               or die('Problem In resizing'); 
           }else{ 
               Imagecopyresized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg)) 
               or die('Problem In resizing'); 
           } 
           ImageJPEG($destimg,$prod_img_thumb,90) 
               or die('Problem In saving'); 
           imagedestroy($destimg); 
      } 
        
   } 
   ?> 

Link to comment
Share on other sites

Am I right on this or wrong ?

 

Nope, your wrong. The if statement ensures that your script will not run unless the form has been submitted to it. Thus avoiding all sorts of errors if some happens to request this page without submitting the required form.

Link to comment
Share on other sites

The filename is being uploaded to DB. The file itself is not uploading. Here is what I have as far as a form I have a form which I can both edit by selecting a record or add a new record.

 

Here is form for adding a new record......

 

 <form name='editDeleteSave' action='commit.php' method='post' enctype='multipart/form-data'>
                                                        <table>
						<tr>
                                
                                    <p>Upload Image:</p> 
                                </td>
                                <td>
                                    <input type='file' value='<?php echo($rowPhotos[2]); ?>' name='flFile'><?php echo($rowPhotos[2]); ?>
                                </td>
                            </tr>
                                                      
                             <tr>
                                
                                <td><input type='hidden' value='' name='saveDelete'>
                                <input type='hidden' value='photos' name='page'>
                                <input type='hidden' value=<?php echo($numPhotos); ?> name='numPhotos'></td>

                            </tr>

                            <?php
                                if($numPhotos!=-1)
                              {
                                   echo("<tr><td colspan='2' align='center'><input type='button' name='edit' value='Save Changes' onClick=\"saveChanges('edit');\"><input type='button' name='delete' value='Delete' onClick=\"saveChanges('delete');\"></td></tr>");
                              }
                              else
                              {
                                   echo("<tr><td colspan='2' align='center'><input type='button' name='save' value='    Save    ' onClick=\"saveChanges('save');\"></td></tr>");
                              }

                            ?>

 

Here is the action script for that area....

if($page=='photos')	

{

   if($saveDel=='delete')
   {
            $rsFileName=mysql_query("select uploadedfile from tblphotos where id=" . $numPhotos);

       if(mysql_num_rows($rsFileName)!=0)
       {
           $rowFileName=mysql_fetch_row($rsFileName);
           
       }

       $result=mysql_query("delete from tblphotos where id=" . $numPhotos);
   }
   elseif($saveDel=='edit')
   {
    
       
        if (is_uploaded_file ($flFile))
	include('uploadpic.php');
	{

            $result=mysql_query("update tblphotos set team='" . $txtTeam . "', uploadedfile='" . $flFile_name . "', title='" . $txtTitle . "', description='" . $txtDescription . "' where id=" . $numPhotos);
        }

   }
   else
   {

       $result=mysql_query("insert into tblphotos(team,uploadedfile,title,description) values('" . $txtTeam . "','" . $flFile_name . "','" . $txtTitle . "','" . $txtDescription . "')");

   }

    echo("<script language='javascript'>location.href='jhfksa.php'</script>");

 

 

and the include script.....

<?php 
   if(isset($_POST['Submit'])) 
   { 
       $size = 150; // the thumbnail height 
       $filedir = 'test/'; // the directory for the original image 
       $thumbdir = 'test/'; // the directory for the thumbnail image 
       $prefix = ''; // the prefix to be added to the original name 
       $maxfile = '2000000'; 
       $mode = '0666'; 
       $userfile_name = $_FILES['image']['name']; 
       $userfile_tmp = $_FILES['image']['tmp_name']; 
       $userfile_size = $_FILES['image']['size']; 
       $userfile_type = $_FILES['image']['type']; 
       if (isset($_FILES['image']['name']))  
       { 
           $prod_img = $filedir.$userfile_name; 
           $prod_img_thumb = $thumbdir.$prefix.$userfile_name; 
          move_uploaded_file($userfile_tmp, $prod_img); 
           chmod ($prod_img, octdec($mode)); 
           $sizes = getimagesize($prod_img); 
           //$aspect_ratio = $sizes[1]/$sizes[0];  
           //if ($sizes[1] <= $size) 
         { 
               $new_width = 150; 
               $new_height = 150; 
           //}//else{ 
               //$new_height = $size; 
               //$new_width = abs($new_height/$aspect_ratio); 
           //} 
           $destimg=ImageCreateTrueColor($new_width,$new_height) 
               or die('Problem In Creating image'); 
           $srcimg=ImageCreateFromJPEG($prod_img) 
               or die('Problem In opening Source Image'); 
		   }
           if(function_exists('imagecopyresampled')) 
           { 
               imagecopyresampled($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg)) 
               or die('Problem In resizing'); 
           }else{ 
               Imagecopyresized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg)) 
               or die('Problem In resizing'); 
           } 
           ImageJPEG($destimg,$prod_img_thumb,90) 
               or die('Problem In saving'); 
           imagedestroy($destimg); 
      }
?>

 

On the include script I have tried submit, edit and save......I appreciate the help thorpe !

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.