在php中使用resize上传图像

时间:2013-11-22 16:29:34

标签: php image upload resize

我在php中上传图片有问题

我希望通过调整图片大小上传到宽度= 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);
?>

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

你可以帮忙吗?

3 个答案:

答案 0 :(得分:0)

试试这个:

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;
}

我使用此功能上传和调整图像大小。

答案 1 :(得分:0)

// 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);

试试这个..

答案 2 :(得分:0)

您创建了一个新的模式文件(使用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);
            ?>