您的位置:首页 > 其它

一个可以根据设定输出图片的函数 可以改变图片的大小

2006-02-24 09:54 841 查看
<?php
function UpdateImage($pic='php.jpg',$wrate=2,$hrate=2){
  /*
  先判断图片的类型,决定下面要使用什么函数 如果是gif格式的使用imagecreatefromgif()[从 GIF 文件或 URL 新建一图像];
  在本例中也可以对图片进行比例的缩放
  */ 
  $imagemsg = getimagesize($pic);
  $type_tmp = $imagemsg['mime'];
  $width  = $imagemsg[0];
  $height = $imagemsg[1];
   $type   = substr($type_tmp,strpos($type_tmp,'/')+1);
  if($type == 'jpeg') {
   $fun    = 'ImageCreateFromJPEG';
   $funout =  'imagejpeg';
   $Content_Type = 'Content-Type: image/jpeg';
  } else if($type == 'gif') {
   $fun    = 'imagecreatefromgif';
   $funout   = 'imagegif';
   $Content_Type = 'Content-Type: image/gif';
  }
  $src = $fun($pic);
  $x = $width/$wrate; $y = $height/$hrate;
  $dst = ImageCreateTrueColor($x,$y);
  ImageCopyResampled($dst,$src,0,0,0,0,$x,$y,$width,$height);
  header($Content_Type); 
  $funout($dst);
}
UpdateImage();

/**
*   一段可以保持图片不变形的函数
*   函数名称:   resizePic
*   函数功能:   合理缩放图片
*   输入参数:   $path ----------------- 图片路径
               $maxwidth ------------- 最大宽度
               $maxheight ------------ 最大高度
*   函数返回值: array
*   其它说明:   2004-8-22
*/
function resizePic($path,$maxwidth=120,$maxheight=120)
{
    $info = array();
    $pic_size    = @GetImageSize($path['tmp_name']);
    $imagewidth  = $pic_size['0'];
    $imageheight = $pic_size['1'];
    if ($imagewidth > $maxwidth)
    {
        $imageprop   = ($maxwidth*100)/$imagewidth;
        $imagevsize  = ($imageheight*$imageprop)/100 ;
        $imagewidth  = $maxwidth;
        $imageheight = ceil($imagevsize);
    }
    if ($imageheight > $maxheight)
    {
        $imageprop   = ($maxheight*100)/$imageheight;
        $imagehsize  = ($imagewidth*$imageprop)/100 ;
        $imagewidth  = ceil($imagehsize);
        $imageheight = $maxheight;
    }
    $info['width']  = $imagewidth;
    $info['height'] = $imageheight;
    Return $info;
}

?>

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  dst function fun path header url
相关文章推荐