TechnoDiver Posted August 5, 2021 Share Posted August 5, 2021 Good Morning, Freaks, I hope you're all well. I've a question - I've been researching coding image resize functionality. While looking into the functions I'd need to do this I came across very similar code used in examples from 3 different sources so decided this was good code to learn from. So I tweeked it a bit and put it into a class method -> public function imageResize($target, $newcopy, $w, $h, $ext) { list($orig_w, $orig_h) = getimagesize($target); $scale_ratio = $orig_w/$orig_h; if(($w / $h) > $scale_ratio) { $w = $h * $scale_ratio; } else { $h = $w / $scale_ratio; } $img = ""; if($ext == "gif" || $ext == "GIF") { $img = imagecreatefromgif($target); } else if($ext == "png" || $ext == "PNG") { $img = imagecreatefrompng($target); } else if($ext == "jpg" || $ext == "JPG" || $ext == "jpeg" || $ext == "JPEG") { $img = imagecreatefromjpeg($target); } $create_tci = imagecreatetruecolor($w, $h); imagecopyresampled($create_tci, $img, 0, 0, 0, 0, $w, $h, $orig_w, $orig_h); imagejpeg($create_tci, $newcopy, 80); } and then connected it to a button -> <?php require("assets/initializations.php"); if(isset($_POST['upload_image'])) { $image_obj = new Image($conn, $user); $image_obj->imageUpload(); } else if(isset($_POST['resize_image'])) { mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT); $image_obj = new Image($conn, $user); $kaboom = explode(".", $image_name); //object params $image_ext = $kaboom[-1]; $target_image = "/opt/lampp/htdocs/site/admin/img/$image_name"; $resized_image = "/opt/lampp/htdocs/site/admin/img/resized_$image_name"; $max_w = 150; $max_h = 150; $image_obj->imageResize($target_image, $resized_image, $max_w, $max_h, $image_ext); //header("Location: add_photo.php"); } ?> Setup: A preview button chooses the image, a preview which is displayed underneath it (using js). There's also an upload button to bring it into the sites file system. This functionality works fine. I've just added the resize button beside the preview button and connected the object method to it. Intended Result: The resize button resizes the previewed image and the resized image is now previewed instead of the original image. Result: Nothing. The button stays in the active state, but nothing happens. I get no warning nor error messages and absolutely nothing in dev tools to work from. Not sure what my next step would be outside asking more experienced coders Any advise or guidance on getting this resolved would be met with appreciation. TIA Quote Link to comment https://forums.phpfreaks.com/topic/313499-no-closure-on-image-resizing/ Share on other sites More sharing options...
Barand Posted August 5, 2021 Share Posted August 5, 2021 If you are uploading a file you should be using the $_FILES array, not $_POST for the image. Quote Link to comment https://forums.phpfreaks.com/topic/313499-no-closure-on-image-resizing/#findComment-1588867 Share on other sites More sharing options...
TechnoDiver Posted August 5, 2021 Author Share Posted August 5, 2021 I am using $_FILES for the upload. public function imageUpload() { $image_name = $_FILES['get_image']['name']; $image_temp_loc = $_FILES['get_image']['tmp_name']; $image_size = $_FILES['get_image']['size']; $image_type = $_FILES['get_image']['type']; $image_error = $_FILES['get_image']['error']; //error handling if(!$image_temp_loc) { echo "ERROR: You forgot to choose a file"; exit(); } else if($image_size > 5242880) { echo "ERROR: Your image is more than 5 Megabytes"; unlink($image_temp_loc); exit(); } else if(!preg_match("/\.(gif|jpeg|jpg|png)$/i", $image_name)) { echo "ERROR: This page is only for images"; unlink($image_temp_loc); exit(); } else if($image_error) { echo "ERROR: An error occurred. Something's fucked up."; exit(); } $move_image = move_uploaded_file($image_temp_loc, "/opt/lampp/htdocs/site/admin/img/$image_name"); if(!$move_image) { echo "ERROR: File not uploaded. Something's fucked up."; unlink($image_temp_loc); exit(); } Uploading works fine. It's the resize button that isn't working. The only time I've used $_POST here is to determine the states of the buttons on the actual page. Unless I'm misunderstanding what your saying.. Quote Link to comment https://forums.phpfreaks.com/topic/313499-no-closure-on-image-resizing/#findComment-1588874 Share on other sites More sharing options...
Barand Posted August 5, 2021 Share Posted August 5, 2021 2 hours ago, TechnoDiver said: $image_ext = $kaboom[-1]; Have you checked the value in $image_ext? Quote Link to comment https://forums.phpfreaks.com/topic/313499-no-closure-on-image-resizing/#findComment-1588877 Share on other sites More sharing options...
TechnoDiver Posted August 5, 2021 Author Share Posted August 5, 2021 (edited) 30 minutes ago, Barand said: Have you checked the value in $image_ext? I've attempted to and unless there's a way to check it outside browser input I can't right now as the button doesn't do anything. I choose the image, hit resize and the button changes color per css to active state but doesn't do anything, just sits there. I can't get any output from anything with it. and this is what happens after clicking resize - nothing except the changed button appearance What I also tried to do is to echo $ext (which is the outside $image_ext parameter) from inside the class method, but as I said nothing gets returned at all. Edited August 5, 2021 by TechnoDiver Quote Link to comment https://forums.phpfreaks.com/topic/313499-no-closure-on-image-resizing/#findComment-1588878 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.