Created
May 25, 2011 07:08
-
-
Save kijtra/990490 to your computer and use it in GitHub Desktop.
[PHP] 画像からシンメトリー画像を作成
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
【シンメトリー画像作成】 | |
$_GET['d']が | |
L・・・左側を切り取り右に反転 | |
R・・・右側を切り取り左に反転 | |
T・・・上側を切り取り下に反転 | |
B・・・下側を切り取り上に反転 | |
例)http://example.com/symmetrizer.php?d=L | |
※重くなるが「LB」や「LBTR」と重ねがけも可能。(3つ以上は意味がないけど…) | |
※なんらかの失敗があると1x1pxの画像を出力。 | |
*/ | |
$filename='sample.jpg'; | |
$filepath='/path/to/file/'.$filename; | |
$direction=!empty($_GET['d']) ? strtoupper($_GET['d']) : 'L'; | |
if(!preg_match('/^[LRTB]+$/',$direction)){ | |
$direction='L'; | |
} | |
$ext=substr($filename,strrpos($filename,'.')+1); | |
if($ext=='jpg' || $ext=='jpeg'){ | |
$src=imagecreatefromjpeg($filepath); | |
}else if($ext=='gif'){ | |
$src=imagecreatefromgif($filepath); | |
}else if($ext=='png'){ | |
$src=imagecreatefrompng($filepath); | |
}else{ | |
_exit($ext); | |
} | |
for($i=0;$i<strlen($direction);$i++){ | |
$src=symmetry($src,$direction{$i}); | |
} | |
if($ext=='jpg' || $ext=='jpeg'){ | |
header("Content-type: image/jpeg"); | |
imagejpeg($src); | |
}else if($ext=='gif'){ | |
header("Content-type: image/gif"); | |
imagegif($src); | |
}else if($ext=='png'){ | |
header("Content-type: image/png"); | |
imagepng($src); | |
}else{ | |
_exit($ext); | |
} | |
exit; | |
###### functions ###### | |
function symmetry($src,$direction='L'){ | |
if($direction!=='L' && $direction!=='R' && $direction!=='T' && $direction!=='B'){ | |
return false; | |
} | |
$srcX=imagesx($src); | |
$srcY=imagesy($src); | |
if($direction==='L' || $direction==='R'){ | |
$halfX=ceil($srcX/2); | |
$half=imagecreatetruecolor($halfX,$srcY); | |
if($direction==='L'){ | |
imagecopyresampled($half,$src,0,0,0,0,$srcX,$srcY,$srcX,$srcY); | |
$halfL=$half; | |
$halfR=flip_horizontal($half); | |
}else{ | |
imagecopyresampled($half,$src,0,0,$halfX,0,$srcX,$srcY,$srcX,$srcY); | |
$halfL=flip_horizontal($half); | |
$halfR=$half; | |
} | |
if(empty($halfL) || empty($halfR)){ | |
_exit(); | |
} | |
$temp=imagecreatetruecolor($srcX,$srcY); | |
imagecopyresampled($temp,$halfL,0,0,0,0,$srcX,$srcY,$srcX,$srcY); | |
imagecopyresampled($temp,$halfR,$halfX,0,0,0,$srcX,$srcY,$srcX,$srcY); | |
return $temp; | |
}else{ | |
$halfY=ceil($srcY/2); | |
$half=imagecreatetruecolor($srcX,$halfY); | |
if($direction==='T'){ | |
imagecopyresampled($half,$src,0,0,0,0,$srcX,$srcY,$srcX,$srcY); | |
$halfT=$half; | |
$halfB=flip_vertical($half); | |
}else{ | |
imagecopyresampled($half,$src,0,0,0,$halfY,$srcX,$srcY,$srcX,$srcY); | |
$halfT=flip_vertical($half); | |
$halfB=$half; | |
} | |
if(empty($halfT) || empty($halfB)){ | |
_exit(); | |
} | |
$temp=imagecreatetruecolor($srcX,$srcY); | |
imagecopyresampled($temp,$halfT,0,0,0,0,$srcX,$srcY,$srcX,$srcY); | |
imagecopyresampled($temp,$halfB,0,$halfY,0,0,$srcX,$srcY,$srcX,$srcY); | |
return $temp; | |
} | |
} | |
function flip_vertical($imgsrc){ | |
$x=imagesx($imgsrc); | |
$y=imagesy($imgsrc); | |
$flip=imagecreatetruecolor($x,$y); | |
if(imagecopyresampled($flip,$imgsrc,0,0,0,$y-1,$x,$y,$x,0-$y)){ | |
return $flip; | |
}else{ | |
return $imgsrc; | |
} | |
} | |
function flip_horizontal($imgsrc){ | |
$x=imagesx($imgsrc); | |
$y=imagesy($imgsrc); | |
$flip=imagecreatetruecolor($x,$y); | |
if(imagecopyresampled($flip,$imgsrc,0,0,$x-1,0,$x,$y,0-$x,$y)){ | |
return $flip; | |
}else{ | |
return $imgsrc; | |
} | |
} | |
function _exit($ext='jpg'){ | |
if($ext=='png'){ | |
header("Content-type: image/png"); | |
$src=base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA3NCSVQICAjb4U/gAAAABlBMVEX///////9VfPVsAAAAAnRSTlMA/1uRIrUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAWdEVYdENyZWF0aW9uIFRpbWUAMDgvMzEvMTDLNYXzAAAAH3RFWHRTb2Z0d2FyZQBNYWNyb21lZGlhIEZpcmV3b3JrcyA4tWjSeAAAAApJREFUCJljYAAAAAIAAfRxZKYAAAAASUVORK5CYII='); | |
$src=imagecreatefromstring($src); | |
imagepng($src); | |
}else if($ext=='gif'){ | |
header("Content-type: image/gif"); | |
$src=base64_decode('R0lGODlhAQABAIAAAP///////yH5BAEHAAAALAAAAAABAAEAAAICRAEAOw=='); | |
$src=imagecreatefromstring($src); | |
imagegif($src); | |
}else{ | |
header("Content-type: image/jpeg"); | |
$src=base64_decode('/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAYEBAQFBAYFBQYJBgUGCQsIBgYICwwKCgsKCgwQDAwMDAwMEAwODxAPDgwTExQUExMcGxsbHCAgICAgICAgICD/2wBDAQcHBw0MDRgQEBgaFREVGiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICD/wAARCAABAAEDAREAAhEBAxEB/8QAFAABAAAAAAAAAAAAAAAAAAAACP/EABQQAQAAAAAAAAAAAAAAAAAAAAD/xAAUAQEAAAAAAAAAAAAAAAAAAAAA/8QAFBEBAAAAAAAAAAAAAAAAAAAAAP/aAAwDAQACEQMRAD8AVIP/2Q=='); | |
$src=imagecreatefromstring($src); | |
imagejpeg($src); | |
} | |
exit; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment