无法使用imagepng()将图像保存到所需目录中

时间:2014-10-18 11:43:46

标签: php wamp

我正在使用PHP的imagettftext函数将文本转换为图像,然后将其保存到服务器。我遇到的问题是将它保存在我想要的目录中,保存到脚本运行的当前目录没有问题。

这是我的代码:

function txt2img($text, $fSize)
{
// Path to our font file
$fType = 'verdana.ttf';

// First we create our bounding box for the text
$bbox = imagettfbbox($fSize, 0, $fType, $text);

// We calculate the width and height using the x and y points.
// Width is: upper right corner, X position - upper left corner, X position
$width = $bbox[4] - $bbox[6] + 8;

// Height is: lower left corner, Y position - upper left corner, Y position
$height = $bbox[1] - $bbox[7] + 4;

// Determine the x position for the text to start
$x = 2;

// Determine the y position for the text to start
$y = $height - 4;

// Create the image
$im = imagecreatetruecolor($width, $height);
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255);

// Create the background color to match the website
$default = imagecolorallocate($im, 255, 238, 198);

// Set the background color
imagefilledrectangle($im, 0, 0, $width, $height, $default);

// Write it
imagettftext($im, $fSize, 0, $x, $y, $black, $fType, $text);

// Set the location to save the image.
//$save = "email_address.png";
$save = "../images/contact/email_address.png";

// Set the folder permissions to allow us to write to the folder.
chmod("../images/contact/",0755);

// .png tends to look better than a .jpg
imagepng($im,$save);
imagedestroy($im);
// Return the image dimensions so they can be stored in the database. This is used to properly diplay the image on the front end.
return ($width."+".$height);
}

如果我使用已注释掉的$ save位置,它完全正常,它只是不在我希望它所在的目录中。我尝试过其他路径,例如“/ images / contact / email_address.png“但没有快乐。这也没有“chmod”权限更改,我只是将其添加为故障排除的另一个选项。我也直接进入了该文件夹,并为用户设置了权限,再次,没有快乐。

我在本地WAMP服务器上运行它。此外,还检查了$ text,并且$ fSize是字体大小,也已经过检查。因为这在当前目录中运行良好,我觉得这不是一个编码问题,因为它是一个目录问题,但这只是我的观点。

1 个答案:

答案 0 :(得分:0)

因此,根据已注释的保存,它会保存在您正在处理的文件夹中。你试过images / contact / email_address.png“;和/images/contact/email_address.png”;没有点?点数上升到一个水平,如果它高于根,它将无法访问php