2016-11-02 13 views
-4

ベース64イメージをデータベースとサーバーフォルダに保存する方法を教えてください。次のコードはそのイメージを自分のサーバーフォルダに保存するために使用しています。画像を開くことはできません。ベース64イメージをデータベースとMy Serverフォルダに格納する方法

  $data = $src; 


     $data = str_replace('data:image/png;base64,', '', $data); 

      $data = str_replace(' ', '+', $data); 

     $data = base64_decode($data); 

     $file = '../emailtest'.rand() . '.png'; 

     $success = file_put_contents($file, $data); 

     $data = base64_decode($data); 

     $source_img = imagecreatefromstring($data); 

    $rotated_img = imagerotate($source_img, 90, 0); 

    $file = '../emailtest'. rand(). '.png'; 

    $imageSave = imagejpeg($rotated_img, $file, 10); 

     imagedestroy($source_img); 
+0

このコードを試してみてください。 – Krishnan

答えて

-1

私のアドバイス:可能な場合は、ファイルに保存し、データベースにバイナリを保存する 1.避けてください。 2. base64をテキスト列に保存するよりも、blob列にバイナリを保存する方が望ましいbase64でエンコードされ、私は 5.ディスプレイをbase64でする 4.コンバート画像のバイナリをDBへのバイナリファイルを保存し 3にバイナリ保存したURL 2から 1.ダウンロードした画像にしてみてください。ここ

これは、より大きくするため、 HTML <を使用して画像をbase64 * img>タグ

私は私のサーバーのフォルダにBase64でイメージを保存して、自分のデータベースへのパスを保存する必要がしたい DEMO

<?php 
$img_url = 'http://cdn-o7.outfit7.com/wp-content/uploads/2016/01/Icon-r-512-3.png'; 
$img_binary = file_get_contents($img_url); 
$ext = pathinfo($img_url, PATHINFO_EXTENSION); 
$img_base64 = "data:image/".$ext.";base64," . base64_encode($img_binary); 

//save into file 
$name = "emailtest_".rand().$ext; 
file_put_contents($name, $img_binary); 
echo "<img src=\"$name\" title=\"show from file\"/>"; 

//store to db 
/* create your new table for testing 
CREATE TABLE IF NOT EXISTS `table_img` (
    `id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, 
    `img_binary` blob NOT NULL, 
    `location` varchar(150) NOT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 
*/ 
$insert="INSERT INTO table_img ('img_binary','location') VALUES('$img_binary','$name')"; //==>save to db using this query 
$select="SELECT img_binary FROM table_img WHERE id=1"; //call your image binary using this query 
// then convert binary into base64 with this : $img_base64= base64_encode(YOUR BINARY DATA FROM DATABASE); 

//because i cant do it live in my server, i will shortcut call previous variable $img_base64 from above instead call binary from db and base64 encode 
echo "<img src=\"$img_base64\" title=\"show from base64\"/>"; 

?> 
+0

イメージをサーバーフォルダに保存する必要があります。 – Krishnan

+0

file_get_contents、file_put_contents。 ftp_putを使用することもできます。あなたは完全な答えをしたい場合は、PHPのftp – plonknimbuzz

+0

についての検索。あなたの質問を編集してください。私は簡単な質問を書いてくれるので簡単な答えであなたの質問に答えます。 – plonknimbuzz

関連する問題