1

I am attempting to have a PHP file add an ID to an uploaded image and resave the image. For some reason the code below is not working even though it looks very similar to other examples I have found online. What am I doing wrong?

    $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 

    }
4
  • Have you checked the error logs? Commented Oct 24, 2016 at 14:22
  • 1
    There's no error checking. You're assuming the directory exists and the file is writeable... Commented Oct 24, 2016 at 14:22
  • @HalfCrazed The move_upload_file command works which is pointing to the same $photoLoc location so I assumed it was not a permissioning issue. Commented Oct 24, 2016 at 14:30
  • That's kind of dangerous to assume -- lots of variability there! See if you can output the PNG to the browser by omitting the 2nd argument (don't forget to set the header content type first) just to see if the image is being rendered as you expected. Commented Oct 24, 2016 at 14:47

1 Answer 1

2

first, move the original file and then remove after watermarking.

$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 

} 

or just change

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

to

$im = imagecreatefrompng($_FILES["file"]["name"]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.