图像旋转脚本 [英] image rotation script
问题描述
提前致谢
HTML:
< img src =rotate.phpalt =随机图片/>
PHP:
<?php
$ folder ='images /';
$ exts ='jpg jpeg png gif';
$ files = array(); $ i = -1;
if(''== $ folder)$ folder ='./';
$ handle = opendir($ folder);
$ exts = explode('',$ exts);
while(false!==($ file = readdir($ handle))){
foreach($ exts as $ ext){
if(preg_match('/ \。'。 $ ext。'$ / i',$ file,$ test)){
$ files [] = $ file;
++ $ i;
}
}
}
closedir($ handle);
mt_srand((double)microtime()* 1000000);
$ rand = mt_rand(0,$ i);
header('Location:'。$ folder。$ files [$ rand]);
?>
更新:这是非常基本的测试,看看它是否有效;
< script>
function changeImage(o){
o.src ='http://www.domain.com/testtest.php?'+(new Date())。getTime(); //时间来帮助停止缓存
}
< / script>
< img src =http://www.domain.com/testtest.phponclick =changeImage(this)/>
并在您的php中添加内容标题以发送图片,例如,如果它是 gif
或png ...
<?php
//参见('Content-type:image / gif');
$ b $ switch $($ selected_random_file_extention){
casegif:
header('Content-type:image / gif');
$ file_ext =gif;打破;
casejpg:
header('Content-type:image / jpg')
$ file_ext =jpg;打破;
情况3:
标题('Content-type:image / png');
$ file_ext =png;打破;
}
// header('Content-type:image / gif'); //发送图片
// $ random_image_name = rand(1,5);
//你会添加$文件夹。$ files [$ rand];并包括你的脚本
$ random_image_name = $文件夹。$ files [$ rand];
//根据需要调整路径
$ img_file =http://www.domain.com/\".$random_image_name.\".gif;
//。$ file_ext;
readfile($ img_file); //不知道内存如何去
?>
理想的是设置头
内容类型,并使用 readfile
读写文件到输出缓冲区,用这个武装你可以使用ajax作为另一种方式来请求图像并使用状态
来轻松显示加载指示符。
如果您在哪里重写URL,您可以更改.rcp的扩展名甚至没有延伸。
http:// php。 net / manual / en / function.pathinfo.php
使用 pathinfo'extension'
你可以获得延伸,然后设置
approprate Content-type
So I have this script that will rotate images located in a directory, which works great. However, it requires a refresh of the page in order to rotate the image. Is there a way I can modify this to have it rotate the image when the image itself is clicked? Thanks in advance
HTML:
<img src="rotate.php" alt="random image" />
PHP:
<?php
$folder = 'images/';
$exts = 'jpg jpeg png gif';
$files = array(); $i = -1;
if ('' == $folder) $folder = './';
$handle = opendir($folder);
$exts = explode(' ', $exts);
while (false !== ($file = readdir($handle))) {
foreach($exts as $ext) {
if (preg_match('/\.'.$ext.'$/i', $file, $test)) {
$files[] = $file;
++$i;
}
}
}
closedir($handle);
mt_srand((double)microtime()*1000000);
$rand = mt_rand(0, $i);
header('Location: '.$folder.$files[$rand]);
?>
Update: this is very basic but test it out and see if it works;
<script>
function changeImage(o){
o.src = 'http://www.domain.com/testtest.php?' + (new Date()).getTime(); // time to help stop caching
}
</script>
<img src="http://www.domain.com/testtest.php" onclick="changeImage(this)" />
and in your php add a content header to send the image eg if it is a gif
or png...
<?php
// see link at bottom for getting file extention
switch ($selected_random_file_extention) {
case "gif":
header('Content-type: image/gif');
$file_ext = "gif"; break;
case "jpg":
header('Content-type: image/jpg')
$file_ext = "jpg"; break;
case 3:
header('Content-type: image/png');
$file_ext = "png"; break;
}
//header('Content-type: image/gif'); // send as image
//$random_image_name = rand(1,5);
// you would add $folder.$files[$rand]; and include you script above
$random_image_name = $folder.$files[$rand];
// adjust paths as needed
$img_file = "http://www.domain.com/".$random_image_name.".gif";
// .$file_ext;
readfile($img_file); // not sure how memory will go
?>
The idear is to set the header
content type and to read and write the file to the output buffer using readfile
, armed with this you could use ajax as another means to request the image and using the status
to easly display a loading indicator.
If you where to Rewrite the URL you could change the extension of .php for the src or not even have an extention.
http://php.net/manual/en/function.pathinfo.php
using pathinfo 'extension'
you can get the extention and then set the
approprate Content-type
这篇关于图像旋转脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!