note 126654 deleted from function.imagejpeg by cmb
| From: | cmb@php.net | Date: | Thu, 02 Dec 2021 11:55:33 +0000 |
| Subject: | note 126654 deleted from function.imagejpeg by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-217319@lists.php.net to get a copy of this message | ||
Note Submitter: storagestream at klausweise dot de
----
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;
}
}