Skip to content

Instantly share code, notes, and snippets.

@kazup0n
Created June 22, 2017 15:50
Show Gist options
  • Save kazup0n/d7be546ea0e92f082631561310f517eb to your computer and use it in GitHub Desktop.
Save kazup0n/d7be546ea0e92f082631561310f517eb to your computer and use it in GitHub Desktop.
<!--
<実現したい事>
1. アップロードする画像ファイルを指定する
2. 表示したい文字を入力する
3. フォームを送信する
4. アップロードした画像のサムネイルと、入力した文字が表示される
<必要な処理>
1. 一時保存ファイル $_FILES[‘upfile][‘tmp_name’] を任意のフォルダに移動する.
2. 1のファイルからサムネイルを生成して保存する
3. 2のサムネイルと表示したい文字を表示するHTMLを返す
-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>sample</title>
</head>
<body>
<?php
if(is_uploaded_file($_FILES["upfile"]["tmp_name"])) {
//1. 一時保存ファイル $_FILES[‘upfile][‘tmp_name’] を任意のフォルダに移動する.
$copy_of_file = "files/" . $_FILES["upfile"]["name"];
if (move_uploaded_file($_FILES["upfile"]["tmp_name"], $copy_of_file)) {
chmod("files/" . $_FILES["upfile"]["name"], 0644);
// getimagesize関数 オリジナル画像の横幅・高さを取得
list($original_width, $original_height) = getimagesize($original_file);
//2. 1のファイルからサムネイルを生成して保存する
$original_image = imagecreatefromjpeg($copy_of_file);
$thumb_image = imagecreatetruecolor(300, 300); //サイズは 仮300x300
imagecopyresized($thumb_image, $original_image, 0,0, 0, 0, 300, 300, $original_width, $original_height);
imagejpeg($thumb_image, 'files/thumb.jpeg');
}
?>
<!-- //3. 2のサムネイルと表示したい文字を表示するHTMLを返す -->
<img src="files/thumb.jpeg" />
<p><?php echo $_POST['display_text']; ?></p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment