├── .DS_Store ├── .gitignore ├── .hanzicard.php.swp ├── LICENSE ├── README.md ├── fonts ├── .DS_Store ├── 儷黑 Pro.ttf ├── 华文细黑.ttf └── 华文黑体.ttf ├── img ├── ic_img_laba.png ├── ic_img_lizi.png ├── ic_img_shiyi.png ├── ic_img_tianzg.png ├── ic_img_top_tzg.png ├── ic_img_xuhao1.png ├── ic_img_xuhao2.png ├── ic_img_xuhao3.png ├── ic_img_xuhao4.png ├── ic_img_xuhao5.png ├── ic_img_xuhao6.png ├── ic_img_xuhao7.png ├── ic_img_xuhao8.png ├── ic_img_xuhao9.png ├── ic_img_xuhao_cheng.png ├── ic_img_xuhao_zhi.png ├── ic_qr.png └── test.png ├── mycard.php ├── php └── ImageCrop.php ├── res ├── .DS_Store ├── s.png ├── test4.png ├── test5.jpg ├── test5.png ├── test6.jpg ├── test6.png ├── 解1.jpg ├── 解1.png ├── 解2.jpg └── 解2.png ├── test.php ├── topcard.php ├── xml ├── .DS_Store └── resource.xml └── 解.png /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SaudM/PhpCard/9f7a3ac5552a656f8c5d94628c534007f2a000a3/.DS_Store -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ -------------------------------------------------------------------------------- /.hanzicard.php.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SaudM/PhpCard/9f7a3ac5552a656f8c5d94628c534007f2a000a3/.hanzicard.php.swp -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, and 10 | distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by the copyright 13 | owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all other entities 16 | that control, are controlled by, or are under common control with that entity. 17 | For the purposes of this definition, "control" means (i) the power, direct or 18 | indirect, to cause the direction or management of such entity, whether by 19 | contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the 20 | outstanding shares, or (iii) beneficial ownership of such entity. 21 | 22 | "You" (or "Your") shall mean an individual or Legal Entity exercising 23 | permissions granted by this License. 24 | 25 | "Source" form shall mean the preferred form for making modifications, including 26 | but not limited to software source code, documentation source, and configuration 27 | files. 28 | 29 | "Object" form shall mean any form resulting from mechanical transformation or 30 | translation of a Source form, including but not limited to compiled object code, 31 | generated documentation, and conversions to other media types. 32 | 33 | "Work" shall mean the work of authorship, whether in Source or Object form, made 34 | available under the License, as indicated by a copyright notice that is included 35 | in or attached to the work (an example is provided in the Appendix below). 36 | 37 | "Derivative Works" shall mean any work, whether in Source or Object form, that 38 | is based on (or derived from) the Work and for which the editorial revisions, 39 | annotations, elaborations, or other modifications represent, as a whole, an 40 | original work of authorship. For the purposes of this License, Derivative Works 41 | shall not include works that remain separable from, or merely link (or bind by 42 | name) to the interfaces of, the Work and Derivative Works thereof. 43 | 44 | "Contribution" shall mean any work of authorship, including the original version 45 | of the Work and any modifications or additions to that Work or Derivative Works 46 | thereof, that is intentionally submitted to Licensor for inclusion in the Work 47 | by the copyright owner or by an individual or Legal Entity authorized to submit 48 | on behalf of the copyright owner. For the purposes of this definition, 49 | "submitted" means any form of electronic, verbal, or written communication sent 50 | to the Licensor or its representatives, including but not limited to 51 | communication on electronic mailing lists, source code control systems, and 52 | issue tracking systems that are managed by, or on behalf of, the Licensor for 53 | the purpose of discussing and improving the Work, but excluding communication 54 | that is conspicuously marked or otherwise designated in writing by the copyright 55 | owner as "Not a Contribution." 56 | 57 | "Contributor" shall mean Licensor and any individual or Legal Entity on behalf 58 | of whom a Contribution has been received by Licensor and subsequently 59 | incorporated within the Work. 60 | 61 | 2. Grant of Copyright License. 62 | 63 | Subject to the terms and conditions of this License, each Contributor hereby 64 | grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, 65 | irrevocable copyright license to reproduce, prepare Derivative Works of, 66 | publicly display, publicly perform, sublicense, and distribute the Work and such 67 | Derivative Works in Source or Object form. 68 | 69 | 3. Grant of Patent License. 70 | 71 | Subject to the terms and conditions of this License, each Contributor hereby 72 | grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, 73 | irrevocable (except as stated in this section) patent license to make, have 74 | made, use, offer to sell, sell, import, and otherwise transfer the Work, where 75 | such license applies only to those patent claims licensable by such Contributor 76 | that are necessarily infringed by their Contribution(s) alone or by combination 77 | of their Contribution(s) with the Work to which such Contribution(s) was 78 | submitted. If You institute patent litigation against any entity (including a 79 | cross-claim or counterclaim in a lawsuit) alleging that the Work or a 80 | Contribution incorporated within the Work constitutes direct or contributory 81 | patent infringement, then any patent licenses granted to You under this License 82 | for that Work shall terminate as of the date such litigation is filed. 83 | 84 | 4. Redistribution. 85 | 86 | You may reproduce and distribute copies of the Work or Derivative Works thereof 87 | in any medium, with or without modifications, and in Source or Object form, 88 | provided that You meet the following conditions: 89 | 90 | You must give any other recipients of the Work or Derivative Works a copy of 91 | this License; and 92 | You must cause any modified files to carry prominent notices stating that You 93 | changed the files; and 94 | You must retain, in the Source form of any Derivative Works that You distribute, 95 | all copyright, patent, trademark, and attribution notices from the Source form 96 | of the Work, excluding those notices that do not pertain to any part of the 97 | Derivative Works; and 98 | If the Work includes a "NOTICE" text file as part of its distribution, then any 99 | Derivative Works that You distribute must include a readable copy of the 100 | attribution notices contained within such NOTICE file, excluding those notices 101 | that do not pertain to any part of the Derivative Works, in at least one of the 102 | following places: within a NOTICE text file distributed as part of the 103 | Derivative Works; within the Source form or documentation, if provided along 104 | with the Derivative Works; or, within a display generated by the Derivative 105 | Works, if and wherever such third-party notices normally appear. The contents of 106 | the NOTICE file are for informational purposes only and do not modify the 107 | License. You may add Your own attribution notices within Derivative Works that 108 | You distribute, alongside or as an addendum to the NOTICE text from the Work, 109 | provided that such additional attribution notices cannot be construed as 110 | modifying the License. 111 | You may add Your own copyright statement to Your modifications and may provide 112 | additional or different license terms and conditions for use, reproduction, or 113 | distribution of Your modifications, or for any such Derivative Works as a whole, 114 | provided Your use, reproduction, and distribution of the Work otherwise complies 115 | with the conditions stated in this License. 116 | 117 | 5. Submission of Contributions. 118 | 119 | Unless You explicitly state otherwise, any Contribution intentionally submitted 120 | for inclusion in the Work by You to the Licensor shall be under the terms and 121 | conditions of this License, without any additional terms or conditions. 122 | Notwithstanding the above, nothing herein shall supersede or modify the terms of 123 | any separate license agreement you may have executed with Licensor regarding 124 | such Contributions. 125 | 126 | 6. Trademarks. 127 | 128 | This License does not grant permission to use the trade names, trademarks, 129 | service marks, or product names of the Licensor, except as required for 130 | reasonable and customary use in describing the origin of the Work and 131 | reproducing the content of the NOTICE file. 132 | 133 | 7. Disclaimer of Warranty. 134 | 135 | Unless required by applicable law or agreed to in writing, Licensor provides the 136 | Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, 137 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, 138 | including, without limitation, any warranties or conditions of TITLE, 139 | NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are 140 | solely responsible for determining the appropriateness of using or 141 | redistributing the Work and assume any risks associated with Your exercise of 142 | permissions under this License. 143 | 144 | 8. Limitation of Liability. 145 | 146 | In no event and under no legal theory, whether in tort (including negligence), 147 | contract, or otherwise, unless required by applicable law (such as deliberate 148 | and grossly negligent acts) or agreed to in writing, shall any Contributor be 149 | liable to You for damages, including any direct, indirect, special, incidental, 150 | or consequential damages of any character arising as a result of this License or 151 | out of the use or inability to use the Work (including but not limited to 152 | damages for loss of goodwill, work stoppage, computer failure or malfunction, or 153 | any and all other commercial damages or losses), even if such Contributor has 154 | been advised of the possibility of such damages. 155 | 156 | 9. Accepting Warranty or Additional Liability. 157 | 158 | While redistributing the Work or Derivative Works thereof, You may choose to 159 | offer, and charge a fee for, acceptance of support, warranty, indemnity, or 160 | other liability obligations and/or rights consistent with this License. However, 161 | in accepting such obligations, You may act only on Your own behalf and on Your 162 | sole responsibility, not on behalf of any other Contributor, and only if You 163 | agree to indemnify, defend, and hold each Contributor harmless for any liability 164 | incurred by, or claims asserted against, such Contributor by reason of your 165 | accepting any such warranty or additional liability. 166 | 167 | END OF TERMS AND CONDITIONS 168 | 169 | APPENDIX: How to apply the Apache License to your work 170 | 171 | To apply the Apache License to your work, attach the following boilerplate 172 | notice, with the fields enclosed by brackets "{}" replaced with your own 173 | identifying information. (Don't include the brackets!) The text should be 174 | enclosed in the appropriate comment syntax for the file format. We also 175 | recommend that a file or class name and description of purpose be included on 176 | the same "printed page" as the copyright notice for easier identification within 177 | third-party archives. 178 | 179 | Copyright 2016 Saud 180 | 181 | Licensed under the Apache License, Version 2.0 (the "License"); 182 | you may not use this file except in compliance with the License. 183 | You may obtain a copy of the License at 184 | 185 | http://www.apache.org/licenses/LICENSE-2.0 186 | 187 | Unless required by applicable law or agreed to in writing, software 188 | distributed under the License is distributed on an "AS IS" BASIS, 189 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 190 | See the License for the specific language governing permissions and 191 | limitations under the License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### php的gd库生成卡片的程序 2 | 3 | > 一个是在php环境下,需要有gd库。 4 | 5 | > 进入项目目录下,在终端下执行命令 6 | 7 | php mycard.php 8 | 9 | 10 | > topcard 执行命令: 11 | 12 | php topcard.php 我 > 我.png 13 | 14 | 这样第一个“我”是传入的参数 第二个“我”是生成图片的文件名 15 | 16 | ![实例图](https://github.com/SaudM/PhpCard/blob/master/解.png) 17 | -------------------------------------------------------------------------------- /fonts/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SaudM/PhpCard/9f7a3ac5552a656f8c5d94628c534007f2a000a3/fonts/.DS_Store -------------------------------------------------------------------------------- /fonts/儷黑 Pro.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SaudM/PhpCard/9f7a3ac5552a656f8c5d94628c534007f2a000a3/fonts/儷黑 Pro.ttf -------------------------------------------------------------------------------- /fonts/华文细黑.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SaudM/PhpCard/9f7a3ac5552a656f8c5d94628c534007f2a000a3/fonts/华文细黑.ttf -------------------------------------------------------------------------------- /fonts/华文黑体.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SaudM/PhpCard/9f7a3ac5552a656f8c5d94628c534007f2a000a3/fonts/华文黑体.ttf -------------------------------------------------------------------------------- /img/ic_img_laba.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SaudM/PhpCard/9f7a3ac5552a656f8c5d94628c534007f2a000a3/img/ic_img_laba.png -------------------------------------------------------------------------------- /img/ic_img_lizi.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SaudM/PhpCard/9f7a3ac5552a656f8c5d94628c534007f2a000a3/img/ic_img_lizi.png -------------------------------------------------------------------------------- /img/ic_img_shiyi.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SaudM/PhpCard/9f7a3ac5552a656f8c5d94628c534007f2a000a3/img/ic_img_shiyi.png -------------------------------------------------------------------------------- /img/ic_img_tianzg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SaudM/PhpCard/9f7a3ac5552a656f8c5d94628c534007f2a000a3/img/ic_img_tianzg.png -------------------------------------------------------------------------------- /img/ic_img_top_tzg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SaudM/PhpCard/9f7a3ac5552a656f8c5d94628c534007f2a000a3/img/ic_img_top_tzg.png -------------------------------------------------------------------------------- /img/ic_img_xuhao1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SaudM/PhpCard/9f7a3ac5552a656f8c5d94628c534007f2a000a3/img/ic_img_xuhao1.png -------------------------------------------------------------------------------- /img/ic_img_xuhao2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SaudM/PhpCard/9f7a3ac5552a656f8c5d94628c534007f2a000a3/img/ic_img_xuhao2.png -------------------------------------------------------------------------------- /img/ic_img_xuhao3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SaudM/PhpCard/9f7a3ac5552a656f8c5d94628c534007f2a000a3/img/ic_img_xuhao3.png -------------------------------------------------------------------------------- /img/ic_img_xuhao4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SaudM/PhpCard/9f7a3ac5552a656f8c5d94628c534007f2a000a3/img/ic_img_xuhao4.png -------------------------------------------------------------------------------- /img/ic_img_xuhao5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SaudM/PhpCard/9f7a3ac5552a656f8c5d94628c534007f2a000a3/img/ic_img_xuhao5.png -------------------------------------------------------------------------------- /img/ic_img_xuhao6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SaudM/PhpCard/9f7a3ac5552a656f8c5d94628c534007f2a000a3/img/ic_img_xuhao6.png -------------------------------------------------------------------------------- /img/ic_img_xuhao7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SaudM/PhpCard/9f7a3ac5552a656f8c5d94628c534007f2a000a3/img/ic_img_xuhao7.png -------------------------------------------------------------------------------- /img/ic_img_xuhao8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SaudM/PhpCard/9f7a3ac5552a656f8c5d94628c534007f2a000a3/img/ic_img_xuhao8.png -------------------------------------------------------------------------------- /img/ic_img_xuhao9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SaudM/PhpCard/9f7a3ac5552a656f8c5d94628c534007f2a000a3/img/ic_img_xuhao9.png -------------------------------------------------------------------------------- /img/ic_img_xuhao_cheng.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SaudM/PhpCard/9f7a3ac5552a656f8c5d94628c534007f2a000a3/img/ic_img_xuhao_cheng.png -------------------------------------------------------------------------------- /img/ic_img_xuhao_zhi.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SaudM/PhpCard/9f7a3ac5552a656f8c5d94628c534007f2a000a3/img/ic_img_xuhao_zhi.png -------------------------------------------------------------------------------- /img/ic_qr.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SaudM/PhpCard/9f7a3ac5552a656f8c5d94628c534007f2a000a3/img/ic_qr.png -------------------------------------------------------------------------------- /img/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SaudM/PhpCard/9f7a3ac5552a656f8c5d94628c534007f2a000a3/img/test.png -------------------------------------------------------------------------------- /mycard.php: -------------------------------------------------------------------------------- 1 | load('xml/resource.xml'); 15 | } else { 16 | throw new Exception("File not found: xml/resource.xml"); 17 | } 18 | $cards = $doc->getElementsByTagName("card"); 19 | 20 | //解析haed标签 21 | $haed = $doc->getElementsByTagName("haed"); 22 | $str_haed = $haed->item(0)->nodeValue; 23 | $str_haed = shanchu($str_haed); 24 | 25 | //解析pinyin标签 26 | $pinyin = $doc->getElementsByTagName("pinyin"); 27 | $str_pinyin = $pinyin->item(0)->nodeValue; 28 | $str_pinyin = shanchu($str_pinyin); 29 | 30 | 31 | //画背景 32 | $im = imagecreatetruecolor(750, 7000); 33 | $bgcolor = imagecolorallocate($im, 255, 255, 255); 34 | imagefill($im, 0, 0, $bgcolor); 35 | 36 | //生成汉字的背景矩形 37 | $image_width = 694; 38 | $image_height = 368; 39 | //矩形上面加圆角 40 | $radius = 10; 41 | $dst_x = 28; 42 | $y = 40; 43 | $w = 750; 44 | 45 | imagebackgroundmycard($im, $dst_x, $y, $image_width, $image_height, $radius); 46 | $y += $image_height; 47 | //画田字格 48 | $src = 'img/ic_img_tianzg.png'; 49 | imageresource($im, $src, ($w - 240) / 2, 108, 240, 240); 50 | 51 | //田字格中的字 52 | imagetext($im, $w, 120, $str_haed); 53 | 54 | //喇叭和拼音 55 | $yin = '[ ' . $str_pinyin . ' ]'; 56 | $y += 20; 57 | $dst_y = imagetextcenter($im, $w, $y, $yin); 58 | $y += 60; 59 | $i = 0; 60 | 61 | foreach ($cards as $card) { 62 | 63 | $i++; 64 | 65 | $titles = $card->getElementsByTagName("title"); 66 | if ($titles->length > 0) { 67 | $title = $titles->item(0)->nodeValue; 68 | $title = shanchu($title); 69 | } 70 | $images = $card->getElementsByTagName("image"); 71 | if ($images->length > 0) { 72 | $image = $images->item(0)->nodeValue; 73 | $image = shanchu($image); 74 | } 75 | 76 | $lis = $card->getElementsByTagName("li"); 77 | if ($lis->length > 0) { 78 | $li = $lis->item(0)->nodeValue; 79 | $li = shanchu($li); 80 | } 81 | 82 | $books = $card->getElementsByTagName("book"); 83 | if ($books->length > 0) { 84 | $book = $books->item(0)->nodeValue; 85 | $book = shanchu($book); 86 | } 87 | 88 | $myshi = $card->getElementsByTagName("shi"); 89 | if ($myshi->length > 0) { 90 | $shii = $myshi->item(0)->nodeValue; 91 | $shii = shanchu($shii); 92 | } 93 | $cis = $card->getElementsByTagName("ci"); 94 | if ($cis->length > 0) { 95 | $ci = $cis->item(0)->nodeValue; 96 | $ci = shanchu($ci); 97 | } 98 | 99 | //测量卡片解析的内容占用的高度 100 | $card_h = 35; 101 | if ($titles->length > 0) { 102 | $title_size = titlesize($im, $title); 103 | $card_h += $title_size + 35; 104 | } else { 105 | $title_size = 0; 106 | } 107 | 108 | if ($images->length > 0) { 109 | $images_size = 288; 110 | $card_h += $images_size + 40; 111 | } else { 112 | $images_size = 0; 113 | } 114 | 115 | if ($lis->length > 0) { 116 | $li_size = lisize($im, $li); 117 | $card_h += $li_size; 118 | } else { 119 | $li_size = 0; 120 | } 121 | 122 | if ($books->length > 0) { 123 | $book_size = booksize($im, $book); 124 | $card_h += $book_size; 125 | } else { 126 | $book_size = 0; 127 | } 128 | if ($myshi->length > 0) { 129 | $shii_size = shiisize($im, $shii); 130 | $card_h += $shii_size; 131 | } else { 132 | $shii_size = 0; 133 | } 134 | if ($cis->length > 0) { 135 | $ci_size = cisize($im, $ci); 136 | $card_h += $ci_size; 137 | } else { 138 | $ci_size = 0; 139 | } 140 | $card_h += 40; 141 | 142 | 143 | //画图 144 | draw_card($im, $dst_x, $y, $image_width, $card_h, $radius); 145 | draw_num($im, $i, $y - 29); 146 | 147 | 148 | $temp_y = $y + 35; 149 | if ($titles->length > 0) { 150 | draw_title($im, $title, $temp_y); 151 | $temp_y += $title_size + 35; 152 | } 153 | 154 | if ($images->length > 0) { 155 | draw_images($im, $image, $temp_y, $radius); 156 | $temp_y += $images_size + 40; 157 | } 158 | 159 | if ($lis->length > 0) { 160 | draw_li($im, $temp_y, $li); 161 | $temp_y += $li_size; 162 | } 163 | 164 | if ($books->length > 0) { 165 | draw_book($im, $temp_y, $book); 166 | $temp_y += $book_size; 167 | } 168 | 169 | if ($myshi->length > 0) { 170 | draw_shii($im, $temp_y, $shii); 171 | $temp_y += $shii_size; 172 | } 173 | 174 | if ($cis->length > 0) { 175 | draw_ci($im, $temp_y, $ci); 176 | } 177 | 178 | $y += $card_h + 40; 179 | 180 | } 181 | 182 | //解析“知”标签 183 | $zhi = $doc->getElementsByTagName("zhi"); 184 | if ($zhi->length > 0) { 185 | $str_zhi = $zhi->item(0)->nodeValue; 186 | $str_zhi = shanchu($str_zhi); 187 | 188 | //测量字的高度: 189 | $size = zhi_cheng_size($im, $str_zhi, $y + 35, false); 190 | //绘制 191 | draw_card($im, $dst_x, $y, $image_width, $size + 90, $radius); 192 | $src_zhi = "img/ic_img_xuhao_zhi.png"; 193 | draw_zhi_cheng($im, $src_zhi, $y - 29); 194 | zhi_cheng_size($im, $str_zhi, $y + 35, true); 195 | 196 | $y = $y + $size + 90 + 40; 197 | 198 | 199 | } 200 | //解析“成”标签 201 | $cheng = $doc->getElementsByTagName("cheng"); 202 | if ($cheng->length > 0) { 203 | $str_cheng = $cheng->item(0)->nodeValue; 204 | $str_cheng = shanchu($str_cheng); 205 | 206 | //测量字的高度: 207 | $size = zhi_cheng_size($im, $str_cheng, $y + 35, false); 208 | //绘制 209 | draw_card($im, $dst_x, $y, $image_width, $size + 90, $radius); 210 | $src_cheng = "img/ic_img_xuhao_cheng.png"; 211 | draw_zhi_cheng($im, $src_cheng, $y - 29); 212 | zhi_cheng_size($im, $str_cheng, $y + 35, true); 213 | 214 | $y = $y + $size + 90 + 40; 215 | 216 | } 217 | //绘制二维码 218 | draw_QR($im, $y); 219 | 220 | $y = $y + 307 + 40; 221 | 222 | //生成图片 223 | imagepng($im, $str_haed . ".png"); 224 | imagedestroy($im); 225 | 226 | //图片剪裁适合的高度 227 | $new_image = imagecreatetruecolor(750, $y); 228 | $src = imagecreatefromstring(file_get_contents($str_haed . ".png")); 229 | imagecopyresampled($new_image, $src, 0, 0, 0, 0, 750, $y, 750, $y); 230 | header('Content-Type: image/png'); 231 | imagepng($new_image, $str_haed . ".png"); 232 | imagedestroy($src); 233 | imagedestroy($new_image); 234 | 235 | 236 | //*****************************************以下为函数方法区*************************************************** 237 | 238 | function draw_ci($im, $y, $ci) 239 | { 240 | $src = "img/ic_img_shiyi.png"; 241 | $img = imagecreatefrompng($src); 242 | imagecopy($im, $img, 100, $y, 0, 0, 54, 54); 243 | return drawmintext($im, $y, $ci); 244 | } 245 | 246 | function draw_shii($im, $y, $shii) 247 | { 248 | $src = "img/ic_img_shiyi.png"; 249 | $img = imagecreatefrompng($src); 250 | imagecopy($im, $img, 100, $y, 0, 0, 54, 54); 251 | return drawmintext($im, $y, $shii); 252 | } 253 | 254 | 255 | function draw_book($im, $y, $li) 256 | { 257 | $li = "—— " . $li; 258 | return drawmintext($im, $y, $li); 259 | 260 | } 261 | 262 | /** 263 | * 获取文件名 264 | * @param $url 265 | * @return mixed 266 | */ 267 | function retrieve(string $url): string { 268 | $path = parse_url($url); 269 | $str = explode('.', $path['path']); 270 | return $str[0]; 271 | } 272 | 273 | /** 274 | * 绘制图片 275 | * @param $im 276 | * @param $src 277 | * @param $y 278 | * @param $radius 279 | */ 280 | function draw_images($im, $src, $y, $radius) 281 | { 282 | 283 | //裁图 284 | $newsrc = "res/" . $src; 285 | $filename = retrieve($newsrc); 286 | $ic = new ImageCrop($newsrc, $filename); 287 | $ic->Crop(560, 288, 1); 288 | $ic->saveAlpha(); 289 | $ic->destroy(); 290 | 291 | $img = imagecreatefrompng($filename . ".png"); 292 | 293 | imagecopy($im, $img, 100, $y, 0, 0, 560, 288); 294 | //画圆角 295 | $lt_corner = get_lt_rounder_corner($radius, 0xef, 0xef, 0xe1);//圆角的背景色 296 | myradus($im, 100, $y, $lt_corner, $radius, 288, 560); 297 | 298 | } 299 | 300 | /** 301 | * 绘制二维码 302 | * @param $im 303 | * @param $src 304 | * @param $y 305 | * @param $radius 306 | */ 307 | function draw_QR($im, $y) 308 | { 309 | $src = "img/ic_qr.png"; 310 | $img = imagecreatefrompng($src); 311 | imagecopy($im, $img, 0, $y, 0, 0, 750, 307); 312 | 313 | 314 | } 315 | 316 | 317 | /** 318 | * 319 | * 画浅色的背景card 320 | * @param $im 321 | * @param $dst_x 322 | * @param $y 323 | * @param $image_width 324 | * @param $image_height 325 | * @param $radius 326 | */ 327 | function draw_card($im, int $dst_x, int $y, int $image_width, int $image_height, int $radius): void { 328 | imagebackgroundmycard($im, $dst_x, $y, $image_width, $image_height, $radius); 329 | } 330 | 331 | /** 332 | * 画左边的小标记 333 | * @param $im 334 | * @param $num 335 | * @param $y 336 | */ 337 | function draw_num($im, $num, $y) 338 | { 339 | 340 | $ic_num = "img/ic_img_xuhao" . $num . ".png"; 341 | imageresource($im, $ic_num, 5, $y, 72, 72); 342 | 343 | } 344 | 345 | /** 346 | * 画左边的小标记 347 | * @param $im 348 | * @param $num 349 | * @param $y 350 | */ 351 | function draw_zhi_cheng($im, $src, $y) 352 | { 353 | 354 | imageresource($im, $src, 5, $y, 72, 72); 355 | 356 | } 357 | 358 | /** 359 | * 测量语句的出处的大小 360 | * @param $im 361 | * @param $str 362 | * @return int 363 | */ 364 | function booksize($im, $str) 365 | { 366 | 367 | $fontsize = 27; 368 | $hang_size = 50; 369 | $temp = array("color" => array(55, 55, 55), "fontsize" => $fontsize, "width" => 496, "left" => 100, "top" => 0, "hang_size" => $hang_size); 370 | return draw_txt_to($im, $temp, $str, false); 371 | } 372 | 373 | /** 374 | * 测量“词”的大小 375 | * @param $im 376 | * @param $str 377 | * @return int 378 | */ 379 | function cisize($im, $str) 380 | { 381 | return lisize($im, $str); 382 | } 383 | 384 | /** 385 | * 测量"释"的大小 386 | * @param $im 387 | * @param $str 388 | * @return int 389 | */ 390 | function shiisize($im, $str) 391 | { 392 | return lisize($im, $str); 393 | } 394 | 395 | 396 | /** 397 | * 绘制“例”的图标和内容 398 | * @param $im 399 | * @param $y 400 | * @param $li 401 | * @return int 402 | */ 403 | function draw_li($im, $y, $li) 404 | { 405 | 406 | $src = "img/ic_img_lizi.png"; 407 | $img = @imagecreatefrompng($src); 408 | if (!$img) { 409 | throw new Exception("Failed to load image: " . $src); 410 | } 411 | imagecopy($im, $img, 100, $y, 0, 0, 54, 54); 412 | return drawmintext($im, $y, $li); 413 | 414 | } 415 | 416 | /** 417 | * 绘制“例”“释”等汉字 418 | * @param $im 419 | * @param $y 420 | * @param $li 421 | * @return int 422 | */ 423 | function drawmintext($im, $y, $li) 424 | { 425 | $fontsize = 23; 426 | $hang_size = 50; 427 | $temp = array("color" => array(77, 77, 77), "fontsize" => $fontsize, "width" => 496, "left" => 164, "top" => $y - 10, "hang_size" => $hang_size); 428 | return draw_txt_to($im, $temp, $li, true); 429 | } 430 | 431 | /** 432 | * 测量“例”的大小 433 | * @param $im 434 | * @param $str 435 | * @return int 436 | */ 437 | function lisize($im, $str) 438 | { 439 | $fontsize = 23; 440 | $hang_size = 50; 441 | $temp = array("color" => array(55, 55, 55), "fontsize" => $fontsize, "width" => 496, "left" => 100, "top" => 0, "hang_size" => $hang_size); 442 | $h = draw_txt_to($im, $temp, $str, false); 443 | return $h > 54 ? $h : 54; 444 | } 445 | 446 | /** 447 | * 测量“例”的大小 448 | * @param $im 449 | * @param $str 450 | * @return int 451 | */ 452 | function zhi_cheng_size($im, $str, $top, $isdraw) 453 | { 454 | $fontsize = 23; 455 | $hang_size = 50; 456 | $temp = array("color" => array(55, 55, 55), "fontsize" => $fontsize, "width" => 560, "left" => 100, "top" => $top, "hang_size" => $hang_size); 457 | return draw_txt_to($im, $temp, $str, $isdraw); 458 | 459 | } 460 | 461 | /** 462 | * 画title文字 463 | * @param $im 464 | * @param $title 465 | * @param $y 466 | * @return int 467 | */ 468 | function draw_title($im, $title, $y) 469 | { 470 | 471 | 472 | $fontsize = 26; 473 | $hang_size = 50; 474 | $temp = array("color" => array(11, 11, 11), "fontsize" => $fontsize, "width" => 560, "left" => 100, "top" => $y, "hang_size" => $hang_size); 475 | return draw_txt_to($im, $temp, $title, true); 476 | 477 | } 478 | 479 | /** 480 | * 测量“头”的大小 481 | * @param $im 482 | * @param $str 483 | * @return int 484 | */ 485 | function titlesize($im, $title) 486 | { 487 | 488 | $fontsize = 26; 489 | $hang_size = 50; 490 | $temp = array("color" => array(55, 55, 55), "fontsize" => $fontsize, "width" => 560, "left" => 100, "top" => 0, "hang_size" => $hang_size); 491 | return draw_txt_to($im, $temp, $title, false); 492 | } 493 | 494 | 495 | /** 496 | * 文字自动换行算法 497 | * @param $card 画板 498 | * @param $pos 数组,top距离画板顶端的距离,fontsize文字的大小,width宽度,left距离左边的距离,hang_size行高 499 | * @param $str 要写的字符串 500 | * @param $iswrite 是否输出,ture,花出文字,false只计算占用的高度 501 | * @return int 返回整个字符所占用的高度 502 | */ 503 | function draw_txt_to($card, $pos, $str, $iswrite) 504 | { 505 | 506 | $_str_h = $pos["top"]; 507 | $fontsize = $pos["fontsize"]; 508 | $width = $pos["width"]; 509 | $margin_lift = $pos["left"]; 510 | $hang_size = $pos["hang_size"]; 511 | $temp_string = ""; 512 | $font_file = "./Fonts/华文细黑.ttf"; 513 | $tp = 0; 514 | 515 | $font_color = imagecolorallocate($card, $pos["color"][0], $pos["color"][1], $pos["color"][2]); 516 | for ($i = 0; $i < mb_strlen($str); $i++) { 517 | 518 | $box = imagettfbbox($fontsize, 0, $font_file, $temp_string); 519 | $_string_length = $box[2] - $box[0]; 520 | $temptext = mb_substr($str, $i, 1); 521 | 522 | $temp = imagettfbbox($fontsize, 0, $font_file, $temptext); 523 | 524 | if ($_string_length + $temp[2] - $temp[0] < $width) {//长度不够,字数不够,需要 525 | 526 | //继续拼接字符串。 527 | 528 | $temp_string .= mb_substr($str, $i, 1); 529 | 530 | if ($i == mb_strlen($str) - 1) {//是不是最后半行。不满一行的情况 531 | $_str_h += $hang_size;//计算整个文字换行后的高度。 532 | $tp++;//行数 533 | if ($iswrite) {//是否需要写入,核心绘制函数 534 | imagettftext($card, $fontsize, 0, $margin_lift, $_str_h, $font_color, $font_file, $temp_string); 535 | } 536 | 537 | } 538 | } else {//一行的字数够了,长度够了。 539 | 540 | // 打印输出,对字符串零时字符串置null 541 | $texts = mb_substr($str, $i, 1);//零时行的开头第一个字。 542 | 543 | // 判断默认第一个字符是不是符号; 544 | $isfuhao = preg_match("/[\\pP]/u", $texts) ? true : false;//一行的开头这个字符,是不是标点符号 545 | if ($isfuhao) {//如果是标点符号,则添加在第一行的结尾 546 | $temp_string .= $texts; 547 | 548 | // 判断如果是连续两个字符出现,并且两个丢失必须放在句末尾的,单独处理 549 | $f = mb_substr($str, $i + 1, 1); 550 | $fh = preg_match("/[\\pP]/u", $f) ? true : false; 551 | if ($fh) { 552 | $temp_string .= $f; 553 | $i++; 554 | } 555 | 556 | } else { 557 | $i--; 558 | } 559 | 560 | $tmp_str_len = mb_strlen($temp_string); 561 | $s = mb_substr($temp_string, $tmp_str_len - 1, 1);//取零时字符串最后一位字符 562 | 563 | if (is_firstfuhao($s)) {//判断零时字符串的最后一个字符是不是可以放在见面 564 | //讲最后一个字符用“_”代替。指针前移动一位。重新取被替换的字符。 565 | $temp_string = rtrim($temp_string, $s); 566 | $i--; 567 | } 568 | // } 569 | 570 | 571 | // 计算行高,和行数。 572 | $_str_h += $hang_size; 573 | $tp++; 574 | if ($iswrite) { 575 | 576 | imagettftext($card, $fontsize, 0, $margin_lift, $_str_h, $font_color, $font_file, $temp_string); 577 | } 578 | // 写完了改行,置null该行的临时字符串。 579 | $temp_string = ""; 580 | } 581 | } 582 | 583 | return $tp * $hang_size; 584 | 585 | } 586 | 587 | 588 | function is_firstfuhao($str) 589 | { 590 | $fuhaos = array("\"", "“", "'", "<", "《",); 591 | 592 | return in_array($str, $fuhaos); 593 | 594 | } 595 | 596 | /** 597 | * 喇叭和拼音 598 | * @param $im 总图 599 | * @param $w 总图宽 600 | * @param $str 拼音 601 | */ 602 | function imagetextcenter($im, $w, $y, $str) 603 | { 604 | //画小喇叭和拼音 605 | $font = "./Fonts/华文黑体.ttf"; 606 | $imgtext = imgtextsize(24, $font, $str); 607 | $fontcolor = imagecolorallocate($im, 33, 33, 33); 608 | $laba = imagecreatefrompng('./img/ic_img_laba.png'); 609 | imagecopy($im, $laba, ($w - $imgtext["width"]) / 2, $y, 0, 0, 42, 42); 610 | imagettftext($im, 24, 0, ($w - $imgtext["width"]) / 2 + 42 + 10, 424 + 4 + $imgtext["height"], $fontcolor, $font, $str); 611 | 612 | return $imgtext["height"]; 613 | } 614 | 615 | 616 | /** 617 | * 测量图片和字一起的大小 618 | * @param $imgsize 619 | * @param $font 620 | * @param $str 621 | * @return array [width,height] 622 | */ 623 | function imgtextsize($imgsize, $font, $str) 624 | { 625 | 626 | $bbox = imagettfbbox($imgsize, 0, $font, $str); 627 | $yin_w = $bbox[2] - $bbox[0]; 628 | $yin_h = $bbox[1] - $bbox[5]; 629 | $all_w = 42 + 20 + $yin_w;//算总宽 630 | return array("width" => $all_w, "height" => $yin_h); 631 | 632 | } 633 | 634 | 635 | /**除去空格回车 636 | * @param $str 637 | * @return mixed|string 638 | */ 639 | function shanchu($str) 640 | { 641 | $str = trim($str); 642 | $str = preg_replace('/\n/', '', $str); 643 | $str = preg_replace('/\r/', '', $str); 644 | return $str; 645 | } 646 | 647 | /** 648 | * 写一个居中的字 649 | * @param $im 字相对于最大的一个图 650 | * @param $w 最大图的大小 651 | * @param $size 字体大小 652 | * @param $font 字体样式 653 | * @param $str 这些字 654 | */ 655 | function imagetext($im, $w, $size, $str) 656 | { 657 | //往上写汉字 658 | $font = "./Fonts/华文细黑.ttf"; 659 | $fontcolor = imagecolorallocate($im, 33, 33, 33); 660 | $bbox = imagettfbbox($size, 0, $font, $str); 661 | $font_w = $bbox[2] - $bbox[0]; 662 | $font_h = $bbox[1] - $bbox[5]; 663 | imagettftext($im, $size, 0, ($w - $font_w) / 2 - 12, 108 + $font_h + (240 - $font_h) / 2 - $bbox[1], $fontcolor, $font, $str); 664 | } 665 | 666 | /** 667 | * 讲资源文件图片画到指定位置 668 | * @param $im 669 | * @param $src 670 | * @param $dst_x 671 | * @param $dst_y 672 | * @param $image_w 673 | * @param $image_h 674 | */ 675 | function imageresource($im, $src, $dst_x, $dst_y, $image_w, $image_h) 676 | { 677 | $tianzg = imagecreatefrompng($src); 678 | imagecopy($im, $tianzg, $dst_x, $dst_y, 0, 0, $image_w, $image_h); 679 | 680 | } 681 | 682 | /** 683 | * 画一个带圆角的背景图 684 | * @param $im 底图 685 | * @param $dst_x 画出的图的(0,0)位于底图的x轴位置 686 | * @param $dst_y 画出的图的(0,0)位于底图的y轴位置 687 | * @param $image_w 画的图的宽 688 | * @param $image_h 画的图的高 689 | * @param $radius 圆角的值 690 | */ 691 | function imagebackgroundmycard($im, $dst_x, $dst_y, $image_w, $image_h, $radius) 692 | { 693 | $resource = imagecreatetruecolor($image_w, $image_h); 694 | $bgcolor = imagecolorallocate($resource, 0xef, 0xef, 0xe1);//该图的背景色 695 | imagefill($resource, 0, 0, $bgcolor); 696 | $lt_corner = get_lt_rounder_corner($radius, 255, 255, 255);//圆角的背景色 697 | // lt(左上角) 698 | imagecopymerge($resource, $lt_corner, 0, 0, 0, 0, $radius, $radius, 100); 699 | // lb(左下角) 700 | $lb_corner = imagerotate($lt_corner, 90, 0); 701 | imagecopymerge($resource, $lb_corner, 0, $image_h - $radius, 0, 0, $radius, $radius, 100); 702 | // rb(右上角) 703 | $rb_corner = imagerotate($lt_corner, 180, 0); 704 | imagecopymerge($resource, $rb_corner, $image_w - $radius, $image_h - $radius, 0, 0, $radius, $radius, 100); 705 | // rt(右下角) 706 | $rt_corner = imagerotate($lt_corner, 270, 0); 707 | imagecopymerge($resource, $rt_corner, $image_w - $radius, 0, 0, 0, $radius, $radius, 100); 708 | 709 | imagecopy($im, $resource, $dst_x, $dst_y, 0, 0, $image_w, $image_h); 710 | } 711 | 712 | 713 | /** 画圆角 714 | * @param $radius 圆角位置 715 | * @param $color_r 716 | * @param $color_g 717 | * @param $color_b 718 | * @return resource 719 | */ 720 | function get_lt_rounder_corner($radius, $color_r, $color_g, $color_b) 721 | { 722 | // 创建一个正方形的图像 723 | $img = imagecreatetruecolor($radius, $radius); 724 | // 图像的背景 725 | $bgcolor = imagecolorallocate($img, $color_r, $color_g, $color_b); 726 | $fgcolor = imagecolorallocate($img, 0, 0, 0); 727 | imagefill($img, 0, 0, $bgcolor); 728 | // $radius,$radius:以图像的右下角开始画弧 729 | // $radius*2, $radius*2:已宽度、高度画弧 730 | // 180, 270:指定了角度的起始和结束点 731 | // fgcolor:指定颜色 732 | imagefilledarc($img, $radius, $radius, $radius * 2, $radius * 2, 180, 270, $fgcolor, IMG_ARC_PIE); 733 | // 将弧角图片的颜色设置为透明 734 | imagecolortransparent($img, $fgcolor); 735 | return $img; 736 | } 737 | 738 | /** 739 | * @param $im 740 | * @param $lt_corner 741 | * @param $radius 742 | * @param $image_h 743 | * @param $image_w 744 | */ 745 | function myradus($im, $lift, $top, $lt_corner, $radius, $image_h, $image_w) 746 | { 747 | /// lt(左上角) 748 | imagecopymerge($im, $lt_corner, $lift, $top, 0, 0, $radius, $radius, 100); 749 | // lb(左下角) 750 | $lb_corner = imagerotate($lt_corner, 90, 0); 751 | imagecopymerge($im, $lb_corner, $lift, $image_h - $radius + $top, 0, 0, $radius, $radius, 100); 752 | // rb(右上角) 753 | $rb_corner = imagerotate($lt_corner, 180, 0); 754 | imagecopymerge($im, $rb_corner, $image_w + $lift - $radius, $image_h + $top - $radius, 0, 0, $radius, $radius, 100); 755 | // rt(右下角) 756 | $rt_corner = imagerotate($lt_corner, 270, 0); 757 | imagecopymerge($im, $rt_corner, $image_w - $radius + $lift, $top, 0, 0, $radius, $radius, 100); 758 | } 759 | 760 | 761 | ?> -------------------------------------------------------------------------------- /php/ImageCrop.php: -------------------------------------------------------------------------------- 1 | Crop(120,80,2); 18 | * $ic->SaveImage(); 19 | * //$ic->SaveAlpha();将补白变成透明像素保存 20 | * $ic->destory(); 21 | * 22 | * 23 | */ 24 | class ImageCrop 25 | { 26 | 27 | private GdImage $sImage; 28 | private ?GdImage $dImage = null; 29 | private string $src_file; 30 | private string $dst_file; 31 | private int $src_width; 32 | private int $src_height; 33 | private int $src_type; 34 | private string $src_ext; 35 | 36 | public function __construct(string $src_file, string $dst_file = '') 37 | { 38 | $this->src_file = $src_file; 39 | $this->dst_file = $dst_file; 40 | $this->loadImage(); 41 | } 42 | 43 | /** 44 | * 设置源文件 45 | * 46 | * @param string $src_file 源文件路径 47 | */ 48 | public function setSrcFile(string $src_file): void 49 | { 50 | $this->src_file = $src_file; 51 | $this->loadImage(); 52 | } 53 | 54 | /** 55 | * 设置目标文件 56 | * 57 | * @param string $dst_file 目标文件路径 58 | */ 59 | public function setDstFile(string $dst_file): void 60 | { 61 | $this->dst_file = $dst_file; 62 | } 63 | 64 | 65 | /** 66 | * 加载图像并初始化相关属性 67 | * 68 | * @throws InvalidArgumentException 如果图像类型不支持 69 | * @throws RuntimeException 如果加载图像失败 70 | */ 71 | private function loadImage(): void 72 | { 73 | $imageInfo = getimagesize($this->src_file); 74 | if ($imageInfo === false) { 75 | throw new RuntimeException("无法获取图像尺寸:{$this->src_file}"); 76 | } 77 | 78 | [$this->src_width, $this->src_height, $this->src_type] = $imageInfo; 79 | 80 | $this->sImage = match ($this->src_type) { 81 | IMAGETYPE_JPEG => imagecreatefromjpeg($this->src_file), 82 | IMAGETYPE_PNG => imagecreatefrompng($this->src_file), 83 | IMAGETYPE_GIF => imagecreatefromgif($this->src_file), 84 | default => throw new InvalidArgumentException("不支持的图像类型:{$this->src_type}"), 85 | }; 86 | 87 | $this->src_ext = match ($this->src_type) { 88 | IMAGETYPE_JPEG => 'jpg', 89 | IMAGETYPE_PNG => 'png', 90 | IMAGETYPE_GIF => 'gif', 91 | }; 92 | } 93 | 94 | /** 95 | * 保存图像到指定文件 96 | * 97 | * @param string $fileName 目标文件名(可选) 98 | * @throws RuntimeException 如果保存图像失败 99 | */ 100 | public function saveImage(string $fileName = ''): void 101 | { 102 | $this->dst_file = $fileName ?: $this->dst_file; 103 | 104 | if (!$this->dImage) { 105 | throw new RuntimeException("目标图像未创建"); 106 | } 107 | 108 | match ($this->src_type) { 109 | IMAGETYPE_JPEG => imagejpeg($this->dImage, $this->dst_file, 100), 110 | IMAGETYPE_PNG => imagepng($this->dImage, $this->dst_file), 111 | IMAGETYPE_GIF => imagegif($this->dImage, $this->dst_file), 112 | default => throw new RuntimeException("无法保存图像:不支持的图像类型"), 113 | }; 114 | } 115 | 116 | /** 117 | * 输出图像到浏览器 118 | * 119 | * @throws RuntimeException 如果输出图像失败 120 | */ 121 | public function outImage(): void 122 | { 123 | if (!$this->dImage) { 124 | throw new RuntimeException("目标图像未创建"); 125 | } 126 | 127 | match ($this->src_type) { 128 | IMAGETYPE_JPEG => header('Content-Type: image/jpeg') && imagejpeg($this->dImage), 129 | IMAGETYPE_PNG => header('Content-Type: image/png') && imagepng($this->dImage), 130 | IMAGETYPE_GIF => header('Content-Type: image/gif') && imagegif($this->dImage), 131 | default => throw new RuntimeException("无法输出图像:不支持的图像类型"), 132 | }; 133 | } 134 | 135 | /** 136 | * 保存图像为带透明通道的 PNG 137 | * 138 | * @param string $fileName 目标文件名(可选) 139 | * @throws RuntimeException 如果保存图像失败 140 | */ 141 | public function saveAlpha(string $fileName = ''): void 142 | { 143 | $this->dst_file = $fileName ? "{$fileName}.png" : "{$this->dst_file}.png"; 144 | 145 | if (!$this->dImage) { 146 | throw new RuntimeException("目标图像未创建"); 147 | } 148 | 149 | imagesavealpha($this->dImage, true); 150 | imagepng($this->dImage, $this->dst_file); 151 | } 152 | 153 | /** 154 | * 输出图像为带透明通道的 PNG 155 | * 156 | * @throws RuntimeException 如果输出图像失败 157 | */ 158 | public function outAlpha(): void 159 | { 160 | if (!$this->dImage) { 161 | throw new RuntimeException("目标图像未创建"); 162 | } 163 | 164 | imagesavealpha($this->dImage, true); 165 | header('Content-Type: image/png'); 166 | imagepng($this->dImage); 167 | } 168 | 169 | /** 170 | * 销毁图像资源 171 | */ 172 | public function destroy(): void 173 | { 174 | if (isset($this->sImage) && is_resource($this->sImage)) { 175 | imagedestroy($this->sImage); 176 | } 177 | if (isset($this->dImage) && is_resource($this->dImage)) { 178 | imagedestroy($this->dImage); 179 | } 180 | } 181 | 182 | /** 183 | * 裁剪图像 184 | * 185 | * @param int $dst_width 目标宽度 186 | * @param int $dst_height 目标高度 187 | * @param int $mode 裁剪模式 188 | * @param string $dst_file 目标文件名(可选) 189 | * @throws InvalidArgumentException 如果裁剪模式无效 190 | */ 191 | public function crop(int $dst_width, int $dst_height, int $mode = 1, string $dst_file = ''): void 192 | { 193 | if ($dst_file) { 194 | $this->dst_file = $dst_file; 195 | } 196 | 197 | $this->dImage = imagecreatetruecolor($dst_width, $dst_height); 198 | if (!$this->dImage) { 199 | throw new RuntimeException("无法创建目标图像"); 200 | } 201 | 202 | $bg = imagecolorallocatealpha($this->dImage, 255, 255, 255, 127); 203 | imagefill($this->dImage, 0, 0, $bg); 204 | imagecolortransparent($this->dImage, $bg); 205 | 206 | $ratio_w = $dst_width / $this->src_width; 207 | $ratio_h = $dst_height / $this->src_height; 208 | $ratio = 1.0; 209 | 210 | switch ($mode) { 211 | case 1: // 强制裁剪 212 | if (($ratio_w < 1 && $ratio_h < 1) || ($ratio_w > 1 && $ratio_h > 1)) { 213 | $ratio = $ratio_w < $ratio_h ? $ratio_h : $ratio_w; 214 | $tmp_w = (int)($dst_width / $ratio); 215 | $tmp_h = (int)($dst_height / $ratio); 216 | $tmp_img = imagecreatetruecolor($tmp_w, $tmp_h); 217 | if (!$tmp_img) { 218 | throw new RuntimeException("无法创建临时图像"); 219 | } 220 | $src_x = (int)(($this->src_width - $tmp_w) / 2); 221 | $src_y = (int)(($this->src_height - $tmp_h) / 2); 222 | imagecopy($tmp_img, $this->sImage, 0, 0, $src_x, $src_y, $tmp_w, $tmp_h); 223 | imagecopyresampled($this->dImage, $tmp_img, 0, 0, 0, 0, $dst_width, $dst_height, $tmp_w, $tmp_h); 224 | imagedestroy($tmp_img); 225 | } else { 226 | $ratio = $ratio_w < $ratio_h ? $ratio_h : $ratio_w; 227 | $tmp_w = (int)($this->src_width * $ratio); 228 | $tmp_h = (int)($this->src_height * $ratio); 229 | $tmp_img = imagecreatetruecolor($tmp_w, $tmp_h); 230 | if (!$tmp_img) { 231 | throw new RuntimeException("无法创建临时图像"); 232 | } 233 | imagecopyresampled($tmp_img, $this->sImage, 0, 0, 0, 0, $tmp_w, $tmp_h, $this->src_width, $this->src_height); 234 | $src_x = (int)(($tmp_w - $dst_width) / 2); 235 | $src_y = (int)(($tmp_h - $dst_height) / 2); 236 | imagecopy($this->dImage, $tmp_img, 0, 0, $src_x, $src_y, $dst_width, $dst_height); 237 | imagedestroy($tmp_img); 238 | } 239 | break; 240 | 241 | case 2: // 仅在缩小时裁剪 242 | if ($ratio_w < 1 && $ratio_h < 1) { 243 | $ratio = $ratio_w < $ratio_h ? $ratio_h : $ratio_w; 244 | $tmp_w = (int)($dst_width / $ratio); 245 | $tmp_h = (int)($dst_height / $ratio); 246 | $tmp_img = imagecreatetruecolor($tmp_w, $tmp_h); 247 | if (!$tmp_img) { 248 | throw new RuntimeException("无法创建临时图像"); 249 | } 250 | $src_x = (int)(($this->src_width - $tmp_w) / 2); 251 | $src_y = (int)(($this->src_height - $tmp_h) / 2); 252 | imagecopy($tmp_img, $this->sImage, 0, 0, $src_x, $src_y, $tmp_w, $tmp_h); 253 | imagecopyresampled($this->dImage, $tmp_img, 0, 0, 0, 0, $dst_width, $dst_height, $tmp_w, $tmp_h); 254 | imagedestroy($tmp_img); 255 | } elseif ($ratio_w > 1 && $ratio_h > 1) { 256 | $dst_x = (int)(abs($dst_width - $this->src_width) / 2); 257 | $dst_y = (int)(abs($dst_height - $this->src_height) / 2); 258 | imagecopy($this->dImage, $this->sImage, $dst_x, $dst_y, 0, 0, $this->src_width, $this->src_height); 259 | } else { 260 | $src_x = 0; 261 | $dst_x = 0; 262 | $src_y = 0; 263 | $dst_y = 0; 264 | 265 | if (($dst_width - $this->src_width) < 0) { 266 | $src_x = (int)(($this->src_width - $dst_width) / 2); 267 | $dst_x = 0; 268 | } else { 269 | $src_x = 0; 270 | $dst_x = (int)(($dst_width - $this->src_width) / 2); 271 | } 272 | 273 | if (($dst_height - $this->src_height) < 0) { 274 | $src_y = (int)(($this->src_height - $dst_height) / 2); 275 | $dst_y = 0; 276 | } else { 277 | $src_y = 0; 278 | $dst_y = (int)(($dst_height - $this->src_height) / 2); 279 | } 280 | 281 | imagecopy($this->dImage, $this->sImage, $dst_x, $dst_y, $src_x, $src_y, $this->src_width, $this->src_height); 282 | } 283 | break; 284 | 285 | case 3: // 仅缩放,不裁剪,保留全部图像信息,会产生补白 286 | if ($ratio_w > 1 && $ratio_h > 1) { 287 | $dst_x = (int)(abs($dst_width - $this->src_width) / 2); 288 | $dst_y = (int)(abs($dst_height - $this->src_height) / 2); 289 | imagecopy($this->dImage, $this->sImage, $dst_x, $dst_y, 0, 0, $this->src_width, $this->src_height); 290 | } else { 291 | $ratio = $ratio_w > $ratio_h ? $ratio_h : $ratio_w; 292 | $tmp_w = (int)($this->src_width * $ratio); 293 | $tmp_h = (int)($this->src_height * $ratio); 294 | $tmp_img = imagecreatetruecolor($tmp_w, $tmp_h); 295 | if (!$tmp_img) { 296 | throw new RuntimeException("无法创建临时图像"); 297 | } 298 | imagecopyresampled($tmp_img, $this->sImage, 0, 0, 0, 0, $tmp_w, $tmp_h, $this->src_width, $this->src_height); 299 | $dst_x = (int)(abs($tmp_w - $dst_width) / 2); 300 | $dst_y = (int)(abs($tmp_h - $dst_height) / 2); 301 | imagecopy($this->dImage, $tmp_img, $dst_x, $dst_y, 0, 0, $tmp_w, $tmp_h); 302 | imagedestroy($tmp_img); 303 | } 304 | break; 305 | 306 | case 4: // 仅缩放,不裁剪,保留全部图像信息,生成图片大小为最终缩放后的图像实际大小,不产生补白 307 | if ($ratio_w > 1 && $ratio_h > 1) { 308 | $this->dImage = imagecreatetruecolor($this->src_width, $this->src_height); 309 | if (!$this->dImage) { 310 | throw new RuntimeException("无法创建目标图像"); 311 | } 312 | imagecopy($this->dImage, $this->sImage, 0, 0, 0, 0, $this->src_width, $this->src_height); 313 | } else { 314 | $ratio = $ratio_w > $ratio_h ? $ratio_h : $ratio_w; 315 | $tmp_w = (int)($this->src_width * $ratio); 316 | $tmp_h = (int)($this->src_height * $ratio); 317 | $this->dImage = imagecreatetruecolor($tmp_w, $tmp_h); 318 | if (!$this->dImage) { 319 | throw new RuntimeException("无法创建目标图像"); 320 | } 321 | imagecopyresampled($this->dImage, $this->sImage, 0, 0, 0, 0, $tmp_w, $tmp_h, $this->src_width, $this->src_height); 322 | } 323 | break; 324 | 325 | default: 326 | throw new InvalidArgumentException("无效的裁剪模式:{$mode}"); 327 | } 328 | } 329 | } 330 | ?> -------------------------------------------------------------------------------- /res/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SaudM/PhpCard/9f7a3ac5552a656f8c5d94628c534007f2a000a3/res/.DS_Store -------------------------------------------------------------------------------- /res/s.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SaudM/PhpCard/9f7a3ac5552a656f8c5d94628c534007f2a000a3/res/s.png -------------------------------------------------------------------------------- /res/test4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SaudM/PhpCard/9f7a3ac5552a656f8c5d94628c534007f2a000a3/res/test4.png -------------------------------------------------------------------------------- /res/test5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SaudM/PhpCard/9f7a3ac5552a656f8c5d94628c534007f2a000a3/res/test5.jpg -------------------------------------------------------------------------------- /res/test5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SaudM/PhpCard/9f7a3ac5552a656f8c5d94628c534007f2a000a3/res/test5.png -------------------------------------------------------------------------------- /res/test6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SaudM/PhpCard/9f7a3ac5552a656f8c5d94628c534007f2a000a3/res/test6.jpg -------------------------------------------------------------------------------- /res/test6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SaudM/PhpCard/9f7a3ac5552a656f8c5d94628c534007f2a000a3/res/test6.png -------------------------------------------------------------------------------- /res/解1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SaudM/PhpCard/9f7a3ac5552a656f8c5d94628c534007f2a000a3/res/解1.jpg -------------------------------------------------------------------------------- /res/解1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SaudM/PhpCard/9f7a3ac5552a656f8c5d94628c534007f2a000a3/res/解1.png -------------------------------------------------------------------------------- /res/解2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SaudM/PhpCard/9f7a3ac5552a656f8c5d94628c534007f2a000a3/res/解2.jpg -------------------------------------------------------------------------------- /res/解2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SaudM/PhpCard/9f7a3ac5552a656f8c5d94628c534007f2a000a3/res/解2.png -------------------------------------------------------------------------------- /test.php: -------------------------------------------------------------------------------- 1 | ")){ 5 | // 6 | // echo "存在"; 7 | //}else{ 8 | // echo "bu存在"; 9 | //}; 10 | // 11 | // 12 | // 13 | //function is_firstfuhao($str){ 14 | // $fuhaos= array("\"","“","'","<","《",); 15 | // 16 | // return in_array($str,$fuhaos); 17 | // 18 | //} 19 | 20 | 21 | 22 | 23 | //$temp_string="1234567890123456789!"; 24 | //$tmp_str_len=mb_strlen($temp_string); 25 | //$texts = mb_substr($temp_string, $tmp_str_len-1, 1); 26 | // 27 | //echo $texts; 28 | 29 | 30 | //$str = "香港加大内地?“水客”打击力度?您怎么看?"; 31 | //echo preg_replace($preg,$strPreg,$str); 32 | //echo strtr($str,"?","_"); 33 | //echo str_replace("?","_",$str); 34 | 35 | 36 | //echo str_replace_limit("?","$",$str,1); 37 | // 38 | //function str_replace_limit($search, $replace, $subject, $limit=-1) { 39 | //// constructing mask(s)... 40 | // if (is_array($search)) { 41 | // foreach ($search as $k=>$v) { 42 | // $search[$k] = '`' . preg_quote($search[$k],'`') . '`'; 43 | // } 44 | // } 45 | // else { 46 | // $search = '`' . preg_quote($search,'`') . '`'; 47 | // } 48 | //// replacement 49 | // return preg_replace($search, $replace, $subject, $limit); 50 | //} 51 | // 52 | //$rg[1]=mb_ereg_replace("?","$",iconv('gb2312', 'utf-8',$str)); 53 | 54 | 55 | 56 | 57 | $str = "香港加大内?地“水客”打击力度!您怎么看?"; 58 | 59 | //$preg = "/(.*)(“)";//请补充 60 | //$strPreg = "\\1";//请补充 61 | //echo preg_replace($preg,$strPreg,$str); 62 | 63 | $needle= '?'; 64 | echo strripos($str, $needle,0); 65 | echo str_replace($str,"$",strripos($str, $needle,0)); 66 | -------------------------------------------------------------------------------- /topcard.php: -------------------------------------------------------------------------------- 1 | #!/usr/bin/php 2 | $left, 122 | "top" => $top, 123 | "width" => $width, 124 | "height" => $height 125 | ]; 126 | } 127 | -------------------------------------------------------------------------------- /xml/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SaudM/PhpCard/9f7a3ac5552a656f8c5d94628c534007f2a000a3/xml/.DS_Store -------------------------------------------------------------------------------- /xml/resource.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 解 4 | 5 | 6 | jiě/jiè/xiè 7 | 8 | 9 | 10 | 11 | “解”的本义是“分解动物”。“解释、解说”道理时需要条分缕析;解说之后就会让人“理解”,在“理解”的基础上,加以思考,就会形成自己的“见解”。“分解动物”义后又引申为“解开”一切纠结之物,故有“解放、解脱”的意义,解开纠结之物后,纠结之物就会“松弛”下来,进一步引申为“懈怠”(心理上的松弛,后写作“懈”)。解开心中的结,就是“和解”。 12 | 13 | 14 | 15 | 16 | 17 | 解释,解答 18 | 19 |
  • 20 | 师者,所以传道受业解惑也。 21 |
  • 22 | 23 | 解1.jpg 24 | 25 | 26 | 《师说》 27 | 28 |
    29 | 30 | 31 | 32 | 剖开,分解动物的肢体 33 | 34 |
  • 35 | 庖丁为文惠君解牛。 36 |
  • 37 | 38 | 《庖丁解牛》 39 | 40 |
    41 | 42 | 43 | 44 | 系着的东西解开 45 | 46 |
  • 47 | 解甲投戈,遂作君臣之仪。 48 |
  • 49 | 50 | 《自嘲》 51 | 52 |
    53 | 54 | 55 | 56 | 解开,使分开 57 | 58 |
  • 59 | 少年大骇,急解令休止。 60 |
  • 61 | 62 | 《促织》 63 | 64 |
    65 | 66 | 67 | 68 | 理解,懂得 69 | 70 |
  • 71 | 其为惑也,终不解矣。 72 |
  • 73 | 74 | 《师说》 75 | 76 |
    77 | 78 | 79 | 80 | 解救、消除 81 | 82 |
  • 83 | 今有一言,可以解燕国之患,而报将军之仇者,何如? 84 |
  • 85 | 86 | 《荆轲刺秦王》 87 | 88 |
    89 | 90 | 91 | 92 | 分裂、涣散 93 | 94 |
  • 95 | 武王左操黄钺,右执白旄以麾之,则瓦解而走,遂土崩而下。 96 |
  • 97 | 98 | 解2.jpg 99 | 100 | 101 | 《淮南子》 102 | 103 |
    104 | 105 |
    106 | -------------------------------------------------------------------------------- /解.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SaudM/PhpCard/9f7a3ac5552a656f8c5d94628c534007f2a000a3/解.png --------------------------------------------------------------------------------