PHP 向图片添加文字并保存

PHP Add text to image and save

我正在尝试让 PHP 文件为上传的图像添加 ID 并重新保存图像。出于某种原因,下面的代码无法正常工作,尽管它看起来与我在网上找到的其他示例非常相似。我究竟做错了什么?

    $productStyle = isset($_POST['productStyle']) ? encodechars($_POST['productStyle']) : "";
    $productSize = isset($_POST['productSize']) ? encodechars($_POST['productSize']) : "";
    $itemID = isset($_POST['itemID']) ? encodechars($_POST['itemID']) : "";

    if ($productStyle!=""){
        $fileLocation ='/home/abcdefg/public_html/uploads/'.$productStyle;
        $photoLoc = $fileLocation . "/" . $itemID.".png";
        if(!is_dir($fileLocation)) {
            mkdir($fileLocation , 0777); //create directory if it doesn't exist
        }

        //add id to image
        $im = imagecreatefrompng($_FILES["file"]["tmp_name"]);
        $font = 'Verdana.ttf';  //<-- this file is included in directory
        $grey = imagecolorallocate($im, 128, 128, 128);
        imagettftext($im, 10, 0, 11, 20, $grey, $font, $itemID);

        imagepng($im, $photoLoc);  //<-- This does not work
        imagedestroy($im);   

        //move_uploaded_file($_FILES["file"]["tmp_name"], $photoLoc);  //<-- This will move the file to the correct folder but without the text added 

    }

先移动原文件,加水印后删除

$productStyle = isset($_POST['productStyle']) ? encodechars($_POST['productStyle']) : "";
$productSize = isset($_POST['productSize']) ? encodechars($_POST['productSize']) : "";
$itemID = isset($_POST['itemID']) ? encodechars($_POST['itemID']) : "";

if ($productStyle!=""){
    $fileLocation ='/home/abcdefg/public_html/uploads/'.$productStyle;
    $photoLoc = $fileLocation . "/" . $_FILES["file"]["name"];
    move_uploaded_file($_FILES["file"]["tmp_name"], $_FILES["file"]["name"]);
    if(!is_dir($fileLocation)) {
        mkdir($fileLocation , 0777); //create directory if it doesn't exist
    }

    //add id to image
    $im = imagecreatefrompng($photoLoc);
    $font = 'Verdana.ttf';  //<-- this file is included in directory
    $grey = imagecolorallocate($im, 128, 128, 128);
    imagettftext($im, 10, 0, 11, 20, $grey, $font, $itemID);

    imagepng($im, $photoLoc);  //<-- This does not work
    imagedestroy($im);   
    unlink($photoLoc);
    //move_uploaded_file($_FILES["file"]["tmp_name"], $photoLoc);  //<-- This will move the file to the correct folder but without the text added 

} 

或者只是改变

$im = imagecreatefrompng($_FILES["file"]["tmp_name"]);

$im = imagecreatefrompng($_FILES["file"]["name"]);