How to get image resource size in bytes with PHP and GD?

后端 未结 5 712
旧时难觅i
旧时难觅i 2020-12-10 06:42

I\'m resizing images with php gd. The result is image resources that i want to upload to Amazon S3. It works great if i store the images on disk first but i would like to up

5条回答
  •  猫巷女王i
    2020-12-10 07:25

    I can't write on php://memory with imagepng, so I use ob_start(), ob_get_content() end ob_end_clean()

    $image = imagecreatefrompng('./image.png'); //load image
    // do your processing here
    //...
    //...
    //...
    ob_start(); //Turn on output buffering
    imagejpeg($image); //Generate your image
    
    $output = ob_get_contents(); // get the image as a string in a variable
    
    ob_end_clean(); //Turn off output buffering and clean it
    echo strlen($output); //size in bytes
    

提交回复
热议问题