note 126654 added to function.imagejpeg

From: Date: Thu, 02 Dec 2021 11:28:32 +0000
Subject: note 126654 added to function.imagejpeg
Groups: php.notes 
Request: Send a blank email to php-notes+get-217318@lists.php.net to get a copy of this message
Here is a simple Storage Class which allows capturing the binary data of the image without using output buffering. Use it like this imagejpg($image, StorageStream::getStream('myImage')); $imageBinData = StorageStream::readUnsetStream('myImage'); /** * Class StorageStream captures streams to redirect without writing temp files and using output buffering * Author: storageStream@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); unset ($streamName); return $data; } } ---- 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.imagejpeg.php Edit -- https://main.php.net/note/edit/126654 Del: integrated -- https://main.php.net/note/delete/126654/integrated Del: useless -- https://main.php.net/note/delete/126654/useless Del: bad code -- https://main.php.net/note/delete/126654/bad+code Del: spam -- https://main.php.net/note/delete/126654/spam Del: non-english -- https://main.php.net/note/delete/126654/non-english Del: in docs -- https://main.php.net/note/delete/126654/in+docs Del: other reasons-- https://main.php.net/note/delete/126654 Reject -- https://main.php.net/note/reject/126654 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#217318) next »