kamal213 Posted September 8, 2011 Share Posted September 8, 2011 Hi Guys, I have this script below which inserts the file name and path into my datebase and uploads the file into the customers folders (which is creates dynamically). The is no error checking as I want it to be as flexible as possible but mostly(99.999%) the files I upload are JPEG and PDF's. I would like my script to be able to resize the files above automatically to be smaller. Please is this possible thanks! Here is script: <?php //Will create a directory once a customer is clicked and will not if page is refreshed an so forth if (file_exists('customerUploads/' . $check_id . ', ' . $c_name . '')) { } else { mkdir('customerUploads/' . $check_id . ', ' . $c_name . ''); } ?> <?php //This php block of code will takecare of inserting the upload variables into the db if(isset($_POST['submitbutton'])) { $target_path = 'customerUploads/' . $check_id . ', ' . $c_name . '/'; $target_path = str_replace("'","",$target_path); $target_path = $target_path . basename( $_FILES['upload']['name']); $manager= mysql_real_escape_string($_POST['username']); $upload = $_FILES['upload']['name']; $upload = str_replace("'","",$upload); $check_id = mysql_real_escape_string($_POST['id']); $submitbutton= mysql_real_escape_string($_POST['submitbutton']); if($submitbutton) { if($manager&&$upload) { if (file_exists($target_path)) { echo $_FILES["upload"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["upload"]["tmp_name"],$target_path); echo "Stored in: " . 'customerUploads/' . $check_id . ', ' . $c_name . '/' . $_FILES["upload"]["name"]; $insert=mysql_query("INSERT INTO img_up (username,upload,id,target_path,img_date) VALUES ('$manager','$upload','$check_id','$target_path', now()) "); // Where the file is going to be placed $target_path = 'customerUploads/' . $check_id . ', ' . $c_name . '/'; /* Add the original filename to our target path. Result is "uploads/filename.extension" */ $target_path = $target_path . basename( $_FILES['upload']['name']); $target_path = 'customerUploads/' . $check_id . ', ' . $c_name . '/'; $target_path = $target_path . basename( $_FILES['upload']['name']); if (file_exists($target_path)) { echo $_FILES["upload"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["upload"]["tmp_name"],$target_path); echo "Stored in: " . 'customerUploads/' . $check_id . ', ' . $c_name . '/' . $_FILES["upload"]["name"]; } } } else { echo "There was an error uploading the file, please try again!"; } } header("location: mainupload_complete.php?id=$check_id"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/246695-php-resize-any-uploaded-file/ Share on other sites More sharing options...
trq Posted September 8, 2011 Share Posted September 8, 2011 Where are you stuck? And what exactly do you mean by resizing them to be smaller? Do you mean file size or image dimensions? Quote Link to comment https://forums.phpfreaks.com/topic/246695-php-resize-any-uploaded-file/#findComment-1266769 Share on other sites More sharing options...
kamal213 Posted September 8, 2011 Author Share Posted September 8, 2011 Thanks for getting back! The image dimension exactly. Basically after the file have been uploaded I display a link in the front-end were user can click to view the file, so the problems is when user click on the link (especially pdf's) its open and its about 230% zoom and they keep changing it to about 80-90%. I want to make it smaller automatically to about 80-90% save them the hassle of doing it manually everytime so i figure have to somehow resize the dimension so it looks smaller when users open the file. Quote Link to comment https://forums.phpfreaks.com/topic/246695-php-resize-any-uploaded-file/#findComment-1266771 Share on other sites More sharing options...
voip03 Posted September 8, 2011 Share Posted September 8, 2011 EXAMPLE <?php // The file $filename = 'test.jpg'; $percent = 0.5; // Content type header('Content-Type: image/jpeg'); // Get new dimensions list($width, $height) = getimagesize($filename); $new_width = $width * $percent; $new_height = $height * $percent; // Resample $image_p = imagecreatetruecolor($new_width, $new_height); $image = imagecreatefromjpeg($filename); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); // Output imagejpeg($image_p, null, 100); ?> http://php.net/manual/en/function.imagecopyresampled.php Quote Link to comment https://forums.phpfreaks.com/topic/246695-php-resize-any-uploaded-file/#findComment-1266772 Share on other sites More sharing options...
kamal213 Posted September 8, 2011 Author Share Posted September 8, 2011 Thanks for your reply! This seems like its from the GD Library I would prefer ImageMagik as it allows PDF'S Quote Link to comment https://forums.phpfreaks.com/topic/246695-php-resize-any-uploaded-file/#findComment-1266775 Share on other sites More sharing options...
voip03 Posted September 8, 2011 Share Posted September 8, 2011 Do you want the code or guidance Quote Link to comment https://forums.phpfreaks.com/topic/246695-php-resize-any-uploaded-file/#findComment-1266797 Share on other sites More sharing options...
trq Posted September 8, 2011 Share Posted September 8, 2011 PDF's are not images. Quote Link to comment https://forums.phpfreaks.com/topic/246695-php-resize-any-uploaded-file/#findComment-1266815 Share on other sites More sharing options...
kamal213 Posted September 8, 2011 Author Share Posted September 8, 2011 Is there a way2 resize them? to make the appear smaller automatically when you open them? Quote Link to comment https://forums.phpfreaks.com/topic/246695-php-resize-any-uploaded-file/#findComment-1266831 Share on other sites More sharing options...
litebearer Posted September 8, 2011 Share Posted September 8, 2011 Might take a look at this... http://blog.gilbertconsulting.com/2009/08/how-to-force-pdf-to-open-at-particular.html Quote Link to comment https://forums.phpfreaks.com/topic/246695-php-resize-any-uploaded-file/#findComment-1266834 Share on other sites More sharing options...
radiations3 Posted September 8, 2011 Share Posted September 8, 2011 WORKING CODE EXAMPLE AS PER YOUR ISSUE http://www.codingmix.com/2011/01/php-gd-upload-image-downsize-compress.html Quote Link to comment https://forums.phpfreaks.com/topic/246695-php-resize-any-uploaded-file/#findComment-1266992 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.