2012-02-14 7 views
0

my64データベースのbase64_decodeからサムネイルを生成するためのコードを作成しようとしていますが、ソースファイルが純粋なJPGファイル形式であることを確認するのに問題があります。その後、私は解決することはできません:(mySQLのbase64_decodeからサムネイルを生成

ソースファイル(BASE64_ENCODE):ビュー/ヘルパーフォルダコードで http://pastebin.com/wFBcd79B

image.php:

<?php 
class ImageHelper extends Helper { 
    var $helpers = array('Html'); 
    var $cacheDir = 'imagecache'; 

    function getfile($i) { 

     preg_match_all("/data:(.*);base64,/", $i, $temp_imagetype); 

     $imagetype = $temp_imagetype[1][0]; 

     $image = base64_decode(preg_replace("/data.*base64,/","",$i)); 

     //echo $image; 

     ob_start(); 

     //header("Content-type: ".$imagetype); 
     header('Content-Type: image/jpeg'); 
     print($image); 

     $data = ob_get_clean(); 

     //file_put_contents($this->webroot.'tmp/temp.jpg', 'test'); 

     file_put_contents(ROOT.DS.APP_DIR.DS.WEBROOT_DIR.DS.'tmp/temp.jpg', $data); 


     //return ROOT.DS.APP_DIR.DS.WEBROOT_DIR.DS.'tmp/temp2.jpg'; 
     //return 'temp2.jpg'; 
     return 'temp.jpg'; 
    //} 
    } 


    //source from: http://bakery.cakephp.org/articles/hundleyj/2007/02/16/image-resize-helper and modified for base64_decode 
    function resize($path, $width, $height, $aspect = true, $htmlAttributes = array(), $return = false) { 
     $types = array(1 => "gif", "jpeg", "png", "swf", "psd", "wbmp"); // used to determine image type 

     //$fullpath = ROOT.DS.APP_DIR.DS.WEBROOT_DIR.DS.$this->themeWeb.IMAGES_URL; 

     $fullpath = ROOT.DS.APP_DIR.DS.WEBROOT_DIR.DS.IMAGES_URL; 

     $temppath = ROOT.DS.APP_DIR.DS.WEBROOT_DIR.DS.'tmp/'; 


     //$formImage = imagecreatefromstring(base64_decode(preg_replace("/data.*base64,/","",$path))); 

     //$url = $formImage; 

     //$path = $this->getfile($path); 

     $url = $temppath.$path; 

     //$url = preg_replace("/data.*base64,/","",$path); 

     //$url = base64_decode($url); 

     if (!($size = getimagesize($url))) 
      return; // image doesn't exist 

     if ($aspect) { // adjust to aspect. 
      if (($size[1]/$height) > ($size[0]/$width)) // $size[0]:width, [1]:height, [2]:type 
       $width = ceil(($size[0]/$size[1]) * $height); 
      else 
       $height = ceil($width/($size[0]/$size[1])); 
     } 

     $relfile = $this->cacheDir.'/'.$width.'x'.$height.'_'.basename($path); // relative file 
     $cachefile = $fullpath.$this->cacheDir.DS.$width.'x'.$height.'_'.basename($path); // location on server 

     if (file_exists($cachefile)) { 
     $csize = getimagesize($cachefile); 
     $cached = ($csize[0] == $width && $csize[1] == $height); // image is cached 
     if (@filemtime($cachefile) < @filemtime($url)) // check if up to date 
      $cached = false; 
     } else { 
      $cached = false; 
     } 

     if (!$cached) { 
      $resize = ($size[0] > $width || $size[1] > $height) || ($size[0] < $width || $size[1] < $height); 
     } else { 
      $resize = false; 
     } 

     if ($resize) { 
      $image = call_user_func('imagecreatefrom'.$types[$size[2]], $url); 
      if (function_exists("imagecreatetruecolor") && ($temp = imagecreatetruecolor ($width, $height))) { 
       imagecopyresampled ($temp, $image, 0, 0, 0, 0, $width, $height, $size[0], $size[1]); 
      } else { 
       $temp = imagecreate ($width, $height); 
       imagecopyresized ($temp, $image, 0, 0, 0, 0, $width, $height, $size[0], $size[1]); 
      } 
       call_user_func("image".$types[$size[2]], $temp, $cachefile); 
       imagedestroy ($image); 
       imagedestroy ($temp); 
     } 
     return $this->output(sprintf($this->Html->image($relfile,$htmlAttributes))); 
    } 
} 
?> 

ビューフォルダコードの 'index.phpを' に:

> Warning (2): imagecreatefromjpeg() [function.imagecreatefromjpeg]: 
> gd-jpeg, libjpeg: recoverable error: Premature end of JPEG file 
> [APP/View/Helper/image.php, line 86] 
> Warning (2): imagecreatefromjpeg() [function.imagecreatefromjpeg]: 
> '/Users/user/Sites/mycakeapp/app/webroot/tmp/temp.jpg' is not a valid 
> JPEG file [APP/View/Helper/image.php, line 86] 
> Warning (2): imagecopyresampled() expects parameter 2 to be resource, boolean given 
> [APP/View/Helper/image.php, line 88] 
> Warning (2): imagedestroy() expects parameter 1 to be resource, boolean given 
> [APP/View/Helper/image.php, line 94] 
01:

<?php 
    foreach ($products as $product): 
     echo $this->Image->resize($this->Image->getfile($product['Product']['image1']), 120, 120, false); 
    endforeach; 
    ?> 

出力はエラーログです

+0

あなたは、出力ソースに見たことがありますか?たぶん、JPGを破るPHPエラーメッセージがあります –

+0

yep出力ソース私はtemp.jpgを保存した後にそれを見ることができます...破損したファイルはありません... – user453089

+2

全体のアイデアはひどく間違っています。 base64でコード化されたデータをデータベースに格納するのはナンセンスです。データベースに画像を保存することは、大きなパフォーマンスのヒットです。 –

答えて

0

リンク先のbase64でエンコードされたデータは、正確に216(65536)バイトです。その数字はあまりにも丸すぎて偶然ではありません。

データベース列はTEXTと定義されていますか? MEDIUMTEXTまたはLONGTEXTのような大きなものに変換してください。

イメージをファイルシステムに格納する方が意味があります。データベースはファイルのような任意のデータを格納することができますが、実用的でない制限があります。ネットワーク上でデータベースサーバーからアプリケーションに大量のデータを転送する必要があるため、パフォーマンス上の問題があります。巨大なデータベースはバックアップに時間がかかります。

BLOBオブジェクトまたはTEXTオブジェクトの最大サイズは、そのタイプによって決まりますが、実際にクライアントとサーバーの間で送信できる最大値は、使用可能な量によって決まりますメモリおよび通信バッファのサイズに依存する。 max_allowed_packet variableの値を変更してメッセージバッファサイズを変更することはできますが、サーバーとクライアントプログラムの両方で行う必要があります。

http://dev.mysql.com/doc/refman/5.0/en/blob.html

+0

私はbase64でエンコードされたデータを使用しないことに決めました。通常のデータのようにデータベースに画像を保存する。私はLongBLOBにLongText型を変更しました... – user453089

関連する問題