PHP |创建图像适用于本地服务器但不适用于远程主机

PHP | Create image works on local server but not remote host

我修改了一个项目以将文本插入到图像中。它在本地服务器上完美运行,但在远程他说: 在此服务器上找不到请求的 URL /covers/03c7c0ace395d80182db07ae2c30f034.jpg。

该脚本不会创建图像,但在我的本地服务器上它可以工作。安装了 GD 和 Freefont,所以我不知道该怎么做 请帮助我

<?php

$fontname = './font/arial.ttf';
// controls the spacing between text
$i=30;
//JPG image quality 0-100
$quality = 90;

function create_image($user){

  global $fontname;
  global $quality;
  $file = "./covers/".md5($user[0]['name'].$user[1]['name'].$user[2]['name']).".jpg";

 // if the file already exists dont create it again just serve up the original
 //if (!file_exists($file)) {


   // define the base image that we lay our text on
   $im = imagecreatefromjpeg("pass.jpg");

   // setup the text colours
   $color['grey'] = imagecolorallocate($im, 0, 0, 0);
   $color['green'] = imagecolorallocate($im, 55, 189, 102);

   // this defines the starting height for the text block
   $y = imagesy($im) - $height - 1393;
   $ye = imagesy($im) - $height - 1078;

  // loop through the array and write the text
  foreach ($user as $value){
   // center the text in our image - returns the x value
   $x = 1260;
   $xe = 930;
   $text=$value['name'];
   $testo=$value['gatto'];
   $newtext = wordwrap($text, 40, "\n", true);
   imagettftext($im, $value['font-size'], 0, $x, $y, $color[$value['color']], $fontname,$newtext);
   $newtesto = wordwrap($testo, 40, "\n", true);
   imagettftext($im, $value['font-size'], 0, $xe, $ye, $color[$value['color']], $fontname,$newtesto);
   // add 32px to the line height for the next text block


  }
   // create the image
   imagejpeg($im, $file, $quality);

 //}

  return $file;
}

function center_text($string, $font_size){

   global $fontname;

   $image_width = 800;
   $dimensions = imagettfbbox($font_size, 0, $fontname, $string);

   return ceil(($image_width - $dimensions[4]) / 2);
}



 $user = array(

  array(
   'name'=> '',
   'font-size'=>'9',
   'gatto'=>$_POST['gatto'],
   'color'=>'grey'),


  array(
   'name'=> '',
   'font-size'=>'16',
   'color'=>'grey'),

  array(
   'name'=> '',
   'font-size'=>'13',
   'color'=>'green'
   )

 );




 $user = array(

  array(
   'name'=> $_POST['name'],
   'font-size'=>'9',
   'gatto'=>$_POST['gatto'],
   'color'=>'grey'),


  array(
   'name'=> $_POST['job'],
   'font-size'=>'16',
   'color'=>'grey'),

  array(
   'name'=> $_POST['email'],
   'font-size'=>'13',
   'color'=>'green'
   )

 );





// run the script to create the image
$filename = create_image($user);

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Schoolexploit.com | Modifica Etichette</title>

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">
<link rel="stylesheet" href="./style.css" media="screen" title="no title" charset="utf-8">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>

</head>

<body>

<div class="row">
 <div class="col-xs-12" id="asd">
   <img src="<?=$filename;?>" width="1000" height="500"/><br/><br/>
 </div>

</div>


<ul>
<?php if(isset($error)){

 foreach($error as $errors){

  echo '<li>'.$errors.'</li>';

 }


}?>
</ul>
<div class="col-xs-12" id="asd">

<h2>Istruzioni:</h2>

<p>Inserisci qui sotto il testo (rispetta il limite di caratteri)</p>
<p>Clicca sul pulsante Modifica per generare l'etichetta</p>
<p>Clicca col tasto destro sull'immagine, "salva immagine con nome..."</p>
<p>Stampa e ritaglia l'etichetta (per un risultato ottimale è consigliata una stampante laser)</p>
<p> Sostituiscila all'originale</p>

<div class="dynamic-form">
<form action="" method="post">
<label>Testo1(Max 200):</label>
<input type="text" value="<?php if(isset($_POST['name'])){echo $_POST['name'];}?>" name="name" maxlength="200" placeholder=""><br/>
<label>Testo2(Max 120):</label>
<input type="text" value="<?php if(isset($_POST['gatto'])){echo $_POST['gatto'];}?>" name="gatto" maxlength="120" placeholder=""><br/>
<input name="submit" type="submit" class="btn btn-primary" value="Modifica" />
</form>
</div>
</div>




</body>
</html>

你检查过covers目录的权限了吗?将其设置为 644 或 777。

我假设您是 运行 Windows 上的本地项目。 Windows 处理权限的方式与 Linux 不同。这就是为什么它可以在本地运行但不能在您的远程服务器上运行的原因。

他先于我,但是:

chmod("/file/or/directory", 0777);  

我忘记了:如果您(您的网络服务器)对该目录没有写权限。您不能直接对该目录进行 chmod,您需要使用 Chown 或 chmod 从 shell.

获取所有权

在Linux终端

sudo chmod 777 /some/directory