我正在尝试在上传时调整图片大小。我有上传代码,并添加了一些我发现的调整大小的代码。 Imagejpeg()
返回false,因此可能存在问题。但我不知道它会是什么。
function processImg($id){
$file_result ="";
if($_FILES['image']['error'] > 0){
$file_result .="No file uploaded or Invalid File";
$file_result .= "Error Code: " . $_FILES['image']['error'] ."<br>";
//header('Location: products.php');
exit();
}
$path = $_SERVER['DOCUMENT_ROOT'] . "/shop/imgs/" .$id."."."jpg";
$tmp = resize(308,173, $path);
/*if(!move_uploaded_file($tmp, $path)){
?> <script> console.log("ahoj") </script> <?php
}*/
return $tmp;
}
function resize($width, $height, $path){
/* Get original image x y*/
list($w, $h) = getimagesize($_FILES['image']['tmp_name']);
/* calculate new image size with ratio */
$ratio = max($width/$w, $height/$h);
$h = ceil($height / $ratio);
$x = ($w - $width / $ratio) / 2;
$w = ceil($width / $ratio);
/* read binary data from image file */
$imgString = file_get_contents($_FILES['image']['tmp_name']);
/* create image from string */
$image = imagecreatefromstring($imgString);
$tmp = imagecreatetruecolor($width, $height);
imagecopyresampled($tmp, $image,
0, 0,
$x, 0,
$width, $height,
$w, $h);
$data = getimagesize($_FILES['image']['tmp_name']);
var_dump($data);
$img = imagejpeg($tmp, $path, 100);
return img;
}
?>
编辑错误消息
Warning: imagejpeg(): open_basedir restriction in effect. File(/var/www/html/shop/imgs/60.jpg) is not within the allowed path(s): (/home/vhosts/predajveci.orgfree.com/:/tmp/:/usr/share/pear/) in /home/vhosts/predajveci.orgfree.com/shop/admin/includes/functions.php on line 47
Warning: imagejpeg(/var/www/html/shop/imgs/60.jpg): failed to open stream: Operation not permitted in /home/vhosts/predajveci.orgfree.com/shop/admin/includes/functions.php on line 47
Notice: Use of undefined constant img - assumed 'img' in /home/vhosts/predajveci.orgfree.com/shop/admin/includes/functions.php on line 49
Strict Standards: Only variables should be passed by reference in /home/vhosts/predajveci.orgfree.com/shop/admin/includes/processproducts.php on line 98