lingo5 Posted March 22, 2013 Share Posted March 22, 2013 (edited) hi, this is my script to upload and image, change the file extension to lowercase and save the path to my db....to which I need to add a function to resize th image to 750px wide before saving it....please help $uploadDir = '../uploads/'; if(isset($_POST['upload'])) { fforeach ($_FILES as $file) { $fileName = strtolower($file['name']); $tmpName = $file['tmp_name']; $fileSize = $file['size']; $fileType = $file['type']; if($fileName==""){ $filePath = '../uploads/img/none.jpg'; } else{ $filePath = $uploadDir . $fileName; } ////assigning random name //// get the file extension first $ext = substr(strrchr($fileName, "."), 1); //// make the random file name $randName = md5(rand() * time()); //// and now we have the unique file name for the upload file $filePath = $uploadDir . $randName . '.' . $ext; // Replace spaces with a '_' $filePath = str_replace(" ", "_", $filePath); $result = move_uploaded_file($tmpName, $filePath); if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); $filePath = addslashes($filePath); } $fileinsert[]=$filePath; } Edited March 22, 2013 by lingo5 Quote Link to comment https://forums.phpfreaks.com/topic/276035-please-help-resiing-image/ Share on other sites More sharing options...
Christian F. Posted March 22, 2013 Share Posted March 22, 2013 (edited) For this you'll have to use imagecopyresampled, which will allow you to copy and scale the image. Prior to this, however, you'll need to calculate the correct new dimensions of the scaled down version. Normally this is done by finding which of the dimensions has the greatest difference, in percent, compared to the maximum size for that particular side. This you figure out by taking the actual size and dividing it by the maximum size, for both dimensions. Then compare the results against each other, and select the biggest number out of the two. Next you'll need to divide the actual dimensions on this number, to get the target dimensions for the new image. At which point you're ready to use the above function, to get a nicely scaled picture, with the same aspect ratios as the original. PS: If you only need it to be less than 750 pixels wide, you can just check if the width is bigger. If it is, calculate the difference ratio for the width, using the same method as described above, and use it straight away. No need to calculate the ratio for the height. Edited March 22, 2013 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/276035-please-help-resiing-image/#findComment-1420392 Share on other sites More sharing options...
lingo5 Posted March 22, 2013 Author Share Posted March 22, 2013 thanks Christian Quote Link to comment https://forums.phpfreaks.com/topic/276035-please-help-resiing-image/#findComment-1420399 Share on other sites More sharing options...
Psycho Posted March 23, 2013 Share Posted March 23, 2013 1. Get the size of the image: getimagesize() 2. If image size < 750, do nothing - exit 3. Determine the percentage for the new size: 750 divided by current width 4. Determine the new height using current height times the percentage above 5. Use imagecopyresampled() to resize the image. Example #1 and the information above should be enough to get started. Quote Link to comment https://forums.phpfreaks.com/topic/276035-please-help-resiing-image/#findComment-1420416 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.