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 

    }
miken32
  • 42,008
  • 16
  • 111
  • 154
Nate23VT
  • 423
  • 8
  • 27
  • Have you checked the error logs? – Jay Blanchard Oct 24 '16 at 14:22
  • 1
    There's no error checking. You're assuming the directory exists and the file is writeable... – Rob W Oct 24 '16 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. – Nate23VT Oct 24 '16 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. – Rob W Oct 24 '16 at 14:47

1 Answers1

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"]);
Divyesh Jesadiya
  • 1,105
  • 4
  • 30
  • 68