php上传图片生成等比例缩略图代码 |
时间:2015-01-23 来源:西部数据 作者:西部数据 |
|
一个简单的使用php上传图片文件生然后再生成一张等比例的缩略图效果,有需要学习的朋友可参考参考,实例代码如下:
- <?php
- function _UPLOADPIC($upfile, $maxsize, $updir, $newname = 'date') {
-
- if ($newname == 'date')
-
- $newname = date ( "Ymdhis" );
-
- $name = $upfile ["name"];
-
- $type = $upfile ["type"];
-
- $size = $upfile ["size"];
-
- $tmp_name = $upfile ["tmp_name"];
-
- switch ($type) {
-
- case 'image/pjpeg' :
-
- case 'image/jpeg' :
-
- $extend = ".jpg";
-
- break;
-
- case 'image/gif' :
-
- $extend = ".gif";
-
- break;
-
- case 'image/png' :
-
- $extend = ".png";
-
- break;
-
- }
-
- if (emptyempty ( $extend )) {
-
- echo ( "警告!只能上传图片类型:GIF JPG PNG" );
-
- exit ();
-
- }
-
- if ($size > $maxsize) {
-
- $maxpr = $maxsize / 1000;
-
- echo ( "警告!上传图片大小不能超过" . $maxpr . "K!" );
-
- exit ();
-
- }
-
- if (move_uploaded_file ( $tmp_name, $updir . $newname . $extend )) {
-
- return $updir . $newname . $extend;
-
- }
-
- }
-
-
-
- function show_pic_scal($width, $height, $picpath) {
-
- $imginfo = GetImageSize ( $picpath );
-
- $imgw = $imginfo [0];
-
- $imgh = $imginfo [1];
-
-
-
- $ra = number_format ( ($imgw / $imgh), 1 );
-
- $ra2 = number_format ( ($imgh / $imgw), 1 );
-
-
-
-
-
- if ($imgw > $width or $imgh > $height) {
-
- if ($imgw > $imgh) {
-
- $newWidth = $width;
-
- $newHeight = round ( $newWidth / $ra );
-
-
-
- } elseif ($imgw < $imgh) {
-
- $newHeight = $height;
-
- $newWidth = round ( $newHeight / $ra2 );
-
- } else {
-
- $newWidth = $width;
-
- $newHeight = round ( $newWidth / $ra );
-
- }
-
- } else {
-
- $newHeight = $imgh;
-
- $newWidth = $imgw;
-
- }
-
- $newsize [0] = $newWidth;
-
- $newsize [1] = $newHeight;
-
-
-
- return $newsize;
-
- }
-
-
-
-
-
-
-
- function getImageInfo($src)
-
- {
-
- return getimagesize($src);
-
- }
-
-
-
-
-
-
-
-
-
-
-
- function create($src)
-
- {
-
- $info=getImageInfo($src);
-
- switch ($info[2])
-
- {
-
- case 1:
-
- $im=imagecreatefromgif($src);
-
- break;
-
- case 2:
-
- $im=imagecreatefromjpeg($src);
-
- break;
-
- case 3:
-
- $im=imagecreatefrompng($src);
-
- break;
-
- }
-
- return $im;
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- function resize($src,$w,$h)
-
- {
-
- $temp=pathinfo($src);
-
- $name=$temp["basename"];
-
- $dir=$temp["dirname"];
-
- $extension=$temp["extension"];
-
- $savepath="{$dir}/{$name}";
-
-
-
-
-
- $info=getImageInfo($src);
-
- $width=$info[0];
-
- $height=$info[1];
-
- $per1=round($width/$height,2);
-
- $per2=round($w/$h,2);
-
-
-
-
-
- if($per1>$per2||$per1==$per2)
-
- {
-
-
-
- $per=$w/$width;
-
- }
-
- if($per1<$per2)
-
- {
-
-
-
- $per=$h/$height;
-
- }
-
- $temp_w=intval($width*$per);
-
- $temp_h=intval($height*$per);
-
- $temp_img=imagecreatetruecolor($temp_w,$temp_h);
-
- $im=create($src);
-
- imagecopyresampled($temp_img,$im,0,0,0,0,$temp_w,$temp_h,$width,$height);
-
- if($per1>$per2)
-
- {
-
- imagejpeg($temp_img,$savepath, 100);
-
- imagedestroy($im);
-
- return addBg($savepath,$w,$h,"w");
-
-
-
- }
-
- if($per1==$per2)
-
- {
-
- imagejpeg($temp_img,$savepath, 100);
-
- imagedestroy($im);
-
- return $savepath;
-
-
-
- }
-
- if($per1<$per2)
-
- {
-
- imagejpeg($temp_img,$savepath, 100);
-
- imagedestroy($im);
-
- return addBg($savepath,$w,$h,"h");
-
-
-
- }
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- function addBg($src,$w,$h,$fisrt="w")
-
- {
-
- $bg=imagecreatetruecolor($w,$h);
-
- $white = imagecolorallocate($bg,255,255,255);
-
- imagefill($bg,0,0,$white);
-
-
-
-
-
- $info=getImageInfo($src);
-
- $width=$info[0];
-
- $height=$info[1];
-
- $img=create($src);
-
- if($fisrt=="wh")
-
- {
-
-
-
- return $src;
-
- }
-
- else
-
- {
-
- if($fisrt=="w")
-
- {
-
- $x=0;
-
- $y=($h-$height)/2;
-
- }
-
- if($fisrt=="h")
-
- {
-
- $x=($w-$width)/2;
-
- $y=0;
-
- }
-
- imagecopymerge($bg,$img,$x,$y,0,0,$width,$height,100);
-
- imagejpeg($bg,$src,100);
-
- imagedestroy($bg);
-
- imagedestroy($img);
-
- return $src;
-
- }
- }
- ?>
-
-
-
- $filename=(_UPLOADPIC($_FILES["upload"],$maxsize,$updir,$newname='date'));
- $show_pic_scal=show_pic_scal(230, 230, $filename);
- resize($filename,$show_pic_scal[0],$show_pic_scal[1]);
|
|
|
|