php上传时调整图片大小

kfi*_*rba 29 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

getExtension函数:

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

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

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

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

注意:您不知道它们被声明的位置的变量如$product_cn在该代码块之前声明,该代码块完全正常(测试).如果你还想看一眼,请随时询问代码.

提前致谢!

zeu*_*stl 40

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

$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 );
}
Run Code Online (Sandbox Code Playgroud)

参考:PHP按最大宽度或重量按比例调整图像大小

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

  • 这是太棒了.虽然它最后缺少一条非常重要的一行:`move_uploaded_file($ _ FILES ['myFile'] ['tmp_name'],$ destinationFilePath);` (3认同)
  • 它有效,但我很困惑 - 为什么你提取两次图像尺寸? (2认同)

小智 25

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

  • 警告:该脚本在GPL下,因此您需要购买商业版本($ 15)或在GPL下打开您的网站资源。 (2认同)

Edw*_*fad 7

//这是我的例子,我曾经自动将每张插入的照片调整为100 x 50像素,图像格式为jpeg希望这也有帮助

if($result){
$maxDimW = 100;
$maxDimH = 50;
list($width, $height, $type, $attr) = getimagesize( $_FILES['photo']['tmp_name'] );
if ( $width > $maxDimW || $height > $maxDimH ) {
    $target_filename = $_FILES['photo']['tmp_name'];
    $fn = $_FILES['photo']['tmp_name'];
    $size = getimagesize( $fn );
    $ratio = $size[0]/$size[1]; // width/height
    if( $ratio > 1) {
        $width = $maxDimW;
        $height = $maxDimH/$ratio;
    } else {
        $width = $maxDimW*$ratio;
        $height = $maxDimH;
    }
    $src = imagecreatefromstring(file_get_contents($fn));
    $dst = imagecreatetruecolor( $width, $height );
    imagecopyresampled($dst, $src, 0, 0, 0, 0, $width, $height, $size[0], $size[1] );

    imagejpeg($dst, $target_filename); // adjust format as needed


}

move_uploaded_file($_FILES['pdf']['tmp_name'],"pdf/".$_FILES['pdf']['name']);
Run Code Online (Sandbox Code Playgroud)

  • 虽然这段代码可能有助于解决问题,但它没有解释_why_和/或_how_它回答了这个问题.提供这种额外的背景将大大提高其长期教育价值.请[编辑]您的答案以添加解释,包括适用的限制和假设. (2认同)