facebook twitter hatena line google mixi email

Php/fuelphp/サムネイル表示

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

管理ページにサムネイル生成&表示controllerを作る

class Controller_Admin_Thumbnail extends Controller_Admin
{
    public function action_response($width, $height, $filename)
    {
        if (!$filename) return;
        $width  = (int) $width;
        $height = (int) $height;
        $basepath = DOCROOT.'uploads/';
        foreach (array('jpg', 'png', 'jpeg') as $extvalue) {
            if (file_exists($basepath.$filename . '.' . $extvalue))
            {
                $filename .= '.' . $extvalue;
                break;
            }
        }
        $orig_filename = $basepath.$filename;
        $thumbpath = 'thumbnail/'.$width.'_'.$height.'_';
        if (file_exists($basepath.$thumbpath.$filename))
        {
            $img = File::read($basepath.$thumbpath.$filename);
            return Response::forge($img, 200, array('Content-Type' => 'image/png'));
        }
        else if(file_exists($orig_filename) && strpos(realpath(dirname($orig_filename)), realpath($basepath)) !== false && $width > 0 && $height > 0)
        {
//            if (!file_exists($basepath.$thumbpath))
//                File::create_dir($basepath, $thumbpath);
            \Image::config(array(
                'persistence' => true,
            ))->load($orig_filename)
                ->resize($width,$height)
                ->save($basepath.$thumbpath.$filename)
                ->output();
            $img = File::read($basepath.$thumbpath.$filename);
            return Response::forge($img, 200, array('Content-Type' => 'image/png'));
        }
        else
        {
            throw new HttpNotFoundException;
        }
    }
}

内部ロジックは以下URLから持ってきて少し変更

http://programmer-jobs.blogspot.jp/2012/10/fuelphp_10.html

httpアクセス

http://~/admin/thumbnail/response/100/100/ic_launcher

注意

メソッドをaction_indexにすると親のメソッドとかぶる恐れあり

Declaration of 
Controller_Admin_Thumbnail::action_index() should be compatible with