php在上传时调整图像大小 [英] php resize image on upload

查看:19
本文介绍了php在上传时调整图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了一个表单,用户可以在其中插入一些数据并上传图片.

I got a form where the user is inserting some data and also uploading an image.

为了处理图像,我得到了以下代码:

To deal with the image, I got the following code:

define("MAX_SIZE", "10000");
$errors = 0;
$image = $_FILES["fileField"]["name"];
$uploadedfile = $_FILES['fileField']['tmp_name'];
if($image){
    $filename = stripslashes($_FILES['fileField']['name']);
    $extension = strtolower(getExtension($filename));
    if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")){
        echo ' Unknown Image extension ';
        $errors = 1;
    } else {
        $newname = "$product_cn.$extension";
        $size = filesize($_FILES['fileField']['tmp_name']);
        if ($size > MAX_SIZE*1024){
            echo "You have exceeded the size limit";
            $errors = 1;
        }
        if($extension == "jpg" || $extension == "jpeg" ){
            $uploadedfile = $_FILES['file']['tmp_name'];
            $src = imagecreatefromjpeg($uploadedfile);
        } else if($extension == "png"){
            $uploadedfile = $_FILES['file']['tmp_name'];
            $src = imagecreatefrompng($uploadedfile);
        } else {
            $src = imagecreatefromgif($uploadedfile);
        }
        list($width, $height) = getimagesize($uploadedfile);
        $newwidth = 60;
        $newheight = ($height/$width)*$newwidth;
        $tmp = imagecreatetruecolor($newwidth, $newheight);
        $newwidth1 = 25;
        $newheight1 = ($height/$width)*$newwidth1;
        $tmp1 = imagecreatetruecolor($newwidth1, $newheight1);
        imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
        imagecopyresampled($tmp1, $src, 0, 0, 0, 0, $newwidth1, $newheight1, $width, $height);
        $filename = "../products_images/$newname";
        $filename1 = "../products_images/thumbs/$newname";
        imagejpeg($tmp, $filename, 100); // file name also indicates the folder where to save it to
        imagejpeg($tmp1, $filename1, 100);
        imagedestroy($src);
        imagedestroy($tmp);
        imagedestroy($tmp1);
    }
}

getExtension 函数:

getExtension function:

function getExtension($str) {
    $i = strrpos($str, ".");
    if (!$i) { return ""; }
    $l = strlen($str) - $i;
    $ext = substr($str,$i+1,$l);
    return $ext;
}

我在代码中写了一些符号,因为我对这些函数不是很熟悉.

I've wrote some notation in the code since I'm not really familiar with those functions.

出于某种原因,它不起作用.当我转到文件夹product_images"或product_images/thumbs"时,我找不到任何上传的图片.

For some reason, it doesn't work. When I'm going to the folder "product_images" or "product_images/thumbs", I can't find any image uploaded.

知道我的代码有什么问题吗?应该有 60px 宽的图片和 25px 宽的图片.

Any idea what's wrong with my code? There should be 60px width image, and 25px width image.

注意:您不知道它们在哪里声明的变量,例如 $product_cn 是在该代码块之前声明的,该代码块工作正常(经过测试).如果您还想看一眼,请随时索要代码.

Note: Variables that you don't know where they were declared such as $product_cn were declared before that block of code which works prefectly fine (tested). If you still want a glance at it, feel free to ask for the code.

提前致谢!

推荐答案

您可以在上传时使用此库来处理图像.http://www.verot.net/php_class_upload.htm

You can use this library to manipulate the image while uploading. http://www.verot.net/php_class_upload.htm

这篇关于php在上传时调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