勉強したことのメモ

Webエンジニア / プログラマが勉強したことのメモ。

PHPで画像を上下反転させ保存する

   2024/01/12  PHP

やりたかった事は、下記の通り。

・既にフォルダに保存されている画像を上下反転させる
・上下反転させた画像を上書き保存する

■ソース
<?
error_reporting(E_ALL & ~E_NOTICE);
$img_fir             = './img/';
$file_name         = 'test.jpg';
$img_url         = $img_fir . $file_name;

if ($_GET['mode'] == 'image_return') {
$source             = imagecreatefromjpeg($img_url);
$degrees         = 180;
$rotate             = imagerotate($source,$degrees, 0);
imagejpeg($rotate,$img_url);
header("Location:./index.php");
}
?>
<html>
<head>
<script type="text/javascript">
function image_return () {
location.href = './index.php?mode=image_return';
}
</script>
</head>
<body>
<img src="<?=$img_url . '?stamp=' . time();?>" width="200px" height="150px"><br>
<input type="button" onClick="return image_return();" value="上下反転">
</body>
</html>

■関数
imagecreatefromjpeg(画像名)
http://php.net/manual/ja/function.imagecreatefromjpeg.php
新しい画像をファイルあるいは URL から作成する

imagerotate(画像名、回転角度、回転後に重ねられない部分の色)
http://php.net/manual/ja/function.imagerotate.php
指定された角度で画像を回転する

imagejpeg(保存したい画像、保存するパス)
http://php.net/manual/ja/function.imagejpeg.php
画像をブラウザあるいはファイルに出力する

 - PHP

  関連記事

画像をアップロードすると複数サムネイルを生成する方法

フォームで画像をアップロードすると、予め定めておいた大中小のサイズでサムネイル画 ...

jQuery.uploadでリアルタイムプレビュー

やりたい事は、 ・<input type="file">で画像を選択 ...

formのinput="file"でディレクトリを選択させ、ディレクトリ内のファイルを全てアップロードする方法

フォームで複数のファイルをアップロードしたい場合、input="file"を複数 ...

CodeIgniter4でフォームからファイルをアップロードし保存する方法

CodeIgniter4.4.4でformから画像等のファイルをアップロードし、 ...

smart_resize_imageで縦横の比率関係なく、固定リサイズ

PHPで縦横どちらかが指定の長さを超えた場合、比率を保ったままリサイズで使ったs ...