$moveToDir - Directory to save the resized image $uploadDir - Directory of the original image $new_height - Height of the resized photo (maximum) $new_width - Width of the resized photo (maximum) These are the parameters it needs: $image_name - Name of the image which is uploaded I've copied it below, might need a bit of tinkering! (It does required the GD library) I had written a peice of code like this for another project I've done. 160 x 236 (rounded) is the correct answer. We do the math according to the solution proposed by PHP.net: It is better (as it maintain the aspect ratio), but it exceeded the maximum accepted width. The next solution proposed is the following: if($old_x < $old_y) Let's do some math taking the accepted solution: if($old_x < $old_y) Let's say we have an image of 1630 x 2400 pixels that we want to be auto resized keeping the aspect ratio to 160 x 240. There are other answers (with examples) on stackoverflow to similar questions (the same question formulated in a different manner) that suffer of the same problem. Please note the relevant lines from the example given on PHP.net website: $ratio_orig = $width_orig/$height_orig Any calculation should reflect this difference. The reason is simple: there will be cases when the ratio of the source image and the ratio of the destination image will be different. $("#dont-crop").Actually the accepted solution it is not the correct solution. If user chooses not to crop and presses dont crop or press close or cancel When the crop button is clicked crop image and set it to your preview and then close modal Now, let’s take a look at the code below.Ĭonst cropModal = $('#crop-modal').modal(ĬropModal.on('shown.bs.modal', cropModal, () => cropper = new Cropper(modalImgElement, options)) Similarly, if user choose not to crop return image name as it is and copy it to folder and you can save the name into the database. So if user crops an image save the cropped image and return the name of image as an input and save it. User choose both crop and not to crop the image.When user select’s an image and open modal to allow user to crop the image.I am writing all the steps inside the code itself so before just copying and pasting the code in your project and using. So, let’s divide into step by step on what are we going to do. You just have to make the ajax call and save it to database. However we are not going to save it into the database but you will get the value that you need to save. Use PHP Image Crop Library and Integrate (CROPPER JS)įinally, its time to integrate php image crop library and crop image while you select and export it and save it to database. So here is my basic structure of the page in html code. So what will be needing is a input which will upload an image and we will see how we will use cropper JS which in PHP image crop library to crop image and send it to database. Create Basic Layout Structureįirst create a basic layout structure for your project. Let’s take it step by step so that it would be very easier to understand on how to implement this in your project. Use PHP image crop library and integrate it.Import jQuery, bootstrap, cropper js libraries in your project. ![]()
0 Comments
Leave a Reply. |