php在上传时调整图像大小

标签 php image

我有一个表单,用户可以在其中插入一些数据并上传一张图片。

为了处理图片,我得到了如下代码:

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

获取扩展函数:

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

我在代码中写了一些符号,因为我不太熟悉这些函数。

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

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

注意:您不知道它们在哪里声明的变量,例如 $product_cn 是在该代码块之前声明的,该代码块运行良好(经过测试)。如果您还想看一眼,请随时索取代码。

提前致谢!

最佳答案

这是另一个不错且简单的解决方案:

$maxDim = 800;
$file_name = $_FILES['myFile']['tmp_name'];
list($width, $height, $type, $attr) = getimagesize( $file_name );
if ( $width > $maxDim || $height > $maxDim ) {
    $target_filename = $file_name;
    $ratio = $width/$height;
    if( $ratio > 1) {
        $new_width = $maxDim;
        $new_height = $maxDim/$ratio;
    } else {
        $new_width = $maxDim*$ratio;
        $new_height = $maxDim;
    }
    $src = imagecreatefromstring( file_get_contents( $file_name ) );
    $dst = imagecreatetruecolor( $new_width, $new_height );
    imagecopyresampled( $dst, $src, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
    imagedestroy( $src );
    imagepng( $dst, $target_filename ); // adjust format as needed
    imagedestroy( $dst );
}

引用:PHP resize image proportionally with max width or weight

编辑:清理并简化了代码。感谢@jan-mirus 的评论。

关于php在上传时调整图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18805497/

相关文章:

javascript - 删除前确认

php - 如果脚本是从控制台或浏览器请求运行的,如何使用 PHP 检查?

php - 如何通过 PHP 创建由点组成的文本图像?

arrays - ReactJS 从数组渲染图像

html - 如何根据背景图像大小自动调整 div 的大小?

php - Codeigniter where 子句不起作用

php - 使用中间sql表不好吗

java - 从 PHP 到 C/C++ & Java 及其他

python - Django 管理员不显示来自 Imagefield 的图像

HTML 输入 + 标签与 JSSOR 画廊的脚本冲突 - 任何修复?