php 加水印 等比例缩放程序
function watermark($desImg,$waterImg,$positon=1,$saveas=false,$alpha=30)
011 {
012 //获取目图片的基本信息
013 $temp=pathinfo($desImg);
014 $name=$temp["basename"];//文件名
015 $path=$temp["dirname"];//文件所在的文件夹
016 $extension=$temp["extension"];//文件扩展名
017 if($saveas)
018 {
019 //需要另存为
020 $name=rtrim($name,".$extension")."_2.";//重新命名
021 $savepath=$path."/".$name.$extension;
022 }
023 else
024 {
025 //不需要另存为则覆盖原图
026 $savepath=$path."/".$name;
027 }
028 $info=getImageInfo($desImg);//获取目标图片的信息
029 $info2=getImageInfo($waterImg);//获取水印图片的信息
030
031 $desImg=create($desImg);//从原图创建
032 $waterImg=create($waterImg);//从水印图片创建
033 //位置1:顶部居左
034 if($positon==1)
035 {
036 $x=0;
037 $y=0;
038 }
039 //位置2:顶部居右
040 if($positon==2)
041 {
042 $x=$info[0]-$info2[0];
043 $y=0;
044 }
045 //位置3:居中
046 if($positon==3)
047 {
048 $x=($info[0]-$info2[0])/2;
049 $y=($info[1]-$info2[1])/2;
050 }
051 //位置4:底部居左
052 if($positon==4)
053 {
054 $x=0;
055 $y=$info[1]-$info2[1];
056 }
057 //位置5:底部居右
058 if($positon==5)
059 {
060 $x=$info[0]-$info2[0];
061 $y=$info[1]-$info2[1];
062 }
063 imagecopymerge($desImg,$waterImg,$x,$y,0,0,$info2[0],$info2[1],$alpha);
064 imagejpeg($desImg,$savepath);
065 imagedestroy($desImg);
066 imagedestroy($waterImg);
067 return $savepath;
068 }
069 /**
070 * 获取图片的信息,width,height,image/type
071 * @param string $src 图片路径
072 * @return 数组
073 * **/
074 function getImageInfo($src)
075 {
076 return getimagesize($src);
077 }
078 /**
079 * 创建图片,返回资源类型
080 * @param string $src 图片路径
081 * @return resource $im 返回资源类型
082 * **/
083 function create($src)
084 {
085 $info=getImageInfo($src);
086 switch ($info[2])
087 {
088 case 1:
089 $im=imagecreatefromgif($src);
090 break;
091 case 2:
092 $im=imagecreatefromjpeg($src);
093 break;
094 case 3:
095 $im=imagecreatefrompng($src);
096 break;
097 }
098 return $im;
099 }
100 /**
101 * 缩略图主函数
102 * @param string $src 图片路径
103 * @param int $w 缩略图宽度
104 * @param int $h 缩略图高度
105 * @return mixed 返回缩略图路径
106 * **/
107
108 function resize($src,$w,$h)
109 {
110 $temp=pathinfo($src);
111 $name=$temp["basename"];//文件名
112 $dir=$temp["dirname"];//文件所在的文件夹
113 $extension=$temp["extension"];//文件扩展名
114 $savepath="{$dir}/{$name}.thumb.jpg";//缩略图保存路径,新的文件名为*.thumb.jpg
115
116 //获取图片的基本信息
117 $info=getImageInfo($src);
118 $width=$info[0];//获取图片宽度
119 $height=$info[1];//获取图片高度
120 $per1=round($width/$height,2);//计算原图长宽比
121 $per2=round($w/$h,2);//计算缩略图长宽比
122
123 //计算缩放比例
124 if($per1>$per2||$per1==$per2)
125 {
126 //原图长宽比大于或者等于缩略图长宽比,则按照宽度优先
127 $per=$w/$width;
128 }
129 if($per1
130 {
131 //原图长宽比小于缩略图长宽比,则按照高度优先
132 $per=$h/$height;
133 }
134 $temp_w=intval($width*$per);//计算原图缩放后的宽度
135 $temp_h=intval($height*$per);//计算原图缩放后的高度
136 $temp_img=imagecreatetruecolor($temp_w,$temp_h);//创建画布
137 $im=create($src);
138 imagecopyresampled($temp_img,$im,0,0,0,0,$temp_w,$temp_h,$width,$height);
139 if($per1>$per2)
140 {
141 imagejpeg($temp_img,$savepath);
142 return addBg($savepath,$w,$h,"w");
143 //宽度优先,在缩放之后高度不足的情况下补上背景
144 }
145 if($per1==$per2)
146 {
147 imagejpeg($temp_img,$savepath);
148 return $savepath;
149 //等比缩放
150 }
151 if($per1
152 {
153 imagejpeg($temp_img,$savepath);
154
155 return addBg($savepath,$w,$h,"h");
156 //高度优先,在缩放之后宽度不足的情况下补上背景
157 }
158 }
159 /**
160 * 添加背景
161 * @param string $src 图片路径
162 * @param int $w 背景图像宽度
163 * @param int $h 背景图像高度
164 * @param String $first 决定图像最终位置的,w 宽度优先 h 高度优先 wh:等比
165 * @return 返回加上背景的图片
166 * **/
167 function addBg($src,$w,$h,$fisrt="w")
168 {
169 $bg=imagecreatetruecolor($w,$h);
170 $white = imagecolorallocate($bg,255,255,255);
171 imagefill($bg,0,0,$white);//填充背景
172
173 //获取目标图片信息
174 $info=getImageInfo($src);
175 $width=$info[0];//目标图片宽度
176 $height=$info[1];//目标图片高度
177 $img=create($src);
178 if($fisrt=="wh")
179 {
180 //等比缩放
181 return $src;
182 }
183 else
184 {
185 if($fisrt=="w")
186 {
187 $x=0;
188 $y=($h-$height)/2;//垂直居中
189 }
190 if($fisrt=="h")
191 {
192 $x=($w-$width)/2;//水平居中
193 $y=0;
194 }
195 imagecopymerge($bg,$img,$x,$y,0,0,$width,$height,100);
196 imagejpeg($bg,$src);
197 imagedestroy($bg);
198 imagedestroy($img);
199 return $src;
200 }
201
202 }

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

Dreamweaver Mac版
视觉化网页开发工具

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

WebStorm Mac版
好用的JavaScript开发工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)