note 126655 added to function.imagepng

From: Date: Thu, 02 Dec 2021 12:02:52 +0000
Subject: note 126655 added to function.imagepng
Groups: php.notes 
Request: Send a blank email to php-notes+get-217320@lists.php.net to get a copy of this message
Here is a simple class to capture binary image data without using output buffering. Usage: imagepng($image, StorageStream::getStream('myImage')); $imageBinData = StorageStream::readUnsetStream('myImage'); <?php /** * Class StorageStream captures streams to redirect without writing temp files and using output buffering * Author: storageStream(at)klausweise.de - 2021-12-02 */ class StorageStream { private $var; // varname in instance private static $isRegistered = false; // for on-the-fly registering private static $myStorage = []; // storage for caputured data public function stream_open(string $path) { $this->var =& StorageStream::$myStorage[parse_url($path)["host"]]; return true; } public function stream_write(string $data) { $this->var .= $data; return strlen($data); } public static function registerStreamWrapper() { if (!StorageStream::$isRegistered) { stream_wrapper_register("storageStream", StorageStream::class); StorageStream::$isRegistered = true; } } public static function getStream($streamName) { // returns a stream, e.g. $myStream = StorageStream::getStream('myStream1'); StorageStream::registerStreamWrapper(); return fopen("storageStream://" . $streamName, ""); } public static function readStream($streamName) { // returns the stored Data, e.g. $data = StorageStream::readStream('myStream1'); return StorageStream::$myStorage[$streamName]; } public static function unsetStream($streamName) { // removes stored Data unset (StorageStream::$myStorage[$streamName]); } public static function readUnsetStream($streamName) { // reads and deletes the stored data from the cache $data = StorageStream::readStream($streamName); StorageStream::unsetStream($streamName); return $data; } } // example: // imagepng($image, StorageStream::getStream('myImage')); // $imageBinData = StorageStream::readUnsetStream('myImage'); ?> ---- Server IP: 45.112.84.4 Probable Submitter: 45.112.85.242 (proxied: 2003:ea:a715:5000:d185:ce1f:c5ca:eb7) ---- Manual Page -- https://php.net/manual/en/function.imagepng.php Edit -- https://main.php.net/note/edit/126655 Del: integrated -- https://main.php.net/note/delete/126655/integrated Del: useless -- https://main.php.net/note/delete/126655/useless Del: bad code -- https://main.php.net/note/delete/126655/bad+code Del: spam -- https://main.php.net/note/delete/126655/spam Del: non-english -- https://main.php.net/note/delete/126655/non-english Del: in docs -- https://main.php.net/note/delete/126655/in+docs Del: other reasons-- https://main.php.net/note/delete/126655 Reject -- https://main.php.net/note/reject/126655 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#217320) next »