Php 如果未上载文件,则使用/从服务器获取默认图像

Php 如果未上载文件,则使用/从服务器获取默认图像,php,image,file-upload,Php,Image,File Upload,嗨,我有一个表单,要求用户上传照片。is将照片用作imagecreatefromjpg和imagecopyresampled中的第二个图像(图像位于另一个图像之上)。但似乎上传的图像没有被正确获取。此外,当未上载任何文件时,不会从服务器传递默认图像。我收到这些错误: Warning</b>: imagecreatefromjpeg() [<a href='function.imagecreatefromjpeg'>function.imagecreatefromjpeg

嗨,我有一个表单,要求用户上传照片。is将照片用作imagecreatefromjpg和imagecopyresampled中的第二个图像(图像位于另一个图像之上)。但似乎上传的图像没有被正确获取。此外,当未上载任何文件时,不会从服务器传递默认图像。我收到这些错误:

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 />
警告:imagecreatefromjpeg()[]:第117行的/home/content/52/12096052/html/Testupdate/processor.php中的文件名不能为空
警告:imagesx()希望参数1是资源,布尔值在第145行的/home/content/52/12096052/html/Testupdate/processor.php中给出
警告:imagesy()希望参数1是资源,布尔值在第145行的/home/content/52/12096052/html/Testupdate/processor.php中给出
警告:imagecopyresampled()要求参数2是资源,布尔值在第145行的/home/content/52/12096052/html/Testupdate/processor.php中给出
警告:无法修改标题信息-标题已由第186行的/home/content/52/12096052/html/Testupdate/processor.php:117)中的/home/content/52/12096052/html/Testupdate/processor.php发送
这是我的代码处理器.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);
    }
$con=mysql\u连接($hostname、$username、$password)或死亡(“无法
连接到数据库!请稍后再试。“);
mysql\u select\u db($dbname);
//记下相对于根目录的当前工作目录。
$directory\u self=str\u replace(basename($\u SERVER['PHP\u self']),'',$\u SERVER['PHP\u self']);
//记下将接收上传文件的目录//我将它设置为.php文件所在的目录
$uploadsDirectory=$\u服务器['DOCUMENT\u ROOT']$目录;
//记下上传表单的位置,以备需要
$uploadForm='https://'$_服务器['HTTP_HOST']$目录(u self)php';
//HTML表单中用于文件的字段名的名称
$fieldname='file';
//检查上传表单是否已实际提交或打印表单
如果(isset($_POST['submit']))
{           
如果($\u FILES['file']['tmp\u name']!=“”)
{
$allowedExts=数组(“jpeg”、“jpg”);
$temp=explode(“.”,$_文件['file']['name']);
$extension=end($temp);
$notallowedExts=数组(“png”、“gif”、“pdf”、“doc”、“docx”、“txt”、“html”、“xlsx”、“mov”);
$nottemp=explode(“.”,$_文件['file']['name']);
$notextension=end($nottemp);
如果(($_文件['file']['type']==“image/jpeg”)| |($_文件['file']['type']==“image/jpg”)&($_文件['file']['size']<1300000)&&in_数组($extension,$allowedExts))
{       
$now=时间();
而(文件存在($uploadFilename=$uploadsDirectory.$now.'-'.$文件[$fieldname]['name']))
{
$now++;
}
移动上传的文件($上传文件[$fieldname]['tmp\u name',$uploadFilename);
}
elseif($_FILES['file']['size']>1300000)| |(在_数组($notextension,$notallowedExts)中)
{
回声';
回显“警报”(“文件上载无效。请检查您的输入。”);
echo'window.location.href=“index.php”;
回声';
模具();
}   
}
其他的
{
$uploadFilename=“def_img.jpg”;
}   
}
//用于文本框输入
$name=$_POST['name'];
$email=$_POST['email'];
$office\u id=$\u POST['office\u 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\u title);
$namehere=“超级故事作者”$\u POST['name'];
$story=“我的超级故事以开头”$_发布[故事]。“我的任务是”$_POST['task'].“具有超能力”$_POST['power'].“我解决了它”$_POST['solve'].“结果是”$_POST['result'];
如果(!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)){
标题(“内容类型:图像/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,8270,$black,$font,$newertitle[0]);
imagettftext($im,33,0,8320,$black,$font,$newertitle[1]);
imagettftext($im,12,028365,$black,$font,$namehere);
$newtext=wordwrap($story,50,“\n”,true);
$newertext=分解(“\n”,$newtext);
imagettftext($im,10,0283385,$black,$font2,$newertext[0]);
imagettftext($im,10,0283400,$black,$font2,$newertext[1]);
imagettftext($im,10,0283415,$black,$font2,$newertext[2]);
imagettftext($im,10,0283430,$black,$font2,$newertext[3]);
imagettftext($im,10,0283445,$black,$font2,$newertext[4]);
imagettftext($im,10,0283460,$black,$font2,$newertext[5]);
imagettftext($im,10,0283475,$black,$font2,$newertext[6]);
imagettftext($im,10,0283490,$black,$font2,$newertext[7]);
imagettftext($im,10,0283505,$black,$font2,$newertext[8]);
imagettftext($im,10,0283520,$black,$font2,$newertext[9]);
imagettftext($im,10,0283535,$black,$font2,$newertext[10]);
imagecopyresampled($im、$img2、10350、0、0263175、imagesx($img2)、imagesy($img2));
$date_created=date(“YmdHis”);//获取创建日期
$img_name=“-img_entry.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);
//}