简体   繁体   English

PHP上传和裁剪图像

[英]PHP Upload & Crop Image

I'm trying to create a php cropper based on other online tutorials but I keep coming up with errors and I can't understand what the actual errors mean. 我正在尝试根据其他在线教程创建php裁剪器,但我不断提出错误,我无法理解实际错误的含义。 Here is my php code with the errors written in too: 这也是我的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();
}
?>

Please help me with this, I've been messing with Avatar uploads for 3 days solid now and I want to get this done, even if I have to use a different php script 请帮我解决这个问题,我已经搞砸了3天的Avatar上传,即使我必须使用其他php脚本,我也希望完成此操作

For your first error you're passing $newimage to imagecreatefromjpeg() . 对于您的第一个错误,您要将$ newimage传递给imagecreatefromjpeg() During this process $newimage does not have a value that imagecreatefromjpeg() can understand. 在此过程中, $ newimage没有imagecreatefromjpeg()可以理解的值。 So basically if $newimage is a file or directory, it's not valid so your problem is the value assigned to $newimage .(Make sure "../uploads/avatars/1380641918-4496.jpg" is valid) Not sure what to tell you on the second warning. 因此,基本上,如果$ newimage是文件或目录,那么它是无效的,因此您的问题是分配给$ newimage的值。(请确保“ ../uploads/avatars/1380641918-4496.jpg”有效)不确定该说什么在第二次警告您。 Good Luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM