我想在网站后从转储文件夹中调用图像。
我的image.php页面如下所示:
<?php
header('Content-type: image/jpeg');
$jpg_image = imagecreatefromjpeg('Desert.jpg');
$white = imagecolorallocate($jpg_image, 73, 41, 236);
$font_path = 'OpenSans-Italic.TTF';
$text = $_GET['name'] ;
imagettftext($jpg_image, 25, 0, 75, 50, $white, $font_path, $text);
$image_url="dump/".rawurlencode(trim($text)).".jpg";
imagejpeg($jpg_image,$image_url);
readfile($image_url);
imagedestroy($jpg_image);
?>我在主页中使用了一个Javascript,它将我重定向到带有图像的result.php。
我的result.php页面如下所示:
<html>
<body>
<img src="image.php?name=<?php echo $_GET['name']; ?>" />
</body>
</html>现在,我从image.php调用图像,并希望从转储文件夹调用它。有什么帮助吗?
发布于 2015-04-17 16:50:42
这是你的results.php。首先,它创建图像并将其保存到/dump文件夹中,然后呈现HTML。此时,图像将直接从/dump文件夹加载。
<html>
<body>
<?php
$text = $_GET['name'] ;
$image_url="dump/".rawurlencode(trim($text)).".jpg";
// Check if the file doesn't exist, create it
if (!file_exists($image_url)) {
$jpg_image = imagecreatefromjpeg('Desert.jpg');
$white = imagecolorallocate($jpg_image, 73, 41, 236);
$font_path = 'OpenSans-Italic.TTF';
imagettftext($jpg_image, 25, 0, 75, 50, $white, $font_path, $text);
imagejpeg($jpg_image,$image_url);
imagedestroy($jpg_image);
}
?>
<img src="<?php echo $image_url; ?>" />
</body>
</html>一旦该原型正常工作,您就可以添加更多的安全性。
发布于 2015-04-17 16:47:43
我想你不应该那样做。
1.)直接在图像标记中使用$_GET['name']。您不应该信任用户输入,所以如果您在图像中使用变量,请先过滤它。
http://php.net/manual/en/function.filter-input.php
2.)当您在上传上生成该图像以保存性能时,效果要好得多。每次你浪费大量的CPU时,你都会进行渲染。
所以最好保存原始图像,构建一个缓存,并在上传后转换图像,或者在您的方法中使用if来检查图像是否被缓存。
<?php
header('Content-type: image/jpeg');
$filename = rawurlencode(trim($text)).".jpg";
if(!is_file(__DIR__.'dump_cache/'.$filename)) {
$jpg_image = imagecreatefromjpeg('Desert.jpg');
$white = imagecolorallocate($jpg_image, 73, 41, 236);
$font_path = 'OpenSans-Italic.TTF';
$text = $_GET['name'] ;
imagettftext($jpg_image, 25, 0, 75, 50, $white, $font_path, $text);
$image_url="dump/".rawurlencode(trim($text)).".jpg";
imagejpeg($jpg_image,$image_url);
readfile($image_url);
.... here write file to your cache folder .....
imagedestroy($jpg_image);
} else {
echo file_get_contents(__DIR__.'dump_cache/'.$filename);
}
?>就像这样。当您的图像没有从转储文件夹加载时,尝试在使用__DIR__之前添加完整的路径。
就像这样:
$image_url= __DIR__."/dump/".rawurlencode(trim($text)).".jpg";如果是同一个文件夹。
https://stackoverflow.com/questions/29704518
复制相似问题