PHP:如何在上传后使用调整大小的getimmage调整图像大小后使用getimgasize()


PHP: how to use getimgasize() after the image is resized using getimageresized after upload?

使用http://www.php.net/manual/en/function.imagecopyresized.php。。。如何使用getimagesize()函数获取图像大小?

代码:

    <?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);
    }
        ?>

我只想得到ImageSize。。。。此外,有可能做一些类似的事情吗:

$_FILES['images']['tmp_name'] = $the_newly_resized_image_returned_from_the_PHP_dot_NET_code';。。。。因此,['images']['tmp_name']现在将具有此新图像作为源??

任何建议都将不胜感激。。。

我决定花点时间研究你的问题。我发现,我认为你不需要像以前那样通过imagejpeg()return调整大小的图像。在function中调用imagejpeg()之后,您可能还需要添加一个imagedestroy(),以销毁所使用的临时内存。

在调整图像大小之前,您需要先完全上传图像。如果您愿意,您可以在执行任何操作时将图像发送到临时存储中,这样Php就不必以'tmp_name'格式处理它。。。然后你可以稍后销毁图像。

一旦图像完全上传,事情就会变得更容易

        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";
    }
        }

请注意,这个答案是经过几次尝试和错误后得出的。。。请明智地使用这一策略。

希望能有所帮助。