您好,欢迎访问一九零五行业门户网

支持文件上传兼容性好图片缩略图程序_PHP教程

下面提供三款生成缩代码,兼容性都相当的不错,也可以自定高度与宽度哦。
function imageresize($srcfile,$tow,$toh,$tofile=)
{
if($tofile==){ $tofile = $srcfile; }
$info = ;
$data = getimagesize($srcfile,$info);
switch ($data[2])
{
case 1:
if(!function_exists(imagecreatefromgif)){
echo 你的gd库不能使用gif格式的图片,请使用jpeg或png格式!返回;
exit();
}
$im = imagecreatefromgif($srcfile);
break;
case 2:
if(!function_exists(imagecreatefromjpeg)){
echo 你的gd库不能使用jpeg格式的图片,请使用其它格式的图片!返回;
exit();
}
$im = imagecreatefromjpeg($srcfile);
break;
case 3:
$im = imagecreatefrompng($srcfile);
break;
}
$srcw=imagesx($im);
$srch=imagesy($im);
$towh=$tow/$toh;
$srcwh=$srcw/$srch;
if($towh$ftow=$tow;
$ftoh=$ftow*($srch/$srcw);
}
else{
$ftoh=$toh;
$ftow=$ftoh*($srcw/$srch);
}
if($srcw>$tow||$srch>$toh)
{
if(function_exists(imagecreatetruecolor)){
@$ni = imagecreatetruecolor($ftow,$ftoh);
if($ni) imagecopyresampled($ni,$im,0,0,0,0,$ftow,$ftoh,$srcw,$srch);
else{
$ni=imagecreate($ftow,$ftoh);
imagecopyresized($ni,$im,0,0,0,0,$ftow,$ftoh,$srcw,$srch);
}
}else{
$ni=imagecreate($ftow,$ftoh);
imagecopyresized($ni,$im,0,0,0,0,$ftow,$ftoh,$srcw,$srch);
}
if(function_exists('imagejpeg')) imagejpeg($ni,$tofile);
else imagepng($ni,$tofile);
imagedestroy($ni);
}
imagedestroy($im);
} 实例代码二
$srch*$dstw)
{
$fdsth = round($srch*$dstw/$srcw);
$dsty = floor(($dsth-$fdsth)/2);
$fdstw = $dstw;
}
else
{
$fdstw = round($srcw*$dsth/$srch);
$dstx = floor(($dstw-$fdstw)/2);
$fdsth = $dsth;
}
$ni=imagecreatetruecolor($dstw,$dsth);
$dstx=($dstx$dsty=($dstx$dstx=($dstx>($dstw/2))?floor($dstw/2):$dstx;
$dsty=($dsty>($dsth/2))?floor($dsth/s):$dsty;
$white = imagecolorallocate($ni,255,255,255);
$black = imagecolorallocate($ni,0,0,0);
imagefilledrectangle($ni,0,0,$dstw,$dsth,$white);// 填充背景色
imagecopyresized($ni,$im,$dstx,$dsty,0,0,$fdstw,$fdsth,$srcw,$srch);
if($markwords!=null)
{
$markwords=iconv(gb2312,utf-8,$markwords);
//转换文字编码
imagettftext($ni,20,30,450,560,$black,simhei.ttf,$markwords); //写入文字水印
//参数依次为,文字大小|偏转度|横坐标|纵坐标|文字颜色|文字类型|文字内容
}
elseif($markimage!=null)
{
$wimage_data = getimagesize($markimage);
switch($wimage_data[2])
{
case 1:
$wimage=@imagecreatefromgif($markimage);
break;
case 2:
$wimage=@imagecreatefromjpeg($markimage);
break;
case 3:
$wimage=@imagecreatefrompng($markimage);
break;
}
imagecopy($ni,$wimage,500,560,0,0,88,31); //写入图片水印,水印图片大小默认为88*31
imagedestroy($wimage);
}
imagejpeg($ni,$dstfile,$rate);
imagejpeg($ni,$srcfile,$rate);
imagedestroy($im);
imagedestroy($ni);
}
?>
支持图片上传代码
$maxwidth) || ($height > $maxheight)){
   $widthratio = $maxwidth/$width;  
   $heightratio = $maxheight/$height; 
   if($widthratio     $ratio = $widthratio;
   }else{
    $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,$name . .jpg);
   imagedestroy ($newim);
  }else{
   imagejpeg ($im,$name . .jpg);
  }
 }
 if($_files['image']['size']){
  //echo $_files['image']['type'];
  if($_files['image']['type'] == image/pjpeg||$_files['image']['type'] == image/jpg||$_files['image']['type'] == image/jpeg){
   $im = imagecreatefromjpeg($_files['image']['tmp_name']);
  }elseif($_files['image']['type'] == image/x-png){
   $im = imagecreatefrompng($_files['image']['tmp_name']);
  }elseif($_files['image']['type'] == image/gif){
   $im = imagecreatefromgif($_files['image']['tmp_name']);
  }
  if($im){
   if(file_exists($pic_name.'.jpg')){
    unlink($pic_name.'.jpg');
   }
   resizeimage($im,$pic_width,$pic_height,$pic_name);
   imagedestroy ($im);
  }
 }
?>
>
生成缩略图宽度:
生成缩略图长度:
http://www.bkjia.com/phpjc/632990.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/632990.htmltecharticle下面提供三款生成缩代码,兼容性都相当的不错,也可以自定高度与宽度哦。 function imageresize($srcfile,$tow,$toh,$tofile=) { if($tofile==){ $tofile =...
其它类似信息

推荐信息