本文介绍了.php外部站点上的随机图像用作.jpg的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发布的论坛仅允许从外部URL加载.jpg,.png和.gif图像.我想解决这个问题,并从服务器上的目录中随机选择一个动态化身,但我无法使其正常工作(可能是由于在外部站点上进行了额外的检查,或者我的代码中有错误)

A forum that I post on only allows .jpg, .png and .gif images to be loaded from an external URL. I want to get around this and have a dynamic avatar chosen randomly from a directory on my server but I'm having trouble getting it to work (possibly due to extra checks being carried out on the external site, or an error in my code).

到目前为止,我已经在服务器上创建了一个名为"avatar.jpg"的文件夹,该文件夹中index.php文件中的代码如下:-

So far I've created a folder named "avatar.jpg" on my server and the code in my index.php file within that folder is as follows:-

<?php
$arr=array();
for($i=1;$i<6;$i++){
$arr[$i]=$i.".jpg";
}
$random=rand(1,6);
echo $arr[$random];
?>

我在avatar.jpg文件夹中有6张图像,分别名为1.jpg,2.jpg等.

I have 6 images in the avatar.jpg folder, named 1.jpg, 2.jpg etc.

当我运行mydomain.com/avatar.jpg时,它会正确显示一个随机图像源,例如. 5.jpg,但是当我在论坛上输入此URL作为我的头像URL时,它无法加载,并且Firebug报告无法加载给定的URL".

When I run mydomain.com/avatar.jpg it correctly displays a random image source, eg. 5.jpg, but when I enter this URL as my avatar URL on the forum it fails to load and Firebug reports "Failed to load given URL".

我是否想从代码中丢失某些东西来实现预期的工作?

Am I missing something from my code to make this work as hoped?

推荐答案

似乎您正在使用文本字符串"$ name.jpg"来响应.jpg文件!您必须设置正确的内容类型标题,并提供实际图像文件的位和字节.

It seems you are responding with the text string "$name.jpg" for a .jpg file! You have to set the correct content-type headers and serve the bits and bytes of the actual image file.

类似的东西:

header("Content-type: image/jpeg");
echo file_get_contents($randomFilepath);

这篇关于.php外部站点上的随机图像用作.jpg的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 10:38