//因为太长,这是接上面的!!
$new_width = 80;
$new_height = 100;if (mysql_num_rows($result) == 1)
{
  $row = mysql_fetch_array($result);
  $oldfoto = $row["fotopath"];
  unlink($oldfoto);
  $oldfoto = $row["thumbpath"];
  unlink($oldfoto);
  move_uploaded_file($_FILES['userphoto']['tmp_name'], $newname);
chmod($newname, 0644);
  list($width_orig, $height_orig) = getimagesize($newname);
  if ($width_orig > $new_width || $height_orig > $new_height)
  {
    if ($ext == ".jpg" || $ext == ".jpeg" || $ext == ".JPG" || $ext == ".JPEG")
    {
      $srcimg = ImageCreateFromJPEG($newname);
      if ($new_width && ($width_orig < $height_orig))
        $new_width = ($new_height / $height_orig) * $width_orig;
      else
        $new_height = ($new_width / $width_orig) * $height_orig; 
      // Resample
      $dest_img = imagecreatetruecolor($new_width, $new_height);
      imageCopyResampleBicubic($dest_img, $srcimg, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig); 
      // Output
      ImageJPEG($dest_img, $thumbname, 100);
    }elseif ($ext == ".png" || $ext == ".PNG")
    {
      $srcimg = ImageCreateFromPNG($newname);
      if ($new_width && ($width_orig < $height_orig))
        $new_width = ($new_height / $height_orig) * $width_orig;
      else
        $new_height = ($new_width / $width_orig) * $height_orig; 
      // Resample
      $dest_img = imagecreatetruecolor($new_width, $new_height);
      imageCopyResampleBicubic($dest_img, $srcimg, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig); 
      // Output
      ImagePNG($dest_img, $thumbname);
    }elseif ($ext == ".gif" || $ext == ".GIF")
    {
      $srcimg = ImageCreateFromGIF($newname);
      if ($new_width && ($width_orig < $height_orig))
        $new_width = ($new_height / $height_orig) * $width_orig;
      else
        $new_height = ($new_width / $width_orig) * $height_orig; 
      // Resample
      $dest_img = imagecreatetruecolor($new_width, $new_height);
      imageCopyResampleBicubic($dest_img, $srcimg, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig); 
      // Output
      ImageGIF($dest_img, $thumbname);
    } elseif ($ext == ".bmp" || $ext == ".BMP")
  //  {
 //     if ($new_width && ($width_orig < $height_orig))
  //      $new_width = ($new_height / $height_orig) * $width_orig;
  //    else
  //      $new_height = ($new_width / $width_orig) * $height_orig; 
      // Resample
 //     $dest_img = imagecreatetruecolor($new_width, $new_height);
 //     imagecopyresampled($dest_img, $srcimg, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig); 
      // Output
   //  ImageWBMP($dest_img, $thumbname);
  copy ($newname, $thumbname);
  //  }
  }
  else
    copy ($newname, $thumbname);
  $result = mysql_query("update photos set
fotopath = '$newname',
thumbpath = '$thumbname',
description = '$description'
WHERE username='$valid_user' AND fotoid = '$fotoid'");  if (!$result)
    db_error();
}
else
{
  move_uploaded_file($_FILES['userphoto']['tmp_name'], $newname);
chmod($newname, 0644);
  list($width_orig, $height_orig) = getimagesize($newname);
  if ($width_orig > $new_width || $height_orig > $new_height)
  {
    if ($ext == ".jpg" || $ext == ".jpeg" || $ext == ".JPG" || $ext == ".JPEG")
    {
      $srcimg = ImageCreateFromJPEG($newname);
      if ($new_width && ($width_orig < $height_orig))
        $new_width = ($new_height / $height_orig) * $width_orig;
      else
        $new_height = ($new_width / $width_orig) * $height_orig; 
      // Resample
      $dest_img = imagecreatetruecolor($new_width, $new_height);
      imageCopyResampleBicubic($dest_img, $srcimg, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig); 
      // Output
      ImageJPEG($dest_img, $thumbname, 100);

    }elseif ($ext == ".png" || $ext == ".PNG")
    {
      $srcimg = ImageCreateFromPNG($newname);
      if ($new_width && ($width_orig < $height_orig))
        $new_width = ($new_height / $height_orig) * $width_orig;
      else
        $new_height = ($new_width / $width_orig) * $height_orig; 
      // Resample
      $dest_img = imagecreatetruecolor($new_width, $new_height);
      imageCopyResampleBicubic($dest_img, $srcimg, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig); 
      // Output
      ImagePNG($dest_img, $thumbname);

    }elseif ($ext == ".gif" || $ext == ".GIF")
    {
      $srcimg = ImageCreateFromGIF($newname);
      if ($new_width && ($width_orig < $height_orig))
        $new_width = ($new_height / $height_orig) * $width_orig;
      else
        $new_height = ($new_width / $width_orig) * $height_orig; 
      // Resample
      $dest_img = imagecreatetruecolor($new_width, $new_height);
      imageCopyResampleBicubic($dest_img, $srcimg, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig); 
      // Output
      ImageGIF($dest_img, $thumbname);

    }elseif ($ext == ".bmp" || $ext == ".BMP")
    {
  copy ($newname, $thumbname);
    }
  }
  else
    copy ($newname, $thumbname);
  $result = mysql_query("insert into photos values ('','$valid_user','$fotoid', '$newname','$thumbname', '$description')");  if (!$result)
    db_error();
}session_unregister ("fotoerrors");header("Location: profilefoto.php");?>

解决方案 »

  1.   

    我的程序就是想能上传.jpg .jpeg .gif .png这些格式的图片文件,然后把上传的图片格式都转成.jpg的,请问大侠有知道怎么做的吗?
      

  2.   

    ....$img_ext = array('.jpg','.jpeg','.gif','.png'); //允许上传的后缀
    $ext = strtolower(strrchr($_FILES['userphoto']['name'], ".")); //取得上传文件的后缀
    if(! in_array($ext,$img_ext)) //检查是否允许
      die("图片格式错");
    $image = substr(strrchr($_FILES['userphoto']['name'],0,strlen($ext)).'.jpg'; //构造目标文件名
    $im = imagecreatefromstring($_FILES['userphoto']['tmp']); //读取上传文件
    imagejpeg($im,$image); //写到目标文件
      

  3.   

    不好意思写错了一句
    $im = imagecreatefromstring($_FILES['userphoto']['tmp']); //读取上传文件
    改为$imgstream = file_get_contents($_FILES['userphoto']['tmp']); //读取上传文件
    $im = imagecreatefromstring($imgstream); //加载图片
      

  4.   

    一个生成jpg缩略图的函,看明白,你就明白了。
    //生成缩略图
    // 本函数从源文件取出图象,设定成指定大小,并输出到目的文件 
    // 源文件格式:gif,jpg,jpe,jpeg,png 
    // 目的文件格式:jpg 
    // 参数说明:$srcFile:源文件 $dstFile:目标文件 $dstW:目标图象宽度 $dstH:目标图象高度  
    function makethumb($srcFile,$dstFile,$dstW,$dstH) { 
    $data = GetImageSize($srcFile); 
    switch ($data[2]) { 
    case 1: 
    $im = @ImageCreateFromGIF($srcFile); 
    break; 
    case 2: 
    $im = @ImageCreateFromJPEG($srcFile); 
    break; 
    case 3: 
    $im = @ImageCreateFromPNG($srcFile); 
    break; 

    $srcW=ImageSX($im); 
    $srcH=ImageSY($im); 
    $ratio = max($srcW/$dstW,$srcH/$dstH);
    $dstW = ($srcW / $ratio);
    $dstH = ($srcH / $ratio);
     
    $ni=ImageCreateTrueColor($dstW,$dstH); $dst_img = imagecreate($dstW, $dstH);
    imagecopyresized($ni, $im, 0, 0, 0, 0, $dstW, $dstH, $srcW, $srcH);
    ImageJPEG($ni,$dstFile);//生成JPG图片。如果你要把图片直接输出到浏览器, 那么把第二个参数去掉,并用header()函数指定mine类型
    ImageDestroy($im); 
    ImageDestroy($ni); 
    }
      

  5.   

    他那程序是不能嵌在你哪里面的,他是要你体会是如何做的,然后照做而已.@ImageCreateFromGIF($srcFile); 
    这样的函数好象并不是初装就有的吧.我试过,好象我的PHP系统里就没有.