Jump to content

No closure on image resizing


TechnoDiver
 Share

Recommended Posts

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

 

 

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

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.

 

image_preview.png.e2acf294b45564c6d9e4c766dd575b6c.png

and this is what happens after clicking resize - nothing except the changed button appearance

after_button_press.png

 

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 by TechnoDiver
Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.