2016-10-24 2 views
0

Je tente d'avoir un fichier PHP ajouter un ID à une image téléchargée et réenregistrer l'image. Pour une raison ou une autre, le code ci-dessous ne fonctionne pas, même s'il ressemble beaucoup à d'autres exemples que j'ai trouvés en ligne. Qu'est-ce que je fais mal?PHP Ajouter le texte à l'image et enregistrer

$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 

    } 
+0

Avez-vous vérifié les journaux d'erreurs? –

+1

Il n'y a pas de vérification d'erreur. Vous supposez que le répertoire existe et que le fichier est accessible en écriture ... –

+0

@HalfCrazed La commande move_upload_file fonctionne en pointant vers le même emplacement $ photoLoc, donc j'ai supposé qu'il ne s'agissait pas d'un problème d'autorisation. – Nate23VT

Répondre

1

d'abord déplacer le fichier d'origine et tendre à enlever après le marquage de l'eau.

$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 

} 

ou tout simplement changer

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

à

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