上传图片代码php无法发现错误

时间:2015-01-19 02:14:23

标签: php mysql

我正在尝试在php中实现图片上传。我查看了一些文章,但我在代码中找不到错误。 websitename/upload_avatar.php出现在地址栏中,但图像路径未出现在MySQL表中。这有什么不对?

HTML表单:

  <form action="upload_avatar.php" method="post" enctype="multipart/form-data">
  <input type="FILE" onchange="this.form.submit()"  name="ava" value=""></form>

upload_avatar.php

<?php
     include ("bd.php");
     if (empty($_FILES['ava']['name']))
     {
    $avatar = "avatars/default.jpg"; 
     }
     else
    {
    $path_directory = 'avatars/';
    if(preg_match('/[.](JPG)|(jpg)|(jpeg)|(JPEG)|(gif)|(GIF)|(png)|(PNG)$/',$_FILES['ava']['name']))
    { 
    $filename = $_FILES['ava']['name'];
    $source = $_FILES['ava']['tmp_name'];
    $target = $path_directory . $filename;
    move_uploaded_file($source, $target);

     if(preg_match('/[.](GIF)|(gif)$/', $filename)) {
     $im = imagecreatefromgif($path_directory.$filename) ;
     }
      if(preg_match('/[.](PNG)|(png)$/', $filename)) {
      $im = imagecreatefrompng($path_directory.$filename) ;
      }  

        if(preg_match('/[.](JPG)|(jpg)|(jpeg)|(JPEG)$/', $filename)) {

        $im = imagecreatefromjpeg($path_directory.$filename);
     }
    $w = 150;
    $w_src = imagesx($im); 
    $h_src = imagesy($im); 
    $dest = imagecreatetruecolor($w,$w);
    if ($w_src>$h_src)

             imagecopyresampled($dest, $im, 0, 0,

             round((max($w_src,$h_src)-min($w_src,$h_src))/2),

             0, $w, $w, min($w_src,$h_src), min($w_src,$h_src));


    if ($w_src < $h_src)

             imagecopyresampled($dest, $im, 0, 0, 0, 0, $w, $w,

             min($w_src,$h_src), min($w_src,$h_src));
             if ($w_src==$h_src)

             imagecopyresampled($dest, $im, 0, 0, 0, 0, $w, $w, $w_src, $w_src);





    $date=time();



    imagejpeg($dest, $path_directory.$date.".jpg");



    $avatar = $path_directory.$date.".jpg";



    $delfull = $path_directory.$filename;

    unlink ($delfull);


    $result = mysql_query ("INSERT INTO users (avatar) VALUES('$avatar')");

    }

    else

    {





    exit ("Image must be <strong>JPG,GIF or PNG</strong>");

     }


    }


    ?>

1 个答案:

答案 0 :(得分:0)

这是一个非常强大的例程,可以创建2个图像预览和大。

它还会检查3种图像类型的最小输出。我不关心图像的格式,因为我将它作为base64放在HTML页面中。

它还将图像转换为base64并将它们保存在SQL表中,并将base64图像保存到HTML页面。

在进行缩放时,会显示照片以供审阅。

您似乎对此有足够的了解以理解我的代码。

我可以发表评论。

$thumbWidth = 60;
$thumbHeight = 60;  
$previewWidth = 200;
$previewHeight = 150;
$bigWidth = 800;
$bigHeight = 600;


if( is_uploaded_file($_FILES['ava']['tmp_name']) || !($_FILES['ava']['error'] !== UPLOAD_ERR_OK)){
  $save = false;
  $jspan = '';
  $gspan = '';
  $pspan = '';

按MIME类型获取图片类型。

然后使用imagecreatefrom 再次检查

无论扩展名为MIME类型,如果是图片jpg,png或gif,它都会正确识别该类型。

  switch(strtolower($_FILES['ava']['type'])){
  case 'image/jpeg':
    $image = @imagecreatefromjpeg($_FILES['ava']['tmp_name']);
    if ($image !== false){$save = true;break;}
  case 'image/png':
    $image = @imagecreatefrompng($_FILES['ava']['tmp_name']);
    if ($image !== false){$save = true;break;}
  case 'image/gif':
    $image = @imagecreatefromgif($_FILES['ava']['tmp_name']);
    if ($image !== false){$save = true;break;}
  default:
    $img = @getimagesize($_FILES['ava']['tmp_name']);
    switch(strtolower($img['mime'])){
    case 'image/jpeg':
      $image = @imagecreatefromjpeg($_FILES['ava']['tmp_name']);
      if ($image !== false){$save = true;break;}
    case 'image/png':
      $image = @imagecreatefrompng($_FILES['ava']['tmp_name']);
      if ($image !== false){$save = true;break;}
    case 'image/gif':
      $image = @imagecreatefromgif($_FILES['ava']['tmp_name']);
      if ($image !== false){$save = true;break;}
    default:
      $filename = $_FILES['ava']['name'];
      $ext = substr($filename,-3);
      switch(strtolower($ext)){
      case 'jpg':
        $image = @imagecreatefromjpeg($_FILES['ava']['tmp_name']);
        if ($image !== false){$save = true;break;}
      case 'gif':
        $image = @imagecreatefromgif($_FILES['ava']['tmp_name']);
        if ($image !== false){$save = true;break;}
      case 'png':
        $image = @imagecreatefrompng($_FILES['ava']['tmp_name']);
        if ($image !== false){$save = true;break;}
      default:
        $image = @imagecreatefromjpeg($_FILES['ava']['tmp_name']);
        if ($image !== false){$save = true;break;}
        $image = @imagecreatefrompng($_FILES['ava']['tmp_name']);
        if ($image !== false){$save = true;break;}
        $image = @imagecreatefromgif($_FILES['ava']['tmp_name']);
        if ($image !== false){$save = true;break;}
      }
    }
  }
}

缩放到预览尺寸

 if ($save === true){
  $originalWidth  = imagesx($image);
  $originalHeight = imagesy($image);
  $scale      = min($previewWidth/$originalWidth, $previewHeight/$originalHeight);
  $newWidth  = ceil($scale*$originalWidth);
  $newHeight = ceil($scale*$originalHeight);
  $newPic = imagecreatetruecolor($newWidth, $newHeight);
  imagecopyresampled($newPic, $image,0, 0, 0, 0,$newWidth, $newHeight, $originalWidth, $originalHeight);
  ob_start();
  imagejpeg($newPic, NULL, 70);
  $jpg = ob_get_clean();
  ob_clean();
  ob_end_flush();
  $jLen = strlen($jpg);
  ob_start();
  imagepng($newPic, NULL, 9);
  $png = ob_get_clean();
  ob_clean();
  ob_end_flush();
  $pLen = strlen($png);
  ob_start();
  imagegif($newPic);
  $gif = ob_get_clean();
  ob_clean();
  ob_end_flush();

检查哪种图像类型创建的字节数最小

  $gLen = strlen($gif);
  if ($gLen < $jLen){
    if ($pLen < $gLen){
      $raw = $png;
      $png = null;
      $type = 'png';
      $pspan = '<span class="size">';
    }
    else{
      $raw = $gif;
      $gif = null;
      $type = 'gif';
      $gspan = '<span class="size">';
    }
  }
  elseif($pLen < $jLen){
    $raw = $png;
    $png = null;
    $type = 'png';
    $pspan = '<span class="size">';
  }
  else{
    $raw = $jpg;
    $jpg = null;
    $type = 'jpg';
    $jspan = '<span class="size">';
  }

转换为bas64并保存

我在HTML页面中使用base64图像。它消除了每个图像的HTTP往返。

反对者抱怨base64增加了30%的图像大小。确实如此。

当base64嵌入HTML时,它会被zgip压缩。额外的30%被gzip压缩得更少,典型性只有几个百分点。

HTTP往返行程因服务器,时间等而异,但通常为200毫秒。 base64的传输时间通常只有几毫秒。

  $base64 = base64_encode($raw);
  $thumb1 = "<img  width=\"$newWidth\" height=\"$newHeight\" src=\"data:image/$type;base64,$base64\"  alt =\"profile thumb one\"/>";
  $size = 'Thumb';
  echo  "<p>Image $num $size <br>$jspan jpg: $jLen bytes</span>, <br>$gspan gif: $gLen bytes</span>, <br>$pspan png: $pLen bytes</span></p> $thumb1"; 
  $filename = 'thumb1.' . $type;
  $fp = fopen($filename  ,"w");
  fwrite($fp, $raw);
  fclose($fp);
  imagedestroy($newPic);
  $filename = "thumb1_$type.html";
  $fp = fopen($filename  ,"w");
  fwrite($fp, $thumb1);
  fclose($fp);
  $thumb1 = mysql_escape_string($thumb1);
  @mysql_unbuffered_query("UPDATE `photos` SET `thumb1`='$thumb1' WHERE `id`=$id");

缩放为大图:

  $scale      = min($bigWidth/$originalWidth, $bigHeight/$originalHeight);
  $newWidth  = ceil($scale*$originalWidth);
  $newHeight = ceil($scale*$originalHeight);
  $newPic = imagecreatetruecolor($newWidth, $newHeight);
  imagecopyresampled($newPic, $image,0, 0, 0, 0,$newWidth, $newHeight, $originalWidth, $originalHeight);
  ob_start();
  imagejpeg($newPic, NULL, 70);
  $jpg = ob_get_clean();
  ob_clean();
  ob_end_flush();
  $jLen = strlen($jpg);
  ob_start();
  imagepng($newPic, NULL, 9);
  $png = ob_get_clean();
  ob_clean();
  ob_end_flush();
  $pLen = strlen($png);
  ob_start();
  imagegif($newPic);
  $gif = ob_get_clean();
  ob_clean();
  ob_end_flush();
  $gLen = strlen($gif);
  if ($gLen < $jLen){
    if ($pLen < $gLen){
      $raw = $png;
      $png = null;
      $type = 'png';
      $pspan = '<span class="size">';
    }
    else{
      $raw = $gif;
      $gif = null;
      $type = 'gif';
      $gspan = '<span class="size">';
    }
  }
  elseif($pLen < $jLen){
    $raw = $png;
    $png = null;
    $type = 'png';
      $pspan = '<span class="size">';
  }
  else{
    $raw = $jpg;
    $jpg = null;
    $type = 'jpg';
    $jspan = '<span class="size">';
  }
  $base64 = base64_encode($raw);
  $size = 'Full Size';
  $filename = 'big1.' . $type;
  $big1 = "<img  width=\"$newWidth\" height=\"$newHeight\" src=\"data:image/$type;base64,$base64\" alt =\"profile photo one\"/>";
  echo  "<p>Image $num $size <br>$jspan jpg: $jLen bytes</span>, <br>$gspan gif: $gLen bytes</span>, <br>$pspan png: $pLen bytes</span></p> $big1"; 
  $fp = fopen($filename  ,"w");
  fwrite($fp, $raw);
  fclose($fp);
  imagedestroy($newPic);
  $filename = "big1_$type.html";
  $fp = fopen($filename  ,"w");
  fwrite($fp, $big1);
  fclose($fp);
  $big1 = mysql_escape_string($big1);
  @mysql_unbuffered_query("UPDATE `photos` SET `big1`='$big1' WHERE `id`=$id");