ホーム > ブログ > [PHP] class.upload.phpで簡単画像アップロード

[PHP] class.upload.phpで簡単画像アップロード

画像アップロードライブラリ「class.upload.php」の使用方法です。

以下は、オリジナル画像をアップロードして、S、M、Lの3サイズの画像を生成する例です。
require 'class.upload.php';

$file_name = 'hoge';
$upload_dir = '/path/to/upload/';

$handle = new Upload($_FILES['image_file']);
	
if ($handle->uploaded) {
	// Sサイズ画像
	$handle->allowed = array('image/*'); // 画像のみ許可
	$handle->image_convert = 'jpg'; // jpgに変換
	$handle->file_overwrite = true; // 上書き許可
	$handle->file_auto_rename = false; // 自動リネーム禁止
	$handle->file_src_name_body = $file_name . "_S"; // ファイル名
	$handle->image_resize = true; // リサイズ許可
	$handle->image_ratio = true; // 縦横比維持
	$handle->image_x = 100; // 横最大値
	$handle->image_y = 100; // 縦最大値
	$handle->image_ratio_no_zoom_in = true; // image_x、image_yより小さいサイズは拡大禁止	
	$handle->Process($upload_dir);
	
	// Mサイズ画像
	$handle->allowed = array('image/*'); // 画像のみ許可
	$handle->image_convert = 'jpg'; // jpgに変換
	$handle->file_overwrite = true; // 上書き許可
	$handle->file_auto_rename = false; // 自動リネーム禁止
	$handle->file_src_name_body = $file_name . "_M"; // ファイル名
	$handle->image_resize = true; // リサイズ許可
	$handle->image_ratio = true; // 縦横比維持
	$handle->image_x = 200; // 横最大値
	$handle->image_y = 200; // 縦最大値
	$handle->image_ratio_no_zoom_in = true; // image_x、image_yより小さいサイズは拡大禁止
	$handle->Process($upload_dir);
	
	// Lサイズ画像
	$handle->allowed = array('image/*'); // 画像のみ許可
	$handle->image_convert = 'jpg'; // jpgに変換
	$handle->file_overwrite = true; // 上書き許可
	$handle->file_auto_rename = false; // 自動リネーム禁止
	$handle->file_src_name_body = $file_name . "_L"; // ファイル名
	$handle->image_resize = true; // リサイズ許可
	$handle->image_ratio = true; // 縦横比維持
	$handle->image_x = 500; // 横最大値
	$handle->image_y = 500; // 縦最大値
	$handle->image_ratio_no_zoom_in = true; // image_x、image_yより小さいサイズは拡大禁止		
	$handle->Process($upload_dir);
	
	if (!$handle->processed) return $handle->error;

} else {
	 return $handle->error;
}

設定できるプロパティはたくさんあります。
詳しくは、class.upload.phpのサンプルページをご覧ください。
前の記事 «
次の記事 »