PHP上传&裁剪图像

时间:2013-10-01 15:47:20

标签: php image file-upload upload crop

我正在尝试基于其他在线教程创建一个php cropper,但我不断出现错误,我无法理解实际错误的含义。这是我的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脚本

1 个答案:

答案 0 :(得分:0)

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