0

こんにちは、ユーザーに写真のアップロードを求めるフォームがあります。これは、imagecreatefromjpg および imagecopyresampled で写真を 2 番目の画像 (別の画像の上に重ねた画像) として使用します。しかし、アップロードされた画像が正しく取得されていないようです。また、ファイルがアップロードされていない場合、デフォルトの画像はサーバーから渡されません。これらのエラーが発生しています:

Warning</b>:  imagecreatefromjpeg() [<a href='function.imagecreatefromjpeg'>function.imagecreatefromjpeg</a>]: Filename cannot be empty in <b>/home/content/52/12096052/html/Testupdate/processor.php</b> on line <b>117</b>

Warning</b>:  imagesx() expects parameter 1 to be resource, boolean given in <b>/home/content/52/12096052/html/Testupdate/processor.php</b> on line <b>145</b><br />
Warning</b>:  imagesy() expects parameter 1 to be resource, boolean given in /home/content/52/12096052/html/Testupdate/processor.php</b> on line <b>145</b><br />

<b>Warning</b>:  imagecopyresampled() expects parameter 2 to be resource, boolean given in /home/content/52/12096052/html/Testupdate/processor.php</b> on line <b>145</b><br />

<b>Warning</b>:  Cannot modify header information - headers already sent by (output started at /home/content/52/12096052/html/Testupdate/processor.php:117) in <b>/home/content/52/12096052/html/Testupdate/processor.php</b> on line <b>186</b><br />

これは私のコードprocessor.phpです:

    $con = mysql_connect($hostname, $username, $password) OR DIE ("Unable to 
    connect to database! Please try again later.");

    mysql_select_db($dbname);

    // make a note of the current working directory, relative to root.
    $directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);

    // make a note of the directory that will recieve the uploaded files //i made it as the same directory where the .php file is
    $uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self;

    // make a note of the location of the upload form in case we need it
    $uploadForm = 'https://' . $_SERVER['HTTP_HOST'] . $directory_self . 'index.php';

    // name of the fieldname used for the file in the HTML form
    $fieldname = 'file';

    // check the upload form was actually submitted else print form
    if(isset($_POST['submit']))
    {           
        if($_FILES['file']['tmp_name']!="")
        {
            $allowedExts = array("jpeg", "jpg");
            $temp = explode(".", $_FILES['file']['name']);
            $extension = end($temp);

            $notallowedExts = array("png", "gif", "pdf", "doc", "docx", "txt", "html", "xlsx", "mov");
            $nottemp = explode(".", $_FILES['file']['name']);
            $notextension = end($nottemp);

            if ((($_FILES['file']['type'] == "image/jpeg") || ($_FILES['file']['type'] == "image/jpg")) && ($_FILES['file']['size'] < 1300000) && in_array($extension, $allowedExts))  
            {       

              $now = time();
              while(file_exists($uploadFilename = $uploadsDirectory.$now.'-'.$_FILES[$fieldname]['name']))
              {
                  $now++;
              }
              move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename);
            }
            elseif(($_FILES['file']['size'] > 1300000)||(in_array($notextension, $notallowedExts)))
            {
                echo '<script type="text/javascript">'; 
                echo 'alert("Invalid file upload. Please check your input.");'; 
                echo 'window.location.href = "index.php";';
                echo '</script>';
                die();
            }   
        }
        else
        {
            $uploadFilename = "def_img.jpg";
        }   
    }

    //for textbox input
    $name = $_POST['name'];
    $email = $_POST['email'];
    $office_id = $_POST['office_id'];
    $var_title = $_POST['title'];
    $var_story = $_POST['story'];
    $var_task = $_POST['task'];
    $var_power = $_POST['power'];
    $var_solve = $_POST['solve'];
    $var_result = $_POST['result'];

    $get_title = $_POST['title'];
    $title = strtoupper ($get_title);
    $namehere = "Super Story By " .$_POST['name'];
    $story = "My super story begins with " . $_POST['story'] . " My task was " . $_POST['task'] ." With the super power of ". $_POST['power'] ." I solved it by ". $_POST['solve'] ." The result was ". $_POST['result'];

    if (!empty($name) && !empty($email) && !empty($office_id) && !empty($title) && !empty($var_title) && !empty($var_story) && !empty($var_task) && !empty($var_power) && !empty($var_solve) && !empty($var_result)) {

    header('Content-Type: image/jpeg');

    $imagename = $uploadFilename;
    $im = imagecreatefromjpeg("bg.jpg");
    $img2 = imagecreatefromjpeg($imagename);
    $black = imagecolorallocate($im, 0, 0, 0);
    $font = 'arialbi.ttf';
    $font2 = 'ariali.ttf';

    $newtitle = wordwrap($title, 24, "\n", true);
    $newertitle = explode("\n", $newtitle);

    imagettftext($im, 33, 0, 8, 270, $black, $font, $newertitle[0]);
    imagettftext($im, 33, 0, 8, 320, $black, $font, $newertitle[1]);
    imagettftext($im, 12, 0, 283, 365, $black, $font, $namehere);

    $newtext = wordwrap($story, 50, "\n", true);
    $newertext = explode("\n", $newtext);
    imagettftext($im, 10, 0, 283, 385, $black, $font2, $newertext[0]);
    imagettftext($im, 10, 0, 283, 400, $black, $font2, $newertext[1]);
    imagettftext($im, 10, 0, 283, 415, $black, $font2, $newertext[2]);
    imagettftext($im, 10, 0, 283, 430, $black, $font2, $newertext[3]);
    imagettftext($im, 10, 0, 283, 445, $black, $font2, $newertext[4]);
    imagettftext($im, 10, 0, 283, 460, $black, $font2, $newertext[5]);
    imagettftext($im, 10, 0, 283, 475, $black, $font2, $newertext[6]);
    imagettftext($im, 10, 0, 283, 490, $black, $font2, $newertext[7]);
    imagettftext($im, 10, 0, 283, 505, $black, $font2, $newertext[8]);
    imagettftext($im, 10, 0, 283, 520, $black, $font2, $newertext[9]);
    imagettftext($im, 10, 0, 283, 535, $black, $font2, $newertext[10]);
    imagecopyresampled($im, $img2, 10, 350, 0, 0, 263, 175, imagesx($img2), imagesy($img2));

    $date_created = date("YmdHis");//get date created
    $img_name = "-img_entry.jpg"; //the file name of the generated image
    $img_newname = $date_created . $img_name; //datecreated+name
    $img_dir =dirname($_SERVER['SCRIPT_FILENAME']) ."/". $img_newname; //the location to save the image 
    imagejpeg($im, $img_dir , 80); //function to save the image with the name and quality

    $newpath = "/home/content/52/12096052/html/Testupdate/image_entry/";
    $newdir = $newpath.$img_newname;
    copy ($img_dir, $newdir); 
    $http_dir = 'https://www.uaewebdeveloper.com/Testupdate/image_entry/';
    $post_link = $http_dir . $img_newname;

    $msg = 'Super Story by ';
    $post_msg = $msg.$name;

    imagedestroy($im);
    }

    else{
        echo '<script type="text/javascript">'; 
        echo 'window.alert("*All fields required.");'; 
        echo 'window.location.href = "index.php";';
        echo '</script>';

    }
    //get time to save in db
    $sql_date = date("Y/m/d H:i:s");

    //save inputs to db
    if (!empty($name) && !empty($email) && !empty($office_id) && !empty($title) && !empty($story)) {

        $save_sql = mysql_query("INSERT INTO `tbl_amatest` (filename, name, email, office_id, title, story, time) VALUES ('$img_newname','$name','$email','$office_id','$title','$story','$sql_date')");

        header('Location:' .'success.html'); 
        mysql_close($con);
    }

2番目の画像をデフォルトとして指定すると、機能します。しかし、誰かがファイルをアップロードすると、アップロードされたものではなく、デフォルトのみが取得されます。基本的に、ここには2つの問題があります。

  1. アップロードされた画像が適切に渡されていない/アップロードされていない
  2. デフォルトの画像がサーバーから正しく取得されていません

私を助けてください。どうすればいいのかわからない。私は投稿を読んだことがありますが、ほとんど同じですが、役に立ちませんでした。事前にどうもありがとう!

4

1 に答える 1

0

