首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用imagepng()获取要保存到所需目录的图像

无法使用imagepng()获取要保存到所需目录的图像
EN

Stack Overflow用户
提问于 2014-10-18 19:43:46
回答 1查看 255关注 0票数 0

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

下面是我的代码:

代码语言:javascript
复制
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 s.png”,但是没有效果。这在没有"chmod“权限更改的情况下也可以工作,我只是将其添加为故障排除的另一个选项。我还直接转到该文件夹并为用户设置了权限,同样没有什么好处。

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

EN

回答 1

Stack Overflow用户

发布于 2014-10-18 20:20:27

因此,它确实保存在您正在工作的文件夹中,按照注释掉的保存。你有没有试过不带点的图像/contact/email_address.png“;和/images/contact/email_address.png";?这些点上升到了一个级别,如果它在根之上,php将无法访问它

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26439376

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档