PHP study notes 10GD image processing
Jul 09, 2016 am 09:10 AM<span style="color: #008080;"> 1</span> <span style="color: #000000;">php </span><span style="color: #008080;"> 2</span> <span style="color: #008000;">//</span><span style="color: #008000;">1. 圖片背景管理</span> <span style="color: #008080;"> 3</span> <span style="color: #008000;">/*</span><span style="color: #008000;"> imagecreatefromjpeg($filename) 從jpeg文件或者url新建一圖像 </span><span style="color: #008080;"> 4</span> <span style="color: #008000;"> * imagecreatefrompng($filename) 從png文件或者url新建一圖象 </span><span style="color: #008080;"> 5</span> <span style="color: #008000;"> * imagecreatefromgif($filename) 從gif文件或者url新建一圖像 </span><span style="color: #008080;"> 6</span> <span style="color: #008000;"> * 用完之后要用imagedestroy()函數(shù)進(jìn)行銷毀 </span><span style="color: #008080;"> 7</span> <span style="color: #008000;"> * getimagesize($filename) 返回?cái)?shù)組,0寬1高,2類型(1GIF,2JPG,3PNG,4SWF..),3是可用于img標(biāo)記的文本字符串(height="x" width="y") </span><span style="color: #008080;"> 8</span> <span style="color: #008000;">*/</span> <span style="color: #008080;"> 9</span> <span style="color: #008000;">//</span><span style="color: #008000;">該方法用于自動(dòng)識(shí)別類型并打開圖片,使用傳入的func函數(shù)操作圖片,vars是參數(shù)列表 </span><span style="color: #008080;"> 10</span> <span style="color: #008000;"> //如果返回的是不是原image,要在func中摧毀image</span> <span style="color: #008080;"> 11</span> <span style="color: #0000ff;">function</span> imagego(<span style="color: #800080;">$filename</span>, <span style="color: #800080;">$func</span>, <span style="color: #800080;">$vars</span><span style="color: #000000;">){ </span><span style="color: #008080;"> 12</span> <span style="color: #0000ff;">list</span>(<span style="color: #800080;">$width</span>, <span style="color: #800080;">$height</span>, <span style="color: #800080;">$type</span>, <span style="color: #800080;">$attr</span>) = <span style="color: #008080;">getimagesize</span>(<span style="color: #800080;">$filename</span><span style="color: #000000;">); </span><span style="color: #008080;"> 13</span> <span style="color: #800080;">$types</span> = <span style="color: #0000ff;">array</span>(1=>"gif", 2=>"jpeg", 3=>"png"<span style="color: #000000;">); </span><span style="color: #008080;"> 14</span> <span style="color: #008000;">//</span><span style="color: #008000;">組合創(chuàng)建圖像函數(shù)名</span> <span style="color: #008080;"> 15</span> <span style="color: #800080;">$createfrom</span> = "imagecreatefrom".<span style="color: #800080;">$types</span>{<span style="color: #800080;">$type</span><span style="color: #000000;">}; </span><span style="color: #008080;"> 16</span> <span style="color: #800080;">$image</span> = <span style="color: #800080;">$createfrom</span>(<span style="color: #800080;">$filename</span><span style="color: #000000;">); </span><span style="color: #008080;"> 17</span> <span style="color: #008000;">//</span><span style="color: #008000;">對(duì)該圖片進(jìn)行操作</span> <span style="color: #008080;"> 18</span> <span style="color: #800080;">$image</span> = <span style="color: #800080;">$func</span>(<span style="color: #800080;">$image</span>, <span style="color: #800080;">$width</span>, <span style="color: #800080;">$height</span>, <span style="color: #800080;">$type</span>, <span style="color: #800080;">$vars</span><span style="color: #000000;">); </span><span style="color: #008080;"> 19</span> <span style="color: #008000;">//</span><span style="color: #008000;">組合輸出圖像函數(shù)名</span> <span style="color: #008080;"> 20</span> <span style="color: #800080;">$output</span> = "image".<span style="color: #800080;">$types</span>{<span style="color: #800080;">$type</span><span style="color: #000000;">}; </span><span style="color: #008080;"> 21</span> <span style="color: #800080;">$filename_s</span> = <span style="color: #008080;">preg_split</span>("/\\./", <span style="color: #800080;">$filename</span><span style="color: #000000;">); </span><span style="color: #008080;"> 22</span> <span style="color: #800080;">$filename</span> = <span style="color: #800080;">$filename_s</span>{0}."_${func}.".<span style="color: #800080;">$filename_s</span>{1<span style="color: #000000;">}; </span><span style="color: #008080;"> 23</span> <span style="color: #800080;">$output</span>(<span style="color: #800080;">$image</span>, <span style="color: #800080;">$filename</span><span style="color: #000000;">); </span><span style="color: #008080;"> 24</span> <span style="color: #008080;"> 25</span> imagedestroy(<span style="color: #800080;">$image</span><span style="color: #000000;">); </span><span style="color: #008080;"> 26</span> <span style="color: #000000;"> } </span><span style="color: #008080;"> 27</span> <span style="color: #008000;">//</span><span style="color: #008000;">該方法用于在圖片上加上字符串,傳入一個(gè)參數(shù)表示添加的字符串</span> <span style="color: #008080;"> 28</span> <span style="color: #0000ff;">function</span> imageaddstring(<span style="color: #800080;">$image</span>, <span style="color: #800080;">$width</span>, <span style="color: #800080;">$height</span>, <span style="color: #800080;">$type</span>, <span style="color: #800080;">$vars</span><span style="color: #000000;">){ </span><span style="color: #008080;"> 29</span> <span style="color: #800080;">$string</span> = <span style="color: #800080;">$vars</span>{0<span style="color: #000000;">}; </span><span style="color: #008080;"> 30</span> <span style="color: #800080;">$sx</span> = (<span style="color: #800080;">$width</span> - imagefontwidth(5)*<span style="color: #008080;">strlen</span>(<span style="color: #800080;">$string</span>))/2<span style="color: #000000;">; </span><span style="color: #008080;"> 31</span> <span style="color: #800080;">$sy</span> = (<span style="color: #800080;">$height</span> - imagefontheight(5))/2<span style="color: #000000;">; </span><span style="color: #008080;"> 32</span> <span style="color: #800080;">$textcolor</span> = imagecolorallocate(<span style="color: #800080;">$image</span>, 255, 0, 0<span style="color: #000000;">); </span><span style="color: #008080;"> 33</span> imagestring(<span style="color: #800080;">$image</span>, 5, <span style="color: #800080;">$sx</span>, <span style="color: #800080;">$sy</span>, <span style="color: #800080;">$string</span>, <span style="color: #800080;">$textcolor</span><span style="color: #000000;">); </span><span style="color: #008080;"> 34</span> <span style="color: #0000ff;">return</span> <span style="color: #800080;">$image</span><span style="color: #000000;">; </span><span style="color: #008080;"> 35</span> <span style="color: #000000;"> } </span><span style="color: #008080;"> 36</span> imagego("img/1.jpg", "imageaddstring", <span style="color: #0000ff;">array</span>("Add a string on JPEG"<span style="color: #000000;">)); </span><span style="color: #008080;"> 37</span> imagego("img/2.gif", "imageaddstring", <span style="color: #0000ff;">array</span>("Add a string on GIF"<span style="color: #000000;">)); </span><span style="color: #008080;"> 38</span> imagego("img/3.png", "imageaddstring", <span style="color: #0000ff;">array</span>("Add a string on PNG"<span style="color: #000000;">)); </span><span style="color: #008080;"> 39</span> <span style="color: #008080;"> 40</span> <span style="color: #008000;">//</span><span style="color: #008000;">2. 圖片縮放與裁剪處理</span> <span style="color: #008080;"> 41</span> <span style="color: #008000;">/*</span><span style="color: #008000;"> imagecopyresized </span><span style="color: #008080;"> 42</span> <span style="color: #008000;"> * imagecopyresampled($dst_img.$src_img,$dst_x,$dst_y,$src_x,$src_y,$dst_w,$dst_h,$src_w,$src_h) </span><span style="color: #008080;"> 43</span> <span style="color: #008000;"> * 以上兩個(gè)函數(shù)都可以進(jìn)行圖片縮放,后者效果好一些.該函數(shù)可以在圖像內(nèi)部復(fù)制,但當(dāng)區(qū)域重復(fù)時(shí)后果不可知 </span><span style="color: #008080;"> 44</span> <span style="color: #008000;"> * 如果源和目標(biāo)的高寬不一樣,則會(huì)自動(dòng)進(jìn)行縮放. </span><span style="color: #008080;"> 45</span> <span style="color: #008000;"> * 同時(shí),該函數(shù)也可以實(shí)現(xiàn)圖像的裁剪. </span><span style="color: #008080;"> 46</span> <span style="color: #008000;">*/</span> <span style="color: #008080;"> 47</span> <span style="color: #008000;">//</span><span style="color: #008000;">該方法用于縮小圖片1.5倍</span> <span style="color: #008080;"> 48</span> <span style="color: #0000ff;">function</span> imagethumb(<span style="color: #800080;">$image</span>, <span style="color: #800080;">$width</span>, <span style="color: #800080;">$height</span>, <span style="color: #800080;">$type</span>, <span style="color: #800080;">$vars</span><span style="color: #000000;">){ </span><span style="color: #008080;"> 49</span> <span style="color: #800080;">$p</span> = <span style="color: #800080;">$vars</span>{0<span style="color: #000000;">}; </span><span style="color: #008080;"> 50</span> <span style="color: #800080;">$n_image</span> = imagecreatetruecolor(<span style="color: #800080;">$width</span>/<span style="color: #800080;">$p</span>, <span style="color: #800080;">$height</span>/<span style="color: #800080;">$p</span><span style="color: #000000;">); </span><span style="color: #008080;"> 51</span> imagecopyresampled(<span style="color: #800080;">$n_image</span>, <span style="color: #800080;">$image</span>, 0, 0, 0, 0, <span style="color: #800080;">$width</span>/<span style="color: #800080;">$p</span>, <span style="color: #800080;">$height</span>/<span style="color: #800080;">$p</span>, <span style="color: #800080;">$width</span>, <span style="color: #800080;">$height</span><span style="color: #000000;">); </span><span style="color: #008080;"> 52</span> imagedestroy(<span style="color: #800080;">$image</span><span style="color: #000000;">); </span><span style="color: #008080;"> 53</span> <span style="color: #0000ff;">return</span> <span style="color: #800080;">$n_image</span><span style="color: #000000;">; </span><span style="color: #008080;"> 54</span> <span style="color: #000000;"> } </span><span style="color: #008080;"> 55</span> imagego("img/1.jpg", "imagethumb", <span style="color: #0000ff;">array</span>(1.5<span style="color: #000000;">)); </span><span style="color: #008080;"> 56</span> imagego("img/2.gif", "imagethumb", <span style="color: #0000ff;">array</span>(1.5<span style="color: #000000;">)); </span><span style="color: #008080;"> 57</span> imagego("img/3.png", "imagethumb", <span style="color: #0000ff;">array</span>(1.5<span style="color: #000000;">)); </span><span style="color: #008080;"> 58</span> <span style="color: #008000;">//</span><span style="color: #008000;">該方法用于實(shí)現(xiàn)圖片的裁剪</span> <span style="color: #008080;"> 59</span> <span style="color: #0000ff;">function</span> imagecut(<span style="color: #800080;">$image</span>, <span style="color: #800080;">$width</span>, <span style="color: #800080;">$height</span>, <span style="color: #800080;">$type</span>, <span style="color: #800080;">$vars</span><span style="color: #000000;">) { </span><span style="color: #008080;"> 60</span> <span style="color: #800080;">$n_x</span> = <span style="color: #800080;">$vars</span>{0<span style="color: #000000;">}; </span><span style="color: #008080;"> 61</span> <span style="color: #800080;">$n_y</span> = <span style="color: #800080;">$vars</span>{1<span style="color: #000000;">}; </span><span style="color: #008080;"> 62</span> <span style="color: #800080;">$n_width</span> = <span style="color: #800080;">$vars</span>{2<span style="color: #000000;">}; </span><span style="color: #008080;"> 63</span> <span style="color: #800080;">$n_height</span> = <span style="color: #800080;">$vars</span>{3<span style="color: #000000;">}; </span><span style="color: #008080;"> 64</span> <span style="color: #008080;"> 65</span> <span style="color: #800080;">$n_image</span> = imagecreatetruecolor(<span style="color: #800080;">$n_width</span>, <span style="color: #800080;">$n_height</span><span style="color: #000000;">); </span><span style="color: #008080;"> 66</span> imagecopyresampled(<span style="color: #800080;">$n_image</span>, <span style="color: #800080;">$image</span>, 0, 0, <span style="color: #800080;">$n_x</span>, <span style="color: #800080;">$n_y</span>, <span style="color: #800080;">$n_width</span>, <span style="color: #800080;">$n_height</span>, <span style="color: #800080;">$n_width</span>, <span style="color: #800080;">$n_height</span><span style="color: #000000;">); </span><span style="color: #008080;"> 67</span> imagedestroy(<span style="color: #800080;">$image</span><span style="color: #000000;">); </span><span style="color: #008080;"> 68</span> <span style="color: #0000ff;">return</span> <span style="color: #800080;">$n_image</span><span style="color: #000000;">; </span><span style="color: #008080;"> 69</span> <span style="color: #000000;"> } </span><span style="color: #008080;"> 70</span> imagego("img/1.jpg", "imagecut", <span style="color: #0000ff;">array</span>(200, 100, 100, 100<span style="color: #000000;">)); </span><span style="color: #008080;"> 71</span> imagego("img/2.gif", "imagecut", <span style="color: #0000ff;">array</span>(200, 100, 100, 100<span style="color: #000000;">)); </span><span style="color: #008080;"> 72</span> imagego("img/3.png", "imagecut", <span style="color: #0000ff;">array</span>(200, 100, 100, 100<span style="color: #000000;">)); </span><span style="color: #008080;"> 73</span> <span style="color: #008080;"> 74</span> <span style="color: #008000;">//</span><span style="color: #008000;">3. 添加圖片水印</span> <span style="color: #008080;"> 75</span> <span style="color: #008000;">/*</span><span style="color: #008000;"> imagecopy($dst_img,$src_img,$dst_x,$dst_y,$src_x,$src_y,$src_w,$src_h) </span><span style="color: #008080;"> 76</span> <span style="color: #008000;"> * 將一個(gè)圖片復(fù)制到另一個(gè)圖片上面 </span><span style="color: #008080;"> 77</span> <span style="color: #008000;">*/</span> <span style="color: #008080;"> 78</span> <span style="color: #0000ff;">function</span> watermark(<span style="color: #800080;">$image</span>, <span style="color: #800080;">$width</span>, <span style="color: #800080;">$height</span>, <span style="color: #800080;">$type</span>, <span style="color: #800080;">$vars</span><span style="color: #000000;">){ </span><span style="color: #008080;"> 79</span> <span style="color: #800080;">$w_image</span> = imagecreatefrompng("img/logo.png"<span style="color: #000000;">); </span><span style="color: #008080;"> 80</span> <span style="color: #0000ff;">list</span>(<span style="color: #800080;">$src_w</span>, <span style="color: #800080;">$src_h</span>) = <span style="color: #008080;">getimagesize</span>("img/logo.png"<span style="color: #000000;">); </span><span style="color: #008080;"> 81</span> imagecopy(<span style="color: #800080;">$image</span>, <span style="color: #800080;">$w_image</span>, <span style="color: #800080;">$vars</span>{0}, <span style="color: #800080;">$vars</span>{1}, 0, 0, <span style="color: #800080;">$src_w</span>, <span style="color: #800080;">$src_h</span><span style="color: #000000;">); </span><span style="color: #008080;"> 82</span> imagedestroy(<span style="color: #800080;">$w_image</span><span style="color: #000000;">); </span><span style="color: #008080;"> 83</span> <span style="color: #0000ff;">return</span> <span style="color: #800080;">$image</span><span style="color: #000000;">; </span><span style="color: #008080;"> 84</span> <span style="color: #000000;"> } </span><span style="color: #008080;"> 85</span> imagego("img/1.jpg", "watermark", <span style="color: #0000ff;">array</span>(50,50<span style="color: #000000;">)); </span><span style="color: #008080;"> 86</span> imagego("img/2.gif", "watermark", <span style="color: #0000ff;">array</span>(100,100<span style="color: #000000;">)); </span><span style="color: #008080;"> 87</span> imagego("img/3.png", "watermark", <span style="color: #0000ff;">array</span>(150,150<span style="color: #000000;">)); </span><span style="color: #008080;"> 88</span> <span style="color: #008080;"> 89</span> <span style="color: #008000;">//</span><span style="color: #008000;">4. 圖片的選裝和翻轉(zhuǎn)</span> <span style="color: #008080;"> 90</span> <span style="color: #008000;">/*</span><span style="color: #008000;"> imagerotate($src,$degree,$bgcolor,[ignore transparent]) 可選參數(shù)是否忽視透明色,返回旋轉(zhuǎn)后的圖片 </span><span style="color: #008080;"> 91</span> <span style="color: #008000;"> * 翻轉(zhuǎn)用imagecopy按像素行或者像素列復(fù)制就可以了 </span><span style="color: #008080;"> 92</span> <span style="color: #008000;"> * 下面的函數(shù)先旋轉(zhuǎn)再翻轉(zhuǎn) </span><span style="color: #008080;"> 93</span> <span style="color: #008000;">*/</span> <span style="color: #008080;"> 94</span> <span style="color: #0000ff;">function</span> rotateandturn(<span style="color: #800080;">$image</span>, <span style="color: #800080;">$width</span>, <span style="color: #800080;">$height</span>, <span style="color: #800080;">$type</span>, <span style="color: #800080;">$vars</span><span style="color: #000000;">) { </span><span style="color: #008080;"> 95</span> <span style="color: #800080;">$angle</span> = <span style="color: #800080;">$vars</span>{0<span style="color: #000000;">}; </span><span style="color: #008080;"> 96</span> <span style="color: #800080;">$image</span> = imagerotate(<span style="color: #800080;">$image</span>, <span style="color: #800080;">$angle</span>, 0<span style="color: #000000;">); </span><span style="color: #008080;"> 97</span> <span style="color: #008000;">//</span><span style="color: #008000;">選裝后圖片大小可能發(fā)生變化</span> <span style="color: #008080;"> 98</span> <span style="color: #800080;">$width</span> = imagesx(<span style="color: #800080;">$image</span><span style="color: #000000;">); </span><span style="color: #008080;"> 99</span> <span style="color: #800080;">$height</span> = imagesy(<span style="color: #800080;">$image</span><span style="color: #000000;">); </span><span style="color: #008080;">100</span> <span style="color: #800080;">$n_image</span> = imagecreatetruecolor(<span style="color: #800080;">$width</span>, <span style="color: #800080;">$height</span><span style="color: #000000;">); </span><span style="color: #008080;">101</span> <span style="color: #008000;">//</span><span style="color: #008000;">1代表x軸翻轉(zhuǎn),2代表y軸翻轉(zhuǎn),0代表不翻轉(zhuǎn)</span> <span style="color: #008080;">102</span> <span style="color: #0000ff;">if</span> (<span style="color: #800080;">$vars</span>{1} == 1<span style="color: #000000;">) { </span><span style="color: #008080;">103</span> <span style="color: #0000ff;">for</span> (<span style="color: #800080;">$x</span> = 0; <span style="color: #800080;">$x</span> $width; <span style="color: #800080;">$x</span>++<span style="color: #000000;">) { </span><span style="color: #008080;">104</span> imagecopy(<span style="color: #800080;">$n_image</span>, <span style="color: #800080;">$image</span>, <span style="color: #800080;">$x</span>, 0, <span style="color: #800080;">$width</span>-<span style="color: #800080;">$x</span>-1, 0, 1, <span style="color: #800080;">$height</span><span style="color: #000000;">); </span><span style="color: #008080;">105</span> <span style="color: #000000;"> } </span><span style="color: #008080;">106</span> } <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> (<span style="color: #800080;">$vars</span>{1} == 2<span style="color: #000000;">){ </span><span style="color: #008080;">107</span> <span style="color: #0000ff;">for</span> (<span style="color: #800080;">$x</span> = 0; <span style="color: #800080;">$x</span> $height; <span style="color: #800080;">$x</span>++<span style="color: #000000;">) { </span><span style="color: #008080;">108</span> imagecopy(<span style="color: #800080;">$n_image</span>, <span style="color: #800080;">$image</span>, 0, <span style="color: #800080;">$x</span>, 0, <span style="color: #800080;">$height</span>-<span style="color: #800080;">$x</span>-1, <span style="color: #800080;">$width</span>, 1<span style="color: #000000;">); </span><span style="color: #008080;">109</span> <span style="color: #000000;"> } </span><span style="color: #008080;">110</span> } <span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #008080;">111</span> imagecopy(<span style="color: #800080;">$n_image</span>, <span style="color: #800080;">$image</span>, 0, 0, 0, 0, <span style="color: #800080;">$width</span>, <span style="color: #800080;">$height</span><span style="color: #000000;">); </span><span style="color: #008080;">112</span> <span style="color: #000000;"> } </span><span style="color: #008080;">113</span> <span style="color: #008080;">114</span> imagedestroy(<span style="color: #800080;">$image</span><span style="color: #000000;">); </span><span style="color: #008080;">115</span> <span style="color: #0000ff;">return</span> <span style="color: #800080;">$n_image</span><span style="color: #000000;">; </span><span style="color: #008080;">116</span> <span style="color: #000000;"> } </span><span style="color: #008080;">117</span> imagego("img/1.jpg", "rotateandturn", <span style="color: #0000ff;">array</span>(10, 0<span style="color: #000000;">)); </span><span style="color: #008080;">118</span> imagego("img/2.gif", "rotateandturn", <span style="color: #0000ff;">array</span>(0,1<span style="color: #000000;">)); </span><span style="color: #008080;">119</span> imagego("img/3.png", "rotateandturn", <span style="color: #0000ff;">array</span>(10,2<span style="color: #000000;">)); </span><span style="color: #008080;">120</span> ?> <span style="color: #008080;">121</span> <span style="color: #008080;">122</span> <span style="color: #008080;">123</span> 原圖<br> <span style="color: #008080;">124</span> <img src="/static/imghw/default1.png" data-src="img/1.jpg" class="lazy" alt=""><img src="/static/imghw/default1.png" data-src="img/2.gif" class="lazy" alt=""><img src="/static/imghw/default1.png" data-src="img/3.png" class="lazy" alt=""> <span style="color: #008080;">125</span> <br>圖中添加字符串<br> <span style="color: #008080;">126</span> <img src="/static/imghw/default1.png" data-src="img/1_imageaddstring.jpg" class="lazy" alt=""><img src="/static/imghw/default1.png" data-src="img/2_imageaddstring.gif" class="lazy" alt=""><img src="/static/imghw/default1.png" data-src="img/3_imageaddstring.png" class="lazy" alt=""> <span style="color: #008080;">127</span> <br>圖片縮放<br> <span style="color: #008080;">128</span> <img src="/static/imghw/default1.png" data-src="img/1_imagethumb.jpg" class="lazy" alt=""><img src="/static/imghw/default1.png" data-src="img/2_imagethumb.gif" class="lazy" alt=""><img src="/static/imghw/default1.png" data-src="img/3_imagethumb.png" class="lazy" alt=""> <span style="color: #008080;">129</span> <br>圖片裁剪<br> <span style="color: #008080;">130</span> <img src="/static/imghw/default1.png" data-src="img/1_imagecut.jpg" class="lazy" alt=""><img src="/static/imghw/default1.png" data-src="img/2_imagecut.gif" class="lazy" alt=""><img src="/static/imghw/default1.png" data-src="img/3_imagecut.png" class="lazy" alt=""> <span style="color: #008080;">131</span> <br>圖片水印<br> <span style="color: #008080;">132</span> <img src="/static/imghw/default1.png" data-src="img/1_watermark.jpg" class="lazy" alt=""><img src="/static/imghw/default1.png" data-src="img/2_watermark.gif" class="lazy" alt=""><img src="/static/imghw/default1.png" data-src="img/3_watermark.png" class="lazy" alt=""> <span style="color: #008080;">133</span> <br>圖片旋轉(zhuǎn)和翻轉(zhuǎn)<br> <span style="color: #008080;">134</span> <img src="/static/imghw/default1.png" data-src="img/1_rotateandturn.jpg" class="lazy" alt=""><img src="/static/imghw/default1.png" data-src="img/2_rotateandturn.gif" class="lazy" alt=""><img src="/static/imghw/default1.png" data-src="img/3_rotateandturn.png" class="lazy" alt=""> <span style="color: #008080;">135</span> <span style="color: #008080;">136</span> <span style="color: #008080;">137</span>
執(zhí)行結(jié)果

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The method to get the current session ID in PHP is to use the session_id() function, but you must call session_start() to successfully obtain it. 1. Call session_start() to start the session; 2. Use session_id() to read the session ID and output a string similar to abc123def456ghi789; 3. If the return is empty, check whether session_start() is missing, whether the user accesses for the first time, or whether the session is destroyed; 4. The session ID can be used for logging, security verification and cross-request communication, but security needs to be paid attention to. Make sure that the session is correctly enabled and the ID can be obtained successfully.

