Table des matières
'.$title.'
Maison php教程 php手册 php处理图片

php处理图片

Jun 06, 2016 pm 07:46 PM
php 图片 处理 时间

这段时间好浮躁,不知道该从哪下手了。想学这个又觉得现在最缺那个,学了这个现在要用到那个。不知道走左边还是右边了,虽然都能到目的地。 这是图片处理的笔记。只是个人笔记,没有细细研究,并且有个小错误。 1 ? php 2 header ("content-type:text/html;

这段时间好浮躁,不知道该从哪下手了。想学这个又觉得现在最缺那个,学了这个现在要用到那个。不知道走左边还是右边了,虽然都能到目的地。

这是图片处理的笔记。只是个人笔记,没有细细研究,并且有个小错误。

<span>  1</span> <span>php
</span><span>  2</span> <span>header</span>("content-type:text/html; charset=utf-8"<span>);
</span><span>  3</span> <span>error_reporting</span>(<span>E_ERROR</span> | <span>E_WARNING</span> | <span>E_PARSE</span><span>);
</span><span>  4</span> <span>function</span> echoHtml(<span>$title</span>, <span>$info</span> = '', <span>$content</span> = <span>array</span>(), <span>$tip</span> = ''<span>)
</span><span>  5</span> <span>{
</span><span>  6</span>     <span>$html</span>    = ''<span>;
</span><span>  7</span>     <span>$html</span>    .= '<h3 id="span-title-span">'.<span>$title</span>.'</h3>'<span>;
</span><span>  8</span>     <span>if</span>(<span>$info</span><span>)
</span><span>  9</span> <span>    {
</span><span> 10</span>         <span>$html</span>    .= "<p>{<span>$info</span>}</p>"<span>;
</span><span> 11</span> <span>    }
</span><span> 12</span>     <span>if</span>(<span>is_array</span>(<span>$content</span>) && !<span>empty</span>(<span>$content</span><span>))
</span><span> 13</span> <span>    {
</span><span> 14</span>         <span>foreach</span> (<span>$content</span> <span>as</span> <span>$key</span> => <span>$row</span><span>)
</span><span> 15</span> <span>        {
</span><span> 16</span>             <span>$i</span>    = <span>$key</span>+1<span>;
</span><span> 17</span>             <span>$html</span>    .= "<p>\t{<span>$i</span>}:\t{<span>$row</span>}</p>"<span>;
</span><span> 18</span> <span>        }
</span><span> 19</span> <span>    }
</span><span> 20</span>     <span>if</span>(<span>$tip</span><span>)
</span><span> 21</span> <span>    {
</span><span> 22</span>         <span>$html</span>    .= "<p><b>\tTIP:{<span>$tip</span>}</b></p>"<span>;
</span><span> 23</span> <span>    }
</span><span> 24</span>     <span>$html</span>    .= "<hr><br>"<span>;
</span><span> 25</span>     <span>echo</span> <span>$html</span><span>;
</span><span> 26</span> <span>}
</span><span> 27</span> <span>/*</span><span>*
</span><span> 28</span> <span> *    生成缩略图
</span><span> 29</span> <span> *    @param $src_img 操作图片文件名
</span><span> 30</span> <span> *    @param $new_img 新的图片文件名
</span><span> 31</span> <span> *    @param $n_w 宽度
</span><span> 32</span> <span> *    @param $n_h 高度
</span><span> 33</span> <span> *    @return resouce 
</span><span> 34</span> <span> *    @author cntnn11
</span><span> 35</span> <span> *    @date 2013-03-10
</span><span> 36</span> <span>*/</span>
<span> 37</span> <span>function</span> thumb(<span>$src_img</span>, <span>$new_img</span>, <span>$n_w</span> = 0, <span>$n_h</span> = 0<span>)
</span><span> 38</span> <span>{
</span><span> 39</span>     <span>if</span>(<span>is_file</span>(<span>$src_img</span><span>))
</span><span> 40</span> <span>    {
</span><span> 41</span>         <span>list</span>(<span>$s_w</span>, <span>$w_h</span>, <span>$s_t</span>)    = <span>getimagesize</span>(<span>$src_img</span><span>);
</span><span> 42</span> 
<span> 43</span>         <span>//</span><span>对心图片的宽高进行等比缩放限制,使用固定的公式
</span><span> 44</span> <span>        //缩放以原始图片最大的边做为新图片最大的尺寸,另一边则根据以下公式进行等比缩放
</span><span> 45</span> <span>        //如果原图的宽度小于高度,那么重新计算新图的宽度,否则重新计算新图的高度</span>
<span> 46</span>         <span>if</span>(<span>$n_w</span> && (<span>$s_w</span> $s_h<span>))
</span><span> 47</span> <span>        {
</span><span> 48</span>             <span>$n_w</span>    = (<span>$n_h</span> / <span>$s_h</span>) * <span>$s_h</span><span>;
</span><span> 49</span> <span>        }
</span><span> 50</span>         <span>else</span>
<span> 51</span> <span>        {
</span><span> 52</span>             <span>$n_h</span>    = (<span>$n_w</span> / <span>$s_w</span>) * <span>$s_w</span><span>;
</span><span> 53</span> <span>        }
</span><span> 54</span> 
<span> 55</span>         <span>//</span><span>开始生成</span>
<span> 56</span>         <span>$res_img_new</span>    = imagecreatetruecolor(<span>$n_w</span>, <span>$n_h</span><span>);
</span><span> 57</span>         <span>switch</span>(<span>$s_t</span><span>)
</span><span> 58</span> <span>        {
</span><span> 59</span>             <span>case</span> 1:
<span> 60</span>                 <span>$res_img</span>    = imagecreatefromgif(<span>$src_img</span><span>);
</span><span> 61</span>                 <span>$ext</span>        = 'gif'<span>;
</span><span> 62</span>                 <span>break</span><span>;
</span><span> 63</span>             <span>case</span> 2:
<span> 64</span>                 <span>$res_img</span>    = imagecreatefromjpeg(<span>$src_img</span><span>);
</span><span> 65</span>                 <span>$ext</span>        = 'jpg'<span>;
</span><span> 66</span>                 <span>break</span><span>;
</span><span> 67</span>             <span>case</span> 3:
<span> 68</span>                 <span>$res_img</span>    = imagecreatefrompng(<span>$src_img</span><span>);
</span><span> 69</span>                 <span>$ext</span>        = 'png'<span>;
</span><span> 70</span>                 <span>break</span><span>;
</span><span> 71</span>             <span>default</span>:
<span> 72</span>                 <span>echo</span>  '不支持的图片类型'<span>;
</span><span> 73</span>                 <span>return</span> <span>false</span><span>;
</span><span> 74</span>                 <span>break</span><span>;
</span><span> 75</span> <span>        }
</span><span> 76</span> 
<span> 77</span>         imagecopyresampled(<span>$res_img_new</span>, <span>$res_img</span>, 0, 0, 0, 0, <span>$n_w</span>, <span>$n_h</span>, <span>$s_w</span>, <span>$s_h</span><span>);
</span><span> 78</span>         <span>//</span><span>$res_img_new    = imagecreate($n_w, $n_h);
</span><span> 79</span> <span>        //imagecopyresized($res_img_new, $res_img, 0, 0, 0, 0, $n_w, $n_h, $s_w, $s_h);</span>
<span> 80</span>         <span>global</span> <span>$img_new_dir</span><span>;
</span><span> 81</span>         <span>$new_img_file</span>    = <span>$img_new_dir</span>.<span>$new_img</span>.'.'.<span>$ext</span><span>;
</span><span> 82</span>         imagejpeg(<span>$res_img_new</span>, <span>$new_img_file</span><span>);
</span><span> 83</span>         <span>//</span><span>genImage($res_img_new, $new_img_file, $s_t);</span>
<span> 84</span>         imagedestroy(<span>$res_img</span><span>);
</span><span> 85</span>         imagedestroy(<span>$res_img_new</span><span>);
</span><span> 86</span>         <span>echo</span> '缩放后的图片:'<span>;
</span><span> 87</span>         <span>echo</span> '<img  src="/static/imghw/default1.png" data-src="'.<span>$new_img_file</span>.'" class="lazy" . alt="php处理图片" >$new_img_file.'" alt="生成的缩略图" ><hr><br>'<span>;
</span><span> 88</span> <span>    }
</span><span> 89</span> <span>}
</span><span> 90</span> <span>/*</span><span>*
</span><span> 91</span> <span> *    接收传入参数生成一张图片
</span><span> 92</span> <span> *    @param $img_res 处理好的图片资源
</span><span> 93</span> <span> *    @param $img_file 新的图片地址
</span><span> 94</span> <span> *    @param $img_type 图片类型
</span><span> 95</span> <span> *    @return resouce 
</span><span> 96</span> <span> *    @author cntnn11
</span><span> 97</span> <span> *    @date 2013-03-10
</span><span> 98</span> <span>*/</span>
<span> 99</span> <span>function</span> genImage(<span>$img_res</span>, <span>$img_file</span>, <span>$img_type</span> = 2<span>)
</span><span>100</span> <span>{
</span><span>101</span>     <span>switch</span>(<span>$img_type</span><span>)
</span><span>102</span> <span>    {
</span><span>103</span>         <span>case</span> 1:
<span>104</span>             <span>return</span> imagegif(<span>$img_res</span>, <span>$img_file</span><span>);
</span><span>105</span>             <span>break</span><span>;
</span><span>106</span>         <span>case</span> 2:
<span>107</span>             <span>return</span> imagejpeg(<span>$img_res</span>, <span>$img_file</span><span>);
</span><span>108</span>             <span>break</span><span>;
</span><span>109</span>         <span>case</span> 3:
<span>110</span>             <span>return</span> imagepng(<span>$img_res</span>, <span>$img_file</span><span>);
</span><span>111</span>             <span>break</span><span>;
</span><span>112</span>         <span>default</span>:
<span>113</span>             <span>return</span> '不支持的图片类型'<span>;
</span><span>114</span>             <span>break</span><span>;
</span><span>115</span> <span>    }
</span><span>116</span> <span>}
</span><span>117</span> 
<span>118</span> <span>/*</span><span>==============================================================================================================================</span><span>*/</span>
<span>119</span> <span>global</span> <span>$img_name</span>,<span>$img_new_dir</span><span>;
</span><span>120</span> <span>$img_name</span>    = 'testimg.jpg'<span>;
</span><span>121</span> <span>$img_new_dir</span>    = 'genimages/'<span>;
</span><span>122</span> 
<span>123</span> <span>echo</span> "<p>原始测试图片:‘{<span>$img_name</span>}’</p>"<span>;
</span><span>124</span> <span>echo</span> '<img  src="/static/imghw/default1.png" data-src="'.<span>$img_name</span>.'" class="lazy" . alt="php处理图片" >$img_name.'" alt="测试图片" width="200px" >'<span>;
</span><span>125</span> 
<span>126</span> 
<span>127</span> <span>//</span><span>获取图片资源,在此使用jpg格式做测试图片</span>
<span>128</span> <span>$img_sour</span>    = imagecreatefromjpeg('testimg.jpg'<span>);
</span><span>129</span> 
<span>130</span> <span>//</span><span>图片宽高的获取</span>
<span>131</span> <span>$title</span>    = "图片宽高度获取"<span>;
</span><span>132</span> <span>$info</span>    = "涉及使用函数imagesx(img source) imagesy(img source)"<span>;
</span><span>133</span> <span>$content</span>= <span>array</span><span>(
</span><span>134</span>     'imagesx(image):传入一个通过imagecreate_()函数创建的图片资源类型做为参数,返回宽度',
<span>135</span>     'imagesy(image):同imagesx,返回高度!',
<span>136</span>     '测试图片的原始宽度:'.imagesx(<span>$img_sour</span>).'px 原始高度:'.imagesy(<span>$img_sour</span>).'px',
<span>137</span> <span>);
</span><span>138</span> <span>$tip</span>    = "这两个函数的参数必须是图片资源类型!!"<span>;
</span><span>139</span> echoHtml(<span>$title</span>, <span>$info</span>, <span>$content</span>, <span>$tip</span><span>);
</span><span>140</span> 
<span>141</span> <span>//</span><span>另一种获取图片属性的方法</span>
<span>142</span> <span>$title</span>    = "使用getimagesize(图片名);获取图片的属性"<span>;
</span><span>143</span> <span>$info</span>    = "该函数返回一个数组,包含了宽度、高度、图片类型信息!只需接收一个文件名即可获取!"<span>;
</span><span>144</span> <span>$content</span>= <span>array</span><span>(
</span><span>145</span>     '使用方式:getimagesize('.<span>$img_name</span>.')',
<span>146</span>     '返回一个数组:下标0表示宽度,下标1表示高度,下标2表示图片类型',
<span>147</span>     '    图片类型说明:1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,6 = BMP,7 = TIFF(intel byte order),8 = TIFF(motorola byte order),9 = JPC,10 = JP2,11 = JPX,12 = JB2,13 = SWC,14 = IFF,15 = WBMP,16 = XBM',
<span>148</span>     '下标从3开始的则为文本类型,可以说是对0、1、2三个下标值的说明',
<span>149</span> <span>);
</span><span>150</span> echoHtml(<span>$title</span>, <span>$info</span>, <span>$content</span>, <span>$tip</span><span>);
</span><span>151</span> 
<span>152</span> <span>//</span><span>图片缩放函数</span>
<span>153</span> <span>$title</span>    = "图片缩放,使用效果更好的imagecopyresampled()函数"<span>;
</span><span>154</span> <span>$info</span>    = "主要学习等比缩放,因为不对宽高进行约束,那么新生成的图片会变形"<span>;
</span><span>155</span> <span>$content</span>= <span>array</span><span>(
</span><span>156</span>     'imagecopyresampled(dst_image, src_image, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h)',
<span>157</span>     '参数说明:',
<span>158</span>     '   dst_image【新图片】 图片资源类型',
<span>159</span>     '   src_image【要缩略的原始图片】 图片资源类型',
<span>160</span>     '   dst_x,dst_y,dst_w,dst_h【缩略图片的X轴起点,y轴起点,宽度和高度】 数值类型',
<span>161</span>     '   src_x,src_y,src_w,src_h【原始图片的X轴起点,y轴起点,宽度和高度】 数值类型',
<span>162</span> <span>);
</span><span>163</span> <span>$tip</span>    = "还有一个imagecopyresized()的函数,参数同imagecopyresampled()函数一致,但效果没有他好,不知道为什么?"<span>;
</span><span>164</span> echoHtml(<span>$title</span>, <span>$info</span>, <span>$content</span>, <span>$tip</span><span>);
</span><span>165</span> 
<span>166</span> <span>//</span><span>测试缩略方法</span>
<span>167</span> <span>$title</span>    = '写一个thumb()方法,用来等比缩略一张图片'<span>;
</span><span>168</span> <span>$info</span>    = '使用imagecopyresampled()函数来使用'<span>;
</span><span>169</span> <span>$content</span>= <span>array</span><span>(
</span><span>170</span>     '首先确定参数:原始图片,缩略图片目标位置,缩略图片的宽度和高度',
<span>171</span>     '获取图片的属性,宽高、类型,以创建相应的图片资源',
<span>172</span>     '使用固定公式算出新图片等比缩放的宽高',
<span>173</span>     '根据图片类型生成新的缩略图片',
<span>174</span>     '释放图片资源',
<span>175</span> <span>);
</span><span>176</span> <span>$tip</span>    = "生成了黑色的图片,需要解决!!!!!!!!!!!"<span>;
</span><span>177</span> echoHtml(<span>$titl</span>, <span>$info</span>, <span>$content</span>, <span>$tip</span><span>);
</span><span>178</span> thumb(<span>$img_name</span>, 'suolve1', 500, 500<span>);
</span><span>179</span> 
<span>180</span> <span>//</span><span>图片透明</span>
<span>181</span> <span>/*</span><span>imagecolortransparent()
</span><span>182</span> <span>imagecolorstotal()
</span><span>183</span> <span>imagecolorsforindex()
</span><span>184</span> <span>imagecolorallocate()
</span><span>185</span> <span>imagefill()</span><span>*/</span>
<span>186</span> 
<span>187</span> imagedestroy(<span>$img_sour</span>);
Copier après la connexion

 

Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn

Outils d'IA chauds

Undresser.AI Undress

Undresser.AI Undress

Application basée sur l'IA pour créer des photos de nu réalistes

AI Clothes Remover

AI Clothes Remover

Outil d'IA en ligne pour supprimer les vêtements des photos.

Undress AI Tool

Undress AI Tool

Images de déshabillage gratuites

Clothoff.io

Clothoff.io

Dissolvant de vêtements AI

AI Hentai Generator

AI Hentai Generator

Générez AI Hentai gratuitement.

Article chaud

R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
2 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Meilleurs paramètres graphiques
2 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Comment réparer l'audio si vous n'entendez personne
2 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌

Outils chauds

Bloc-notes++7.3.1

Bloc-notes++7.3.1

Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise

SublimeText3 version chinoise

Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1

Envoyer Studio 13.0.1

Puissant environnement de développement intégré PHP

Dreamweaver CS6

Dreamweaver CS6

Outils de développement Web visuel

SublimeText3 version Mac

SublimeText3 version Mac

Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Configuration du projet CakePHP Configuration du projet CakePHP Sep 10, 2024 pm 05:25 PM

Dans ce chapitre, nous comprendrons les variables d'environnement, la configuration générale, la configuration de la base de données et la configuration de la messagerie dans CakePHP.

Guide d'installation et de mise à niveau de PHP 8.4 pour Ubuntu et Debian Guide d'installation et de mise à niveau de PHP 8.4 pour Ubuntu et Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 apporte plusieurs nouvelles fonctionnalités, améliorations de sécurité et de performances avec une bonne quantité de dépréciations et de suppressions de fonctionnalités. Ce guide explique comment installer PHP 8.4 ou mettre à niveau vers PHP 8.4 sur Ubuntu, Debian ou leurs dérivés. Bien qu'il soit possible de compiler PHP à partir des sources, son installation à partir d'un référentiel APT comme expliqué ci-dessous est souvent plus rapide et plus sécurisée car ces référentiels fourniront les dernières corrections de bogues et mises à jour de sécurité à l'avenir.

Date et heure de CakePHP Date et heure de CakePHP Sep 10, 2024 pm 05:27 PM

Pour travailler avec la date et l'heure dans cakephp4, nous allons utiliser la classe FrozenTime disponible.

Téléchargement de fichiers CakePHP Téléchargement de fichiers CakePHP Sep 10, 2024 pm 05:27 PM

Pour travailler sur le téléchargement de fichiers, nous allons utiliser l'assistant de formulaire. Voici un exemple de téléchargement de fichiers.

Routage CakePHP Routage CakePHP Sep 10, 2024 pm 05:25 PM

Dans ce chapitre, nous allons apprendre les sujets suivants liés au routage ?

Discuter de CakePHP Discuter de CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP est un framework open source pour PHP. Il vise à faciliter grandement le développement, le déploiement et la maintenance d'applications. CakePHP est basé sur une architecture de type MVC à la fois puissante et facile à appréhender. Modèles, vues et contrôleurs gu

CakePHP travaillant avec la base de données CakePHP travaillant avec la base de données Sep 10, 2024 pm 05:25 PM

Travailler avec la base de données dans CakePHP est très simple. Nous comprendrons les opérations CRUD (Créer, Lire, Mettre à jour, Supprimer) dans ce chapitre.

CakePHP créant des validateurs CakePHP créant des validateurs Sep 10, 2024 pm 05:26 PM

Le validateur peut être créé en ajoutant les deux lignes suivantes dans le contrôleur.

See all articles