http://ja.wikipedia.org/wiki/GD_Graphics_Library
例えばユーザがアップロードした画像から、
指定された範囲をトリミングしたりできます。
トリミングは imagecrop() 関数を使用します。
/**
* 画像のトリミングを行なう
* トリミングしたファイルは以前のファイルに上書きされる
* @param {string} $file_path ファイルパス
* @param {integer} $x トリミング開始X座標
* @param {integer} $y トリミング開始Y座標
* @param {integer} $w トリミング範囲幅
* @param {integer} $h トリミング範囲高さ
* @returns {bool} 成功したらtrue, 失敗したらfalse
*/
function trim_image($file_path, $x, $y, $w, $h) {
// 拡張子を取得
preg_match('/\.(.*)$/', $file_path, $matches);
$extension = $matches[1];
// 拡張子に合わせてトリミング
switch($extension) {
case 'jpg':
case 'jpeg':
$image_resource = imagecreatefromjpeg($file_path);
$image_resource = imagecrop($image_resource, array(
'x' => $x,
'y' => $y,
'width' => $w,
'height' => $h,
));
unlink($file_path);
imagejpeg($image_resource, $file_path);
break;
case 'png':
$image_resource = imagecreatefrompng($file_path);
$image_resource = imagecrop($image_resource, array(
'x' => $x,
'y' => $y,
'width' => $w,
'height' => $h,
));
unlink($file_path);
imagepng($image_resource, $file_path);
break;
case 'gif':
$image_resource = imagecreatefromgif($file_path);
$image_resource = imagecrop($image_resource, array(
'x' => $x,
'y' => $y,
'width' => $w,
'height' => $h,
));
unlink($file_path);
imagegif($image_resource, $file_path);
break;
}
}
0 件のコメント:
コメントを投稿