Question

using the example given at http://www.php.net/manual/en/function.imagecopyresized.php ... how to get image sizes afterward using getimagesize() function?

CODE:

    <?php

        if(isset($_FILES['images'])){
         //TEST1:
          $img = resize_this_image_now($_FILES['images']['tmp_name']);


         //TEST2:
        $img = resize_this_image_now($_FILES['images']['name']);/// This Drastically failed.

          $new_image = getimagesize($img);

        var_dump($new_image[0]);// I guessed this should have printed out the WIDTH_OF_THE_IMAGE... but, it prints some NON_READABLE stuffs (why?)

    }

// The PHP.NET CODE in a Function

    function resize_this_image_now($filename){
        // File and new size
      //  $filename = 'test.jpg';

        $percent = 0.5;

        // Content type
        header('Content-Type: image/jpeg');

        // Get new sizes
        list($width, $height) = getimagesize($filename);
        $newwidth = $width * $percent;
        $newheight = $height * $percent;

        // Load
        $thumb = imagecreatetruecolor($newwidth, $newheight);
        $source = imagecreatefromjpeg($filename);

        // Resize
        imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

        // Output
    return    imagejpeg($thumb);
    }
        ?>

All I want is to get the Size of the Image.... also, is it possible to do something like:

$_FILES['images']['tmp_name'] = $the_newly_resized_image_returned_from_the_PHP_dot_NET_code'; .... So that the ['images']['tmp_name'] will now have as source this new image??

Any suggestion is highly appreciated...

Was it helpful?

Solution

I decided to spend sometime examining you question. What I found out is that, I don't think you'd need to return the resized image through imagejpeg() the way you did. You might also need to add a imagedestroy(), after you call imagejpeg() in you function, to destroy the temporary memory used.

You need to first fully Upload the Image before resizing it. If you'd like, you could send the image in a temporary storage while you do whatever you want to it so that Php does not have to deal with it in a 'tmp_name' format...Then You can destroy the image later on.

Once the image is fully uploaded, things become easier. The codes might look something like:

        if(isset($_FILES['images'])){

        //may be some random numbers to accompany it.
        $rand = floor((mt_rand()+rand()+mt_rand())/3);

//Send it to the temporary folder you have had to create.
        if(move_uploaded_file(
                    $_FILES['images']['tmp_name'],'temporary_storage/image_'.$rand.'.jpg')){

        //Then run the `resize` function from here.
        $image_resized = resize_this_image_now('temporary_storage/image_'.$rand.'.jpg');

        //Now You can get the size if you wish.         
        list($width,$height) = getimagesize('temporary_storage/image_'.$rand.'.jpg');   

        // Out put

        echo "W:".$width."<br>H:".$height;

        //After you use it as desired, you can now destroy it using unlink or so.

unlink('temporary_storage/image_'.$rand.'.jpg');

            }else{
    echo "Upload Error goes here";
    }

        }

Note this answer is produced after several trials and errors... Please use this strategy wisely.

Hope it helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top