phpbeginner Posted April 17, 2007 Share Posted April 17, 2007 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 https://forums.phpfreaks.com/topic/47432-where-to-put-code/ Share on other sites More sharing options...
phpbeginner Posted April 17, 2007 Author Share Posted April 17, 2007 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 https://forums.phpfreaks.com/topic/47432-where-to-put-code/#findComment-231625 Share on other sites More sharing options...
trq Posted April 17, 2007 Share Posted April 17, 2007 This needs to go in the file your forms action points to. Link to comment https://forums.phpfreaks.com/topic/47432-where-to-put-code/#findComment-231628 Share on other sites More sharing options...
phpbeginner Posted April 17, 2007 Author Share Posted April 17, 2007 Thanks thorpe ! Can I put an include in the file that my form action points to or do I need to use the code itself ? Link to comment https://forums.phpfreaks.com/topic/47432-where-to-put-code/#findComment-231633 Share on other sites More sharing options...
Trium918 Posted April 17, 2007 Share Posted April 17, 2007 You can either use a function or the include. Either way you would still have to called one or the other, but it is easy to maintain because if you have a erro or need to update you know where to make corrections. Link to comment https://forums.phpfreaks.com/topic/47432-where-to-put-code/#findComment-231635 Share on other sites More sharing options...
phpbeginner Posted April 17, 2007 Author Share Posted April 17, 2007 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 https://forums.phpfreaks.com/topic/47432-where-to-put-code/#findComment-231674 Share on other sites More sharing options...
trq Posted April 17, 2007 Share Posted April 17, 2007 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 https://forums.phpfreaks.com/topic/47432-where-to-put-code/#findComment-231686 Share on other sites More sharing options...
phpbeginner Posted April 17, 2007 Author Share Posted April 17, 2007 Added the if(isset($_POST['Submit'])) to my include file as suggested but still no go. This part look OK ? if (is_uploaded_file ($flFile)) include('upload.php'); { which goes into my results.... Link to comment https://forums.phpfreaks.com/topic/47432-where-to-put-code/#findComment-231691 Share on other sites More sharing options...
trq Posted April 17, 2007 Share Posted April 17, 2007 but still no go. Thats far too vague. What is happening? Whats not happening? Maybe you should post both your form and your action scripts. Link to comment https://forums.phpfreaks.com/topic/47432-where-to-put-code/#findComment-231722 Share on other sites More sharing options...
phpbeginner Posted April 18, 2007 Author Share Posted April 18, 2007 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 https://forums.phpfreaks.com/topic/47432-where-to-put-code/#findComment-231731 Share on other sites More sharing options...
phpbeginner Posted April 18, 2007 Author Share Posted April 18, 2007 any suggestions on this.....I posted what I have. Link to comment https://forums.phpfreaks.com/topic/47432-where-to-put-code/#findComment-232486 Share on other sites More sharing options...
phpbeginner Posted April 18, 2007 Author Share Posted April 18, 2007 Can someone assist me with this ? I really, really, really hate the thoughts of storing my images in database but may have to if I can't get this working. Link to comment https://forums.phpfreaks.com/topic/47432-where-to-put-code/#findComment-232653 Share on other sites More sharing options...
phpbeginner Posted April 18, 2007 Author Share Posted April 18, 2007 quietly bumping....and for info, the filename is saving to the db, but the images are not uploading. Link to comment https://forums.phpfreaks.com/topic/47432-where-to-put-code/#findComment-232730 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.