自分用の画像読み込みクラスを作ったので記念パピコ。
大したことはやってないです。
引数(ファイル名)の画像を探しに行って
redisに無ければ加工&再読み込みを行いバイナリをredisに保存。
2回目からはredisから取得する仕組み。
最初はfile_get_contentsの部分をreadfileと書いていたのですが、
redisにゴミデータしか入らないなーと思っていたら
readfileって読み込んでそのまま出力しちゃうんですね(*ノωノ) イヤン
ソースコードの一部が表示されてなかったので記事を修正しました。(15/11/08)
namespace Hoge;
use Fuel\Core\Redis_Db;
class Myimage
{
/**
* 指定したファイル名から加工した画像を取得する
*
* @param STRING $org_file_name
* @param STRING $ext
*/
public static function getImage( $org_file_name, $ext = '.jpg' )
{
$redis = Redis_Db::forge(REDIS_NAME);
// redis内の有無を確認
$redis_key = $org_file_name;
$image = $redis->get($redis_key);
if(!$image){
// 画像加工処理
$image_path = 'assets/img/';
$new_file = $org_file_name.'_300'. $ext;
$org_file = $image_path.$org_file_name . $ext;
$obj_image=\Image::load($org_file);
$obj_image->crop_resize(300,300);
$obj_image->border(5, '#000000');
$obj_image->save($image_path. $new_file);
// redisに保存
$img_bin = file_get_contents($image_path. $new_file);
$redis->set($redis_key, $img_bin);
$image = $img_bin;
}
return $image;
}
}

コメント