GDで透かしを追加

//ヘッダーでimg画像であることを宣言
header("Content-Type: image/png");
//画像の読み込み
$dest = imagecreatefrompng('sample.png');
$src = imagecreatefrompng('watermark.png');

//元画像の幅と高さを取得
$dest_w = imagesx($dest);
$dest_h = imagesy($dest);

//透かしの幅と高さを取得
$src_w = imagesx($src);
$src_h = imagesx($src);

//透かしを表示する座標(画像の真ん中)
$dest_x = $dest_w/2-$src_w/2;
$dest_y = $dest_h/2-$src_h/2;


//画像を合成
//(画像リンク,透かしリンク,x座標,y座標,透かしx座標,透かしy座標,画像の幅,高さ,アルファ)
imagecopymerge($dest,$src,$dest_x,$dest_y,0,0,$src_w,$src_h,4);

//書き出し
imagepng($dest);

参考にしたサイト

  • http://www.phppro.jp/phpmanual/php/function.imagecopymerge.html
  • http://symfoware.blog68.fc2.com/blog-entry-861.html
  • http://php.net/manual/ja/function.imagecopymerge.php

PHP7でデータベースコネクト

PHP7系以降のバージョンでは、mysql関数が使用出来ない。

//データベースに接続($host, $username, $password, $dbname)
$mysqli = new mysqli('localhost','root','','database');
if ($mysqli->connect_error) {
error_log($mysqli->connect_error);
exit;
}

//クライアントのデフォルト文字セットをUTF-8に変更
$mysqli->set_charset("utf8");

データの取得

$sql =('select id,name from hoge');
$result = $mysqli -> query($sql);
while ($row = $result -> fetch_assoc()){
//ここに処理を書く
echo $row['name'];
}

参考にしたページ

  • http://qiita.com/kentyama/items/f7759807445c3863ccaa
  • http://replication.hatenablog.com/entry/2014/07/01/015350