【问题标题】:Image resize on Upload Issue上传问题上的图像大小调整
【发布时间】:2014-01-09 12:29:06
【问题描述】:

在上传时调整图像大小时遇到​​问题。

功能

// Image Resizer
function MakeThumbnail($inputFile, $filepath, $ext, $maxWidth, $maxHeight) {
    if($ext=="jpg" || $ext=="jpeg" ){
        $src = imagecreatefromjpeg($inputFile);
    }else if($ext=="png"){
        $src = imagecreatefrompng($inputFile);
    }else {
        $src = imagecreatefromgif($inputFile);
    }
    list($width, $height) = getimagesize($inputFile);
    $newwidth = $maxWidth;
    $newheight = (strlen($maxHeight)>0) ? $maxHeight : ($height / $width) * $newwidth;
    $tmp = imagecreatetruecolor($newwidth, $newheight);
    imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    imagejpeg($tmp, $filepath, 100); //<- This is line 300
    chmod($filepath, 0777);
    imagedestroy($src);
    imagedestroy($tmp);
}

错误

<b>Warning</b>:  imagejpeg(): Unable to open '/assets/images/reviews/1466108_263109843838629_409857768_n.jpg' for writing: No such file or directory in <b>/MYPATH/includes.php</b> on line <b>300</b><br />

目录已被 chmod'd -R 更改为 0777

用法

$pic2Path = '/assets/images/reviews/' . $_FILES['TheImage'];
MakeThumbnail($_FILES['TheImage']['tmp_name'], $pic2Path, 'jpg', 800, 600);

【问题讨论】:

  • $_FILES['TheImage'] 将是一个数组,并且您将生成 /assets/images/reviews/Array 作为您的文件路径。大概您打算使用$_FILES['TheImage']['name'],这可能会在您的服务器上打开一个非常危险的安全漏洞

标签: php image image-processing


【解决方案1】:

你使用了无效的路径,

$pic2Path = '../assets/images/reviews/' . $_FILES['TheImage'];

或

 $pic2Path = 'assets/images/reviews/' . $_FILES['TheImage'];

【讨论】:

    【解决方案2】:

    我怀疑您的/assets 目录是否位于文件系统的根目录。它可能位于网络服务器的根目录中,因此您可以使用以下内容:

    $pic2Path = $_SERVER['DOCUMENT_ROOT'] . '/assets/images/reviews/' . $_FILES['TheImage'];
    

    【讨论】:

    • 天哪!什么工具!菜鸟!!!!我得再等10分钟才能接受。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多