Example #1
0
 function cache_control()
 {
     $f_xml = new File($this->set_cache_filename('xml'));
     $f_img = new File($this->set_cache_filename('img'), true);
     $live = $expire = $this->expire * 3600;
     $live++;
     // AMAZON_NO_IMAGE - $this->items['Height'] = 91; $this->items['Width']  = 69;
     // キャッシュが存在している場合
     if ($f_xml->has() && $f_xml->isReadable()) {
         // 経過秒数
         $live = time() - $f_xml->time();
     }
     // 一度キャッシュを作成した場合、取得できない場合は継続利用されることになる
     if ($expire >= $live) {
         $xml = $f_xml->get(true);
         // read cache file.
         if (empty($xml)) {
             $this->items['image'] = '';
             $this->items['Height'] = 91;
             $this->items['Width'] = 69;
             return false;
         }
         $this->obj_xml = simplexml_load_string($xml);
         $this->asin = $this->obj_xml->Items->Item->ASIN;
         list($URL, $Height, $Width) = $this->get_image_size();
         if ($f_img->has()) {
             $this->items['image'] = $f_img->filename;
             $this->items['Height'] = $Height;
             $this->items['Width'] = $Width;
         } else {
             $this->items['image'] = '';
             $this->items['Height'] = 91;
             $this->items['Width'] = 69;
         }
         return true;
     }
     // 直接読む場合
     $url = $this->ecs_url();
     $xml = $this->fetch_xml($url);
     if (!empty($this->items['Error'])) {
         $this->items['image'] = '';
         $this->items['Height'] = 91;
         $this->items['Width'] = 69;
         return false;
     }
     // ページが読めた場合
     $f_xml->set($xml);
     // write xml file.
     list($URL, $Height, $Width) = $this->get_image_size();
     if (empty($URL)) {
         $this->items['image'] = '';
         $this->items['Height'] = 91;
         $this->items['Width'] = 69;
         return true;
     }
     $img = $this->fetch_img((string) $URL);
     if (!empty($img)) {
         $f_img->set($img);
         // Fileクラスの画像書き込みがちゃんと動かない・・・。
         //self::file_write($f_img->filename, $img); // write img file.
     }
     $this->items['image'] = $f_img->filename;
     $this->items['Height'] = $Height;
     $this->items['Width'] = $Width;
     return true;
 }
Example #2
0
 /**
  * ファイルのMIMEタイプを取得
  */
 public function getMime()
 {
     // finfo関数の出力を優先
     if (function_exists('finfo_file')) {
         $finfo = finfo_open(FILEINFO_MIME_TYPE);
         $mimetype = finfo_file($finfo, $this->basename);
         finfo_close($finfo);
         return $mimetype;
     }
     //return preg_replace("/ [^ ]*/", "", trim(shell_exec('file -bi '.escapeshellcmd($path))));
     $f = new File($this->basename);
     try {
         // @をつけると処理が重いのでtry-catch文を使う
         $size = getimagesize($f->getRealPath());
         // 画像の場合
         switch ($size[2]) {
             case IMAGETYPE_BMP:
                 return 'image/bmp';
             case IMAGETYPE_GIF:
                 return 'image/gif';
             case IMAGETYPE_ICO:
                 return 'image/vnd.microsoft.icon';
             case IMAGETYPE_IFF:
                 return 'image/iff';
             case IMAGETYPE_JB2:
                 return 'image/jbig2';
             case IMAGETYPE_JP2:
                 return 'image/jp2';
             case IMAGETYPE_JPC:
                 return 'image/jpc';
             case IMAGETYPE_JPEG:
                 return 'image/jpeg';
             case IMAGETYPE_JPX:
                 return 'image/jpx';
             case IMAGETYPE_PNG:
                 return 'image/png';
             case IMAGETYPE_PSD:
                 return 'image/psd';
             case IMAGETYPE_SWC:
             case IMAGETYPE_SWF:
                 return 'application/x-shockwave-flash';
             case IMAGETYPE_TIFF_II:
             case IMAGETYPE_TIFF_MM:
                 return 'image/tiff';
             case IMAGETYPE_WBMP:
                 return 'image/vnd.wap.wbmp';
             case IMAGETYPE_XBM:
                 return 'image/xbm';
         }
     } catch (Exception $e) {
         // 画像でない場合エラーが発生するので例外処理で投げる
     }
     // mime-type一覧表を取得
     $config = new Config(self::ATTACH_CONFIG_PAGE_MIME);
     $table = $config->read() ? $config->get('mime-type') : array();
     unset($config);
     // メモリ節約
     foreach ($table as $row) {
         $_type = trim($row[0]);
         $exts = preg_split('/\\s+|,/', trim($row[1]), -1, PREG_SPLIT_NO_EMPTY);
         foreach ($exts as $ext) {
             if (preg_match('/\\.' . $ext . '$/i', $this->filename)) {
                 return $_type;
             }
         }
     }
     return self::DEFAULT_MIME_TYPE;
 }