图像在上传问题时调整大小

时间:2013-12-20 15:38:29

标签: php image image-processing

在上传时调整图片大小时出现问题。

功能

// 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&#39; d -R到0777

用法

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

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:0)

您使用了无效路径,

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

OR

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