我想让用户填写div的所有个人资料照片。我设法填写4:3的照片和16:9的照片,但我没有设法填补肖像。我的脚本从底部和顶部切割。我想要的只是从图片的底部切割,让图片从顶部开始。在宽度图片上,它从侧面切割并获得图像的中心。在肖像照片我想只从底部切割并填充div。我希望我的脚本像facebook profile pic一样工作。这是我的剧本:
<?
// Input parametres check
$w = intval($_GET['width']);
$h = intval($_GET['height']);
$mode = $_GET['mode']=='fit'?'fit':'fill';
if ($w <= 1 || $w >= 1000) $w = 535;
if ($h <= 1 || $h >= 1000) $h = 452;
// Source image
$src = imagecreatefromjpeg('123.jpg');
// Destination image with white background
$dst = imagecreatetruecolor($w, $h);
imagefill($dst, 0, 0, imagecolorallocate($dst, 255, 255, 255));
// All Magic is here
scale_image($src, $dst, $mode);
// Output to the browser
Header('Content-Type: image/jpeg');
imagejpeg($dst);
function scale_image($src_image, $dst_image, $op = 'fill') {
$src_width = imagesx($src_image);
$src_height = imagesy($src_image);
$dst_width = imagesx($dst_image);
$dst_height = imagesy($dst_image);
// Try to match destination image by width
$new_width = $dst_width;
$new_height = round($new_width*($src_height/$src_width));
$new_x = 0;
$new_y = round(($dst_height-$new_height)/2);
// FILL and FIT mode are mutually exclusive
if ($op =='fill')
$next = $new_height < $dst_height;
else
$next = $new_height > $dst_height;
// If match by width failed and destination image does not fit, try by height
if ($next) {
$new_height = $dst_height;
$new_width = round($new_height*($src_width/$src_height));
$new_x = round(($dst_width - $new_width)/2);
$new_y = 0;
}
// Copy image on right place
imagecopyresampled($dst_image, $src_image , $new_x, $new_y, 0, 0, $new_width, $new_height, $src_width, $src_height);
}