コードで主な 2 つの変更を行うようにしてください。

  1. コードを if(isset($_FILES['file']['tmp_name'])){ に置き換えます if($_FILES['file']['tmp_name']!=""){

  2. デフォルトの画像からパスを削除: $uploadFilename = 'def_img.jpg';

私はそれが正常に動作することを試みました。デフォルトの画像とアップロードされた画像の両方をさらに処理できるようにテストした完全なコード。

$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);

// make a note of the directory that will recieve the uploaded files //i made it as the same directory where the .php file is
$uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self;
#echo "$uploadsDirectory<br>";

#exit;
$fieldname = 'file';
if(isset($_POST['submit']))
{           
    if($_FILES['file']['tmp_name']!="")
    {
        $allowedExts = array("jpeg", "jpg");
        $temp = explode(".", $_FILES['file']['name']);
        $extension = end($temp);

        $notallowedExts = array("png", "gif", "pdf", "doc", "docx", "txt", "html", "xlsx", "mov");
        $nottemp = explode(".", $_FILES['file']['name']);
        $notextension = end($nottemp);

        if ((($_FILES['file']['type'] == "image/jpeg") || ($_FILES['file']['type'] == "image/jpg")) && ($_FILES['file']['size'] < 1300000) && in_array($extension, $allowedExts))  
        {       

          $now = time();
          while(file_exists($uploadFilename = $uploadsDirectory.$now.'-'.$_FILES[$fieldname]['name']))
          {
              $now++;
          }
          move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename);
        }
        elseif(($_FILES['file']['size'] > 1300000)||(in_array($notextension, $notallowedExts)))
        {
            echo '<script type="text/javascript">'; 
            echo 'alert("Invalid file upload. Please check your input.");'; 
            echo 'window.location.href = "index.php";';
            echo '</script>';
            die();
        }   
    }
    else
    {
        $uploadFilename = "images.jpg";
    }   
}

/*$get_title = $_POST['title'];
$story = "My super story begins with " . $_POST['story'] . " My task was " . $_POST['task'] ." With the super power of ". $_POST['power'] ." I solved it by ". $_POST['solve'] ." The result was ". $_POST['result'];

if (!empty($name) && !empty($email) && !empty($office_id) && !empty($title) && !empty($var_title) && !empty($var_story) && !empty($var_task) && !empty($var_power) && !empty($var_solve) && !empty($var_result)) {

*/
echo " FILE : $uploadFilename";
//header('Content-Type: image/jpeg');

$upload = $uploadFilename;
$im = imagecreatefromjpeg("bg.jpg");
$img2 = imagecreatefromjpeg($upload);
$black = imagecolorallocate($im, 0, 0, 0);
$font = 'arialbi.ttf';
$font2 = 'ariali.ttf';

$newtitle = wordwrap($title, 24, "\n", true);
$newertitle = explode("\n", $newtitle);


imagettftext($im, 33, 0, 8, 270, $black, $font, $newertitle[0]);
imagettftext($im, 33, 0, 8, 320, $black, $font, $newertitle[1]);
imagettftext($im, 12, 0, 283, 365, $black, $font, $namehere);

$newtext = wordwrap($story, 50, "\n", true);
$newertext = explode("\n", $newtext);
imagettftext($im, 10, 0, 283, 385, $black, $font2, $newertext[0]);
imagettftext($im, 10, 0, 283, 400, $black, $font2, $newertext[1]);

imagecopyresampled($im, $img2, 10, 350, 0, 0, 263, 175, imagesx($img2), imagesy($img2));

$date_created = date("YmdHis");//get date created
$img_name = "-img_entry.jpg"; //the file name of the generated image
$img_newname = $date_created . $img_name; //datecreated+name
$img_dir =dirname($_SERVER['SCRIPT_FILENAME']) ."/". $img_newname; //the location to save the image 
imagejpeg($im, $img_dir , 80); //function to save the image with the name and quality

$newpath = "/home/content/52/12096052/html/Testupdate/image_entry/";
$newdir = $newpath.$img_newname;
copy ($img_dir, $newdir); //copy to new folder


$http_dir = 'https://www.uaewebdeveloper.com/Testupdate/image_entry/';
$post_link = $http_dir . $img_newname;

$msg = 'Super Story by ';
$post_msg = $msg.$name;

imagedestroy($im);
//}  
于 2013-11-14T09:18:31.803 に答える