【问题标题】:PHP: how to use getimgasize() after the image is resized using getimageresized after upload?PHP:如何在上传后使用 getimageresized 调整图像大小后使用 getimgasize()?
【发布时间】:2013-12-17 03:00:07
【问题描述】:

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

我想要的只是是得到Image....的Size 还有,是否有可能做类似的事情:

$_FILES['images']['tmp_name'] = $the_newly_resized_image_returned_from_the_PHP_dot_NET_code'; .... 这样['images']['tmp_name'] 现在将拥有此新图像的源??

非常感谢任何建议...

【问题讨论】:

    标签: php file-upload size return image-resizing


    【解决方案1】:

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

    您需要先完全上传图片,然后再调整其大小。如果你愿意,你可以在做任何你想做的事情时将图像发送到临时存储中,这样 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";
        }
    
            }
    

    请注意,此答案是经过多次试验和错误后产生的...请明智地使用此策略。

    希望对你有帮助。

    【讨论】:

    • 工作,工作,工作......非常感谢。虽然我不明白list 的事情。我堆叠到数组中。喜欢:$new_image = getimagesize('temporary_storage/image_'.$rand.'.jpg'); 然后我称它为:echo $new_image[0] or [1] or [n]。它正确地工作。谢谢:)
    • 酷...享受编程!
    猜你喜欢
    • 1970-01-01
    • 2013-02-09
    • 2013-03-17
    • 2019-05-03
    • 2011-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多