0

我正在尝试根据其他在线教程创建一个 php 裁剪器,但我不断出现错误,我无法理解实际错误的含义。这是我的 php 代码,其中也写有错误:

<?php
include("settings.php");

$extension = end(explode(".", $_FILES["avatar"]["name"]));
$id = mysqli_real_escape_string($con, $_POST["id"]);
$time = time();
$avatarid= time().'-'.mt_rand(1000, 9999);
$avatar = mysqli_real_escape_string($con, $_POST["avatar"]);
$w= mysqli_real_escape_string($con, $_POST["w"]);
$h= mysqli_real_escape_string($con, $_POST["h"]);
$x= mysqli_real_escape_string($con, $_POST["x"]);
$y= mysqli_real_escape_string($con, $_POST["y"]);
$rw = 300;
$rh = 300;

$path = "../uploads/avatars/";
$unlink = "$path$avatar";
$newimage = "$path$avatar";

$insert_avatar_sql = "UPDATE members SET avatar = '".$avatarid.".".$extension."' WHERE id = '$id'";
$insert_avatar_res = mysqli_query($con, $insert_avatar_sql);
if(mysqli_affected_rows($con)>0){
    unlink($unlink);
    move_uploaded_file($_FILES["avatar"]["tmp_name"],"$path" . $avatarid . "." . $extension);

    $wratio = ($rw/$w); 
    $hratio = ($rh/$h); 
    $newW = ceil($w * $wratio);
    $newH = ceil($h * $hratio);
    $newimg = imagecreatetruecolor($newW,$newH);
    $ext=$extension;
    if($ext=="jpg" || $ext=="jpeg" )
    {
        $source = imagecreatefromjpeg($newimage); // Warning: imagecreatefromjpeg(../uploads/avatars/1380641918-4496.jpg) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in C:\AppServ\www\music.co.uk\php\avatar.php on line 34
    }
    else if($ext=="png")
    {
        $source = imagecreatefrompng($newimage);
    }
    else 
    {
        $source = imagecreatefromgif($newimage);
    }
    imagecopyresampled($newimg,$source,0,0,$x1,$y1,$newW,$newH,$w,$h); // Warning: imagecopyresampled(): supplied argument is not a valid Image resource in C:\AppServ\www\music.co.uk\php\avatar.php on line 44
uploads/1380642027-5994
    imagejpeg($newimg,$path.$avatarid,90);
    echo "uploads/".$avatarid;
    exit;

    header("Location: ../edit.php?page=profile");
}
else{
header("Location: ../404.php");
exit();
}
?>

请帮我解决这个问题,我已经搞砸了 3 天的头像上传,我想完成这件事,即使我必须使用不同的 php 脚本

4

1 回答 1

0

对于您的第一个错误,您将$newimage传递给imagecreatefromjpeg()。在此过程中, $newimage没有imagecreatefromjpeg()可以理解的值。所以基本上如果$newimage是一个文件或目录,它是无效的,所以你的问题是分配给$newimage的值。(确保“../uploads/avatars/1380641918-4496.jpg”是有效的)不知道该告诉什么你在第二个警告。祝你好运。

于 2013-10-01T15:57:25.073 回答