To extract substrings from PHP strings, you can use the substr() function, which is syntax substr(string$string,int$start,?int$length=null), and if the length is not specified, it will be intercepted to the end; when processing multi-byte characters such as Chinese, you should use the mb_substr() function to avoid garbled code; if you need to intercept the string according to a specific separator, you can use exploit() or combine strpos() and substr() to implement it, such as extracting file name extensions or domain names.

In PHP, the most common method is to split the string into an array using the exploit() function. This function divides the string into multiple parts through the specified delimiter and returns an array. The syntax is exploit(separator, string, limit), where separator is the separator, string is the original string, and limit is an optional parameter to control the maximum number of segments. For example $str="apple,banana,orange";$arr=explode(",",$str); The result is ["apple","bana

std::chrono is used in C to process time, including obtaining the current time, measuring execution time, operation time point and duration, and formatting analysis time. 1. Use std::chrono::system_clock::now() to obtain the current time, which can be converted into a readable string, but the system clock may not be monotonous; 2. Use std::chrono::steady_clock to measure the execution time to ensure monotony, and convert it into milliseconds, seconds and other units through duration_cast; 3. Time point (time_point) and duration (duration) can be interoperable, but attention should be paid to unit compatibility and clock epoch (epoch)

ToaccessenvironmentvariablesinPHP,usegetenv()orthe$_ENVsuperglobal.1.getenv('VAR_NAME')retrievesaspecificvariable.2.$_ENV['VAR_NAME']accessesvariablesifvariables_orderinphp.iniincludes"E".SetvariablesviaCLIwithVAR=valuephpscript.php,inApach

PHPhasthreecommentstyles://,#forsingle-lineand/.../formulti-line.Usecommentstoexplainwhycodeexists,notwhatitdoes.MarkTODO/FIXMEitemsanddisablecodetemporarilyduringdebugging.Avoidover-commentingsimplelogic.Writeconcise,grammaticallycorrectcommentsandu

There are three key ways to avoid the "undefinedindex" error: First, use isset() to check whether the array key exists and ensure that the value is not null, which is suitable for most common scenarios; second, use array_key_exists() to only determine whether the key exists, which is suitable for situations where the key does not exist and the value is null; finally, use the empty merge operator?? (PHP7) to concisely set the default value, which is recommended for modern PHP projects, and pay attention to the spelling of form field names, use extract() carefully, and check the array is not empty before traversing to further avoid risks.

When using PHP preprocessing statements to execute queries with IN clauses, 1. Dynamically generate placeholders according to the length of the array; 2. When using PDO, you can directly pass in the array, and use array_values to ensure continuous indexes; 3. When using mysqli, you need to construct type strings and bind parameters, pay attention to the way of expanding the array and version compatibility; 4. Avoid splicing SQL, processing empty arrays, and ensuring data types match. The specific method is: first use implode and array_fill to generate placeholders, and then bind parameters according to the extended characteristics to safely execute IN queries.
