php圖片合成方法(多張圖片合成一張)
更新時間:2017年11月25日 10:08:52 作者:凱歌~
下面小編就為大家分享一篇php圖片合成方法(多張圖片合成一張),具有很好的參考價值。希望對大家有所幫助。一起跟隨小編過來看看吧
1.多張圖片合成一張比如:
圖片合成,可以顯示在瀏覽器上面同時保存到文件夾下面
實例如下所示:
<?php /** * 圖片合并 **/ $pic_list = array( 'img2.png', 'img2.png', 'logo.png', 'logo.png', 'logo.png', 'img2.png', 'img2.png', 'img2.png', 'img2.png' ); $pic_list = array_slice($pic_list, 0, 9); // 只操作前9個圖片 $bg_w = 150; // 背景圖片寬度 $bg_h = 150; // 背景圖片高度 $background = imagecreatetruecolor($bg_w,$bg_h); // 背景圖片 $color = imagecolorallocate($background, 202, 201, 201); // 為真彩色畫布創(chuàng)建白色背景,再設(shè)置為透明 imagefill($background, 0, 0, $color); imageColorTransparent($background, $color); $pic_count = count($pic_list); $lineArr = array(); // 需要換行的位置 $space_x = 3; $space_y = 3; $line_x = 0; switch($pic_count) { case 1: // 正中間 $start_x = intval($bg_w/4); // 開始位置X $start_y = intval($bg_h/4); // 開始位置Y $pic_w = intval($bg_w/2); // 寬度 $pic_h = intval($bg_h/2); // 高度 break; case 2: // 中間位置并排 $start_x = 2; $start_y = intval($bg_h/4) + 3; $pic_w = intval($bg_w/2) - 5; $pic_h = intval($bg_h/2) - 5; $space_x = 5; break; case 3: $start_x = 40; // 開始位置X $start_y = 5; // 開始位置Y $pic_w = intval($bg_w/2) - 5; // 寬度 $pic_h = intval($bg_h/2) - 5; // 高度 $lineArr = array(2); $line_x = 4; break; case 4: $start_x = 4; // 開始位置X $start_y = 5; // 開始位置Y $pic_w = intval($bg_w/2) - 5; // 寬度 $pic_h = intval($bg_h/2) - 5; // 高度 $lineArr = array(3); $line_x = 4; break; case 5: $start_x = 30; // 開始位置X $start_y = 30; // 開始位置Y $pic_w = intval($bg_w/3) - 5; // 寬度 $pic_h = intval($bg_h/3) - 5; // 高度 $lineArr = array(3); $line_x = 5; break; case 6: $start_x = 5; // 開始位置X $start_y = 30; // 開始位置Y $pic_w = intval($bg_w/3) - 5; // 寬度 $pic_h = intval($bg_h/3) - 5; // 高度 $lineArr = array(4); $line_x = 5; break; case 7: $start_x = 53; // 開始位置X $start_y = 5; // 開始位置Y $pic_w = intval($bg_w/3) - 5; // 寬度 $pic_h = intval($bg_h/3) - 5; // 高度 $lineArr = array(2,5); $line_x = 5; break; case 8: $start_x = 30; // 開始位置X $start_y = 5; // 開始位置Y $pic_w = intval($bg_w/3) - 5; // 寬度 $pic_h = intval($bg_h/3) - 5; // 高度 $lineArr = array(3,6); $line_x = 5; break; case 9: $start_x = 5; // 開始位置X $start_y = 5; // 開始位置Y $pic_w = intval($bg_w/3) - 5; // 寬度 $pic_h = intval($bg_h/3) - 5; // 高度 $lineArr = array(4,7); $line_x = 5; break; } foreach( $pic_list as $k=>$pic_path ) { $kk = $k + 1; if ( in_array($kk, $lineArr) ) { $start_x = $line_x; $start_y = $start_y + $pic_h + $space_y; } $pathInfo = pathinfo($pic_path); switch( strtolower($pathInfo['extension']) ) { case 'jpg': case 'jpeg': $imagecreatefromjpeg = 'imagecreatefromjpeg'; break; case 'png': $imagecreatefromjpeg = 'imagecreatefrompng'; break; case 'gif': default: $imagecreatefromjpeg = 'imagecreatefromstring'; $pic_path = file_get_contents($pic_path); break; } $resource = $imagecreatefromjpeg($pic_path); // $start_x,$start_y copy圖片在背景中的位置 // 0,0 被copy圖片的位置 // $pic_w,$pic_h copy后的高度和寬度 imagecopyresized($background,$resource,$start_x,$start_y,0,0,$pic_w,$pic_h,imagesx($resource),imagesy($resource)); // 最后兩個參數(shù)為原始圖片寬度和高度,倒數(shù)兩個參數(shù)為copy時的圖片寬度和高度 $start_x = $start_x + $pic_w + $space_x; } header("Content-type: image/jpg"); imagejpeg($background); imagegif($background, "./hero_gam.png"); ?>
以上這篇php圖片合成方法(多張圖片合成一張)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
php實現(xiàn)批量上傳數(shù)據(jù)到數(shù)據(jù)庫(.csv格式)的案例
下面小編就為大家?guī)硪黄猵hp實現(xiàn)批量上傳數(shù)據(jù)到數(shù)據(jù)庫(.csv格式)的案例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06ThinkPHP3.2.3框架郵件發(fā)送功能圖文實例詳解
這篇文章主要介紹了ThinkPHP3.2.3框架郵件發(fā)送功能,結(jié)合圖文與實例形式詳細分析了基于thinkPHP框架進行郵件發(fā)送的相關(guān)原理、配置及操作技巧,需要的朋友可以參考下2019-04-04PHP 中使用explode()函數(shù)切割字符串為數(shù)組的示例
explode()函數(shù)的作用:使用一個字符串分割另一個字符串,打散為數(shù)組。下面通過本文給大家介紹PHP 中使用explode()函數(shù)切割字符串為數(shù)組 ,需要的朋友可以參考下2017-05-05PHP 中 DOMDocument保存xml時中文出現(xiàn)亂碼問題的解決方案
這篇文章主要介紹了PHP 中 DOMDocument保存xml時中文出現(xiàn)亂碼問題的解決方案,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09yii2 開發(fā)api接口時優(yōu)雅的處理全局異常的方法
這篇文章主要介紹了yii2 開發(fā)api接口時優(yōu)雅的處理全局異常的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05