PHP从零单排(十八)图片处理
1.打开已经存在的图片
<?phpheader ("Content-type:image/jpeg"); $img=imagecreatefromjpeg("cc.jpg");imagejpeg($img);imagedestroy($img);?>
2.获取图片的相关属性
<?php $img=imagecreatefromjpeg("cc.jpg");$x=imagesx($img);$y=imagesy($img);echo "图片cc.jpg的宽为:<b>$x pixels";echo "<br>";echo "<br>";echo "图片cc.jpg的高为:<b>$y</b> pixels";?>
array getimagesize(string $filename [, array &imageinfo])
<?php $img_info=getimagesize("cc.jpg");for($i=0;$i<4;++$i){ echo $img_info[$i]; echo "<br/>"; }?>
1:表示该图片是GIF格式
2:表示该图片是JPG格式
3:表示该图片是PNG格式
4:表示该图片是SWF格式
5:表示该图片是PSD格式
6:表示该图片是BMP格式
<?php $pic_name="ee.png";$pic_size=getimagesize($pic_name);?><img src="<?php%20echo%20%24pic_name;%20?>" echo alt="PHP从0单排(十八)图片处理" >>
·获取要添加水印的图片的宽、高值
·确定图片大小是否满足水印文字大小
·确定水印效果在图片中的位置
·设定图像的混色模式
·生成水印效果
·释放资源
<?php function makeimagewatermark($image,$pos,$water_text,$font_size,$color){ $font_type="c://WINDOWS//Fonts//SIMYOU.TTF"; if(!empty($image)&& file_exists($image)) { $img_info=getimagesize($image); $g_w=$img_info[0]; $g_h=$img_info[1]; switch($img_info[2]) { case 1: $img=imagecreatefromgif($image); break; case 2: $img=imagecreatefromjpeg($image); break; case 3: $img=imagecreatefrompng($image); break; default: die("Format Wrong"); } } else { die("Not exists!"); } $temp=imagettfbbox(ceil($font_size*2.5),0,$font_type,$water_text); $w=$temp[2]-$temp[6]; $h=$temp[3]-$temp[7]; if(($g_w<$w) || ($g_h<$h)) { echo "Too small!"; return; } switch($pos){ case 0: $pos_x=rand(0,($g_w-$w)); $pos_y=rand(0,($g_h-$h)); break; case 1: $pos_x=0; $pos_y=0; break; case 2: $pos_x=($g_w-$w)/2; $pos_y=($g_h-$h)/2; break; case 3: $pos_x=$g_w-$w; $pos_y=$g_h-$h; break; default: $pos_x=rand(0,($g_w-$w)); $pos_y=rand(0,($g_h-$h)); break; } imagealphablending($img,true);//设置图像混色模式 if(!empty($color) && (strlen($color)==7)){ $R=hexdec(substr($color,1,2)); $G=hexdec(substr($color,3,2)); $B=hexdec(substr($color,5)); } else { die("Format wrong!"); } $text_color=imagecolorallocate($img,$R,$G,$B); imagettftext($img,$font_size,0,$pos_x,$pos_y,$text_color,$font_type,$water_text); switch($img_info[2]) { case 1 : imagegif($img,$image); break; case 2 : imagejpeg($img,$image); break; case 3: imagepng($img,$image); break; default: die("Formate unSupport!"); } imagedestroy($img); }if(isset($_FILES) && !empty($_FILES['userfile'])&& $_FILES['userfile']['size']>0){$uploadfile="./".time()."_".$_FILES['userfile']['name'];if(copy($_FILES['userfile']['tmp_name'],$uploadfile)){ makeimagewatermark($uploadfile,2,"Photo by Mac",16,"#43042A"); echo "<img src="%5C%22%22.%24uploadfile.%22%5C%22" border='\"0\"' alt="PHP从0单排(十八)图片处理" >"; }else{ echo "uploadWrong!<br>"; }}?><title>19.9.php</title>
<?php header("Content-type:image/jpeg");$img_name="cc.jpg";$src_img=imagecreatefromjpeg($img_name);$ow=imagesx($src_img);$oh=imagesy($src_img);$nw=round($ow*200.0/$ow);$nh=round($oh*200.0/$oh);$desc_img=imagecreate($nw,$nh);imagecopyresized($desc_img,$src_img,0,0,0,0,$nw,$nh,$ow,$oh);imagejpeg($desc_img);imagedestroy($desc_img);imagedestroy($src_img);?>
<?php header("Content-type:image/jpeg");$img_name="cc.jpg";$percent=0.2;$src_img=imagecreatefromjpeg($img_name);$ow=imagesx($src_img);$oh=imagesy($src_img);$nw=$ow*$percent;$nh=$oh*$percent;$desc_img=imagecreatetruecolor($nw,$nh);imagecopyresampled($desc_img,$src_img,0,0,0,0,$nw,$nh,$ow,$oh);imagejpeg($desc_img);imagedestroy($desc_img);imagedestroy($src_img);?>