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

查看:203
本文介绍了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 im not really familiar with those functions.

出于某种原因,它不起作用。

for some reason, it doesn't work.

当我要去文件夹时product_images或product_images / thumbs我找不到任何上传的图片。

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 在该代码块之前被声明,该代码块完全正常(测试它)。如果您仍然想要一瞥,请随时询问代码。

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 it). 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天全站免登陆