Php上传、生成缩略图、生成文字水印和图片水印 |
时间:2015-01-23 来源:西部数据 作者:西部数据 |
|
- <?php
-
-
-
-
-
-
-
-
-
-
-
-
- $bigfolder="img";
- $smallfolder="smallimg";
-
- $fdate=date("Ymd");
-
-
- $fname=date("YmdHis");
- $bigname="0724e_com_".$fname;
- $smallname="0724e_com_".$fname."_s";
-
-
- $bigaddrname=$bigfolder."/".$fdate."/".$bigname;
- $smalladdrname=$smallfolder."/".$fdate."/".$smallname;
-
- $RESIZEWIDTH=180;
- $RESIZEHEIGHT=150;
-
- $imgaddr="img.jpg";
- $fontname="www.0724e.com";
-
- $exit_name=array(".jpg",".gif",".png");
- if (isset($_POST['Submit'])){
-
-
- $upfile=$_FILES['image']['name'];
- $uperror=$_FILES['image']['error'];
-
- switch ($uperror) {
- case 1:
- die("<div align=\"center\">上传真过PHP设置充许的最大值<a href=\"java script:history.back();\">点击返回</a></div>");
- break;
- case 2:
- die("<div align=\"center\">上传超过了表单充许的最大值<a href=\"java script:history.back();\">点击返回</a></div>");
- break;
- case 3:
- die("<div align=\"center\">文件只有部分被上传了<a href=\"java script:history.back();\">点击返回</a></div>");
- break;
- case 4:
- die("<div align=\"center\">文件不有被上传<a href=\"java script:history.back();\">点击返回</a></div>");
- break;
- }
-
-
- $exname=strrchr($upfile,".");
-
- if(!in_array($exname,$exit_name))
- die("<div align=\"center\">文件扩展名不对。只允许上传jpg gif png的图片<a href=\"java script:history.back();\">点击返回</a></div>");
-
-
- if (!file_exists($bigfolder)){
- mkdir($bigfolder,0777);
- mkdir($bigfolder."/".$fdate,0777);
- }else{
- if (!file_exists($bigfolder."/".$fdate)){
- mkdir($bigfolder."/".$fdate,0777);
- }
- }
-
- if (!file_exists($smallfolder)){
- mkdir($smallfolder,0777);
- mkdir($smallfolder."/".$fdate,0777);
- }else{
- if (!file_exists($smallfolder."/".$fdate)){
- mkdir($smallfolder."/".$fdate,0777);
- }
- }
-
-
- $upfiletmp=$_FILES['image']['tmp_name'];
-
- if ($uperror==6)
- die("<div align=\"center\">找不到临时上传文件,上传失败<a href=\"java script:history.back();\">点击返回</a></div>");
-
-
-
-
-
-
-
-
-
-
-
-
- function imageWaterMark($groundImage,$waterPos=0,$waterImage="",$waterText="", $textFont=5,$textColor="#FF0000")
- {
- $isWaterImage = FALSE;
- $formatMsg = "暂不支持该文件格式,请用图片处理软件将图片转换为GIF、JPG、PNG格式。";
-
- if(!emptyempty($waterImage) && file_exists($waterImage)) {
- $isWaterImage = TRUE;
- $water_info = getimagesize($waterImage);
- $water_w = $water_info[0];
- $water_h = $water_info[1];
- switch($water_info[2]) {
- case 1:$water_im = imagecreatefromgif($waterImage);break;
- case 2:$water_im = imagecreatefromjpeg($waterImage);break;
- case 3:$water_im = imagecreatefrompng($waterImage);break;
- default:die($formatMsg);
- }
- }
-
- if(!emptyempty($groundImage) && file_exists($groundImage)) {
- $ground_info = getimagesize($groundImage);
- $ground_w = $ground_info[0];
- $ground_h = $ground_info[1];
- switch($ground_info[2]) {
- case 1:$ground_im = imagecreatefromgif($groundImage);break;
- case 2:$ground_im = imagecreatefromjpeg($groundImage);break;
- case 3:$ground_im = imagecreatefrompng($groundImage);break;
- default:die($formatMsg);
- }
- } else {
- die("需要加水印的图片不存在!");
- }
-
- if($isWaterImage) {
- $w = $water_w;
- $h = $water_h;
- $label = "图片的";
- } else {
- $temp = imagettfbbox(ceil($textFont*2.5),0,"./cour.ttf",$waterText);
- $w = $temp[2] - $temp[6];
- $h = $temp[3] - $temp[7];
- unset($temp);
- $label = "文字区域";
- }
- if( ($ground_w<$w) || ($ground_h<$h) ) {
- echo "需要加水印的图片的长度或宽度比水印".$label."还小,无法生成水印!";
- return;
- }
- switch($waterPos) {
- case 0:
- $posX = rand(0,($ground_w - $w));
- $posY = rand(0,($ground_h - $h));
- break;
- case 1:
- $posX = 0;
- $posY = 0;
- break;
- case 2:
- $posX = ($ground_w - $w) / 2;
- $posY = 0;
- break;
- case 3:
- $posX = $ground_w - $w;
- $posY = 0;
- break;
- case 4:
- $posX = 0;
- $posY = ($ground_h - $h) / 2;
- break;
- case 5:
- $posX = ($ground_w - $w) / 2;
- $posY = ($ground_h - $h) / 2;
- break;
- case 6:
- $posX = $ground_w - $w;
- $posY = ($ground_h - $h) / 2;
- break;
- case 7:
- $posX = 0;
- $posY = $ground_h - $h;
- break;
- case 8:
- $posX = ($ground_w - $w) / 2;
- $posY = $ground_h - $h;
- break;
- case 9:
- $posX = $ground_w - $w;
- $posY = $ground_h - $h;
- break;
- default:
- $posX = rand(0,($ground_w - $w));
- $posY = rand(0,($ground_h - $h));
- break;
- }
-
- imagealphablending($ground_im, true);
- if($isWaterImage) {
- imagecopy($ground_im, $water_im, $posX, $posY, 0, 0, $water_w,$water_h);
- } else {
- if( !emptyempty($textColor) && (strlen($textColor)==7) ) {
- $R = hexdec(substr($textColor,1,2));
- $G = hexdec(substr($textColor,3,2));
- $B = hexdec(substr($textColor,5));
- } else {
- die("水印文字颜色格式不正确!");
- }
- imagestring ( $ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate($ground_im, $R, $G, $B));
- }
-
- @unlink($groundImage);
- switch($ground_info[2]) {
- case 1:imagegif($ground_im,$groundImage);break;
- case 2:imagejpeg($ground_im,$groundImage);break;
- case 3:imagepng($ground_im,$groundImage);break;
- default:die($errorMsg);
- }
-
- if(isset($water_info)) unset($water_info);
- if(isset($water_im)) imagedestroy($water_im);
- unset($ground_info);
- imagedestroy($ground_im);
- }
-
-
-
- $goodupfile=@move_uploaded_file($upfiletmp,$bigaddrname.$exname);
- if (!$goodupfile){
- die("<div align=\"center\">上传图片失败<a href=\"java script:history.back();\">点击返回</a></div>");
- }else{
-
-
- imageWaterMark($bigaddrname.$exname,5,"",$fontname,5,"#FF0000");
-
-
-
-
-
-
- function ResizeImage($im,$maxwidth,$maxheight,$name){
- $width = imagesx($im);
- $height = imagesy($im);
- if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
- if($maxwidth && $width > $maxwidth){
- $widthratio = $maxwidth/$width;
- $RESIZEWIDTH=true;
- }
- if($maxheight && $height > $maxheight){
- $heightratio = $maxheight/$height;
- $RESIZEHEIGHT=true;
- }
- if($RESIZEWIDTH && $RESIZEHEIGHT){
- if($widthratio < $heightratio){
- $ratio = $widthratio;
- }else{
- $ratio = $heightratio;
- }
- }elseif($RESIZEWIDTH){
- $ratio = $widthratio;
- }elseif($RESIZEHEIGHT){
- $ratio = $heightratio;
- }
- $newwidth = $width * $ratio;
- $newheight = $height * $ratio;
- if(function_exists("imagecopyresampled")){
- $newim = imagecreatetruecolor($newwidth, $newheight);
- imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
- }else{
- $newim = imagecreate($newwidth, $newheight);
- imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
- }
- ImageJpeg ($newim,$smalladdrname.$name.".jpg");
- ImageDestroy ($newim);
- }else{
- ImageJpeg ($im,$smalladdrname.$name.".jpg");
- }
- }
-
- if($_FILES['image']['size']){
- if($_FILES['image']['type'] == "image/pjpeg"){
- $im = imagecreatefromjpeg($bigaddrname.$exname);
- }elseif($_FILES['image']['type'] == "image/x-png"){
- $im = imagecreatefrompng($bigaddrname.$exname);
- }elseif($_FILES['image']['type'] == "image/gif"){
- $im = imagecreatefromgif($bigaddrname.$exname);
- }
- if($im){
- if(file_exists($smalladdrname.".jpg")){
- unlink($smalladdrname.".jpg");
- }
- ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,$smalladdrname);
- ImageDestroy ($im);
- }
- }
- echo "<div align='center'><a href='java script:window.history.back()'>上传成功</a></div>";
- }
-
-
- }
- ?>
-
- <form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
- <input type="file" name="image" />
- <input type="hidden" name="MAX_FILE_SIZE" value="<?=1024*100?>">
- <input type="submit" name="Submit" value="上传图片" />
- </form>
|
|
|
|