Home >Backend Development >PHP Tutorial >Does the picture belong to resource? If so, how to store the picture into redis/memcached memory?

Does the picture belong to resource? If so, how to store the picture into redis/memcached memory?

WBOY
WBOYOriginal
2016-07-06 13:52:171644browse

redis/memcached cannot store resources into cache, so are pictures considered resources?

Is there a way to store images in server memory, just like normal data.

(not the front-end expires cache, but stored in the server memory cache)

Reply content:

redis/memcached cannot store resources into cache, so are pictures considered resources?

Is there a way to store images in server memory, just like normal data.

(not the front-end expires cache, but stored in the server memory cache)

Read the image file content directly and put it directly into redis.
Redis is binary safe and can be stored with confidence.

<code><?php
/** @var Redis $redis */
/** @var string $filename */
// store.php
$img = file_get_contents($filename);
$redis->set('img', $img);


// show.php
$img = $redis->get('img');
header('Content-Type: image/png'); // you may change image/png to image/jpeg if you pic is jpeg.
header('Content-Length: ' . strlen($img));
echo $img;</code>

Convert the image to base64 and then store it in redis

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn