蝙蝠岛资源网 Design By www.hbtsch.com
本文实例讲述了PHP实现将几张照片拼接到一起的合成图片功能。分享给大家供大家参考,具体如下:
<"\n");
die();
}
// 自然排序
usort($jpgs, "strnatcmp");
foreach($g_tasks as $page=>$photos){
$files = array();
foreach($photos as $r){
$files[] = $jpgs[$r];
}
$image_all = imagemake($files);
$filename = $page.'.jpg';
imagejpeg($image_all, $dst_path.'/'.$filename);
unset($files);
echo $filename.nl2br("\n");
}
echo 'ok'.nl2br("\n");
die();
/**
* 合成图片
* @param array $images 本页图片集合
* @return resource 合成后的图片
*/
function imagemake($files=array()){
global $g_width,$g_height,$g_models;
// 合成后的图片
$image_all = imageCreatetruecolor($g_width,$g_height);
// 为真彩色画布创建白色背景
$color = imagecolorallocate($image_all, 255, 255, 255);
imagefill($image_all, 0, 0, $color);
// imageColorTransparent($image_all, $color); // 背景透明
//function imagecopyresampled ($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)
// 排版合成
$type = count($files);
switch($type){
case 2:
break;
case 1:
case 3:
case 4:
// 用于合成的图片集
$images = array();
// 修正图片
for($i=0;$i<$type;$i++){
$images[] = imagecropper($files[$i],$g_models[$type][$i]['w'],$g_models[$type][$i]['h']);
}
// 排版合成
for($i=0;$i<$type;$i++){
imagecopyresampled($image_all,$images[$i],
$g_models[$type][$i]['x'],$g_models[$type][$i]['y'],0,0,
$g_models[$type][$i]['w'],$g_models[$type][$i]['h'],imagesx($images[$i]),imagesy($images[$i]));
}
break;
default:
break;
}
return $image_all;
}
/**
* 修剪图片:居中裁剪等比缩放
* @param $source_path 原图路径
* @param $target_width 目标宽度
* @param $target_height 目标高度
* @return bool|resource
*/
function imagecropper($source_path, $target_width, $target_height){
$source_info = getimagesize($source_path);
$source_width = $source_info[0];
$source_height = $source_info[1];
$source_mime = $source_info['mime'];
$source_ratio = $source_height / $source_width;
$target_ratio = $target_height / $target_width;
switch ($source_mime)
{
case 'image/gif':
$source_image = imagecreatefromgif($source_path);
break;
case 'image/jpeg':
$source_image = imagecreatefromjpeg($source_path);
break;
case 'image/png':
$source_image = imagecreatefrompng($source_path);
break;
default:
return false;
break;
}
// 横竖构图不同,旋转
if(($target_width-$target_height)*($source_width-$source_height)<0){
// 旋转
$source_image = imagerotate($source_image, 90, 0);
$source_width = $source_info[1]; // [0]
$source_height = $source_info[0]; // [1]
$source_ratio = $source_height / $source_width;
}
// 源图过高
if ($source_ratio > $target_ratio)
{
$cropped_width = $source_width;
$cropped_height = $source_width * $target_ratio;
$source_x = 0;
$source_y = ($source_height - $cropped_height) / 2;
}
// 源图过宽
elseif ($source_ratio < $target_ratio)
{
$cropped_width = $source_height / $target_ratio;
$cropped_height = $source_height;
$source_x = ($source_width - $cropped_width) / 2;
$source_y = 0;
}
// 源图适中
else
{
$cropped_width = $source_width;
$cropped_height = $source_height;
$source_x = 0;
$source_y = 0;
}
$target_image = imagecreatetruecolor($target_width, $target_height);
$cropped_image = imagecreatetruecolor($cropped_width, $cropped_height);
// 裁剪
imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height);
// 缩放
imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $cropped_width, $cropped_height);
return $target_image;
}
PS:该代码应用于命令行模式,且需要注意图片文件夹路径。
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP图形与图片操作技巧汇总》、《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》及《PHP数学运算技巧总结》
希望本文所述对大家PHP程序设计有所帮助。
标签:
PHP,合成,图片,照片
蝙蝠岛资源网 Design By www.hbtsch.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
蝙蝠岛资源网 Design By www.hbtsch.com
暂无PHP实现将几张照片拼接到一起的合成图片功能【便于整体打印输出】的评论...
更新日志
2025年10月29日
2025年10月29日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]