PHP GD: How to get imagedata as binary string?

后端 未结 3 1931
春和景丽
春和景丽 2020-12-05 14:02

I\'m using a solution for assembling image files to a zip and streaming it to browser/Flex application. (ZipStream by Paul Duncan, http://pablotron.org/software/zipstream-ph

相关标签:
3条回答
  • 2020-12-05 14:28

    The php://memory stream can be used when output-buffer juggling is unwanted. https://www.php.net/manual/en/wrappers.php.php

    $imagedata = imagecreatefrompng($imagefile);
    
    // processing
    
    $stream = fopen('php://memory','r+');
    imagepng($imagedata,$stream);
    rewind($stream);
    $stringdata = stream_get_contents($stream);
    
    // Compressing the string data
    $zdata = gzdeflate($stringdata );
    
    0 讨论(0)
  • 2020-12-05 14:32
    // ob_clean(); // optional
    ob_start();
    imagepng($imagedata);
    $image = ob_get_clean();
    
    0 讨论(0)
  • 2020-12-05 14:38

    One way is to tell GD to output the image, then use PHP buffering to capture it to a string:

    $imagedata = imagecreatefrompng($imagefile);
    ob_start();
    imagepng($imagedata);
    $stringdata = ob_get_contents(); // read from buffer
    ob_end_clean(); // delete buffer
    $zdata = gzdeflate($stringdata);
    
    0 讨论(0)
提交回复
热议问题