【问题标题】:Upload image with resize in php在php中上传调整大小的图像
【发布时间】:2013-12-07 14:57:40
【问题描述】:

我在 php 中上传图片时遇到问题

我想上传,将图片大小调整为 width=600px

例如,当我上传宽度为 2000 像素的图片时,它应该上传宽度为 600 像素的图片

当然还有更小的磁盘空间...

php文件是:

<?php
require_once("db.php");
$name = trim($_POST['name']);
$addr = trim($_POST['addr']);
$dist = trim($_POST['dist']);
$city = trim($_POST['city']);
$phone = trim($_POST['phone']);
$price = trim($_POST['price']);
$lati = trim($_POST['lati']);
$long = trim($_POST['long']);
$tid = trim($_POST['type']);
$img = "";
if($_FILES)
{
    //var_dump($_FILES);
    $random_str = md5(uniqid(mt_rand(), true));
    $f_name = "tmp/".$random_str.".jpg";
    move_uploaded_file($_FILES['placeimg']['tmp_name'], $f_name);
    $img = 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])."/".$f_name;


    $uploadedfile = $_FILES['placeimg']['tmp_name'];

    $size=filesize($_FILES['placeimg']['tmp_name']);

    $uploadedfile =$_FILES['placeimg']['tmp_name'];
    $src = imagecreatefromjpeg($uploadedfile);

list($width,$height)=getimagesize($_FILES['placeimg']['tmp_name']);

$newwidth=600;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);

//$filename = "tmp/". $_FILES['file']['name'];
imagejpeg($tmp,$uploadedfile,75);


imagedestroy($src);

    }

$sql = "INSERT INTO `Place`(`PName`, `PAddr`, `PDistrict`, `PCity`, `PImage`, `PPhone`, `PPrice`, `PLat`, `PLong`, `TID`, `PStatus`) VALUES ('{$name}', '{$addr}', '{$dist}', '{$city}', '{$img}', '{$phone}', '{$price}', '{$lati}', '{$long}', '{$tid}', '0')";
$status = 0;
$mess = "Err!";
if(mysql_query($sql))
{
    $status = 1;
    $mess = "Successful! Waiting approve";
}
$json['status'] = $status;
$json['message'] = $mess;
echo json_encode($json);
?>

结果是没有调整大小的大图像

你能帮忙吗?

【问题讨论】:

    标签: php image upload resize


    【解决方案1】:

    试试这个:

    function upload_resize($file,$newwidth,$resolution,$location,$prefix){
    
    define ("MAX_SIZE","2048");
    
    error_reporting(0); 
    $image =$file["name"];
    $uploadedfile = $file['tmp_name']; 
    if ($image) {
        $filename = stripslashes($file['name']);
        $extension = getExtension($filename);
        $extension = strtolower($extension);
    
        if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) {
            //Unknown Image extension
            return 1;
        }
        else
        {
            $size=filesize($file['tmp_name']);
            if ($size > MAX_SIZE*1024){
                //You have exceeded the size limit
                return 2;
            }
    
            if($extension=="jpg" || $extension=="jpeg" ) {
                $uploadedfile = $file['tmp_name'];
                $src = imagecreatefromjpeg($uploadedfile);
            }
            else if($extension=="png") {
                $uploadedfile = $file['tmp_name'];
                $src = imagecreatefrompng($uploadedfile);
            }
            else {
                $src = imagecreatefromgif($uploadedfile);
            }
            list($width,$height)=getimagesize($uploadedfile);
            $newheight=($height/$width)*$newwidth;
            $tmp=imagecreatetruecolor($newwidth,$newheight);
            imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
            $filename = $location.$prefix.$file['name'];
    
            imagejpeg($tmp,$filename,$resolution);
            imagedestroy($src);
            imagedestroy($tmp);
        }
    }
    return 0;
    }
    function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
    }
    
    // get file from user and send to this function:
    $res=upload_resize($_FILES["image"],600,100,'../images','');
    switch ($res){
        case 0 : echo 'images saved';  break;
        case 1 : echo 'Unknown file type'; break;
        case 2 : echo 'file size error';  break;
    }
    

    我使用这个功能来上传和调整图片大小。

    【讨论】:

    • 抱歉,它不起作用... 600px 在哪里? sql函数在哪里?!!
    • 我更新了帖子。但您必须将此功能仅用于上传和调整图像大小。并为您想要的其他内容添加其他代码。
    【解决方案2】:
    // for outputting a jpeg image, Set the content type header - in this case image/jpeg
    
        header('Content-Type: image/jpeg');
    
    // Output the image
    
        imagejpeg($tmp,$uploadedfile,75);
    

    试试这个..

    【讨论】:

      【解决方案3】:

      您创建了一个新的图案文件(使用imagecreatetruecolor),但您没有使用原始图片。

      尝试使用imagecopyresampled 像这样:

      <?php
                  require_once("db.php");
                  $name = trim($_POST['name']);
                  $addr = trim($_POST['addr']);
                  $dist = trim($_POST['dist']);
                  $city = trim($_POST['city']);
                  $phone = trim($_POST['phone']);
                  $price = trim($_POST['price']);
                  $lati = trim($_POST['lati']);
                  $long = trim($_POST['long']);
                  $tid = trim($_POST['type']);
                  $img = "";
                  if($_FILES)
                  {
                      //var_dump($_FILES);
                      $random_str = md5(uniqid(mt_rand(), true));
                      $f_name = "tmp/".$random_str.".jpg";
                      move_uploaded_file($_FILES['placeimg']['tmp_name'], $f_name);
                      $img = 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])."/".$f_name;
      
      
                      $uploadedfile = $_FILES['placeimg']['tmp_name'];
      
                      $size=filesize($_FILES['placeimg']['tmp_name']);
      
                      $uploadedfile =$_FILES['placeimg']['tmp_name'];
                      $src = imagecreatefromjpeg($uploadedfile);
      
                  list($width,$height)=getimagesize($_FILES['placeimg']['tmp_name']);
      
                  $newwidth=600;
                  $newheight=($height/$width)*$newwidth;
                  $tmp=imagecreatetruecolor($newwidth,$newheight);
                  $buff = imagecreatefromjpeg($uploadedfile);
                  imagecopyresampled($tmp, $b, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
      
                  //$filename = "tmp/". $_FILES['file']['name'];
                  imagejpeg($tmp,$uploadedfile,75);
      
      
                  imagedestroy($src);
      
                      }
      
                  $sql = "INSERT INTO `Place`(`PName`, `PAddr`, `PDistrict`, `PCity`, `PImage`, `PPhone`, `PPrice`, `PLat`, `PLong`, `TID`, `PStatus`) VALUES ('{$name}', '{$addr}', '{$dist}', '{$city}', '{$img}', '{$phone}', '{$price}', '{$lati}', '{$long}', '{$tid}', '0')";
                  $status = 0;
                  $mess = "Err!";
                  if(mysql_query($sql))
                  {
                      $status = 1;
                      $mess = "Successful! Waiting approve";
                  }
                  $json['status'] = $status;
                  $json['message'] = $mess;
                  echo json_encode($json);
                  ?>
      

      【讨论】:

        猜你喜欢
        • 2014-03-14
        • 2013-09-19
        • 2011-01-10
        • 2011-04-16
        • 2015-12-05
        • 2020-04-12
        • 2023-03-18
        • 2023-04-11
        相关资源
        最近更新 更多