QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 2144|回复: 6

如何重复调入图形

[复制链接]
发表于 2006-1-30 01:20:04 | 显示全部楼层 |阅读模式
想批量生成图形存放为文件(每个图形略有差异),模拟代码如下:
<代码>
<?php
$_SESSION['a']='255';
require('file5.php');

$_SESSION['a']='0';
require('file5.php');

?>

<file5.php>
<?php

header("Content-type: image/png");
$eee=$_SESSION['a'];
$file=$eee;
$string = '123';
//$im     = imagecreatefrompng("images/button1.png");
$im=imagecreate(640,580);
$orange = imagecolorallocate($im, $eee, 210, 60);
$px     = (imagesx($im) - 7.5 * strlen($string)) / 2;
imagestring($im, 3, $px, 9, $string, $orange);
imagepng($im,$file);
imagedestroy($im);
?>

应到出现两幅图,却只有一副,即第一副。经过检查,发现没有执行到第二步,难道是调用程序可以,但是生成图片,不能重复调用就不可了?
如果主程序改称如下也不可:
$_SESSION['a']='255';
require('file5.php');

$_SESSION['a']='0';
require('file6.php');

难道php里图形代码不能重载?
发表于 2006-1-30 10:40:08 | 显示全部楼层
祝ccat狗年旺旺

最起码函数化
[code:1]
function ccat ($content, $string = '123')
{
    header("Content-type: image/png");
    $file = $content;
    $im=imagecreate(640,580);
    $orange = imagecolorallocate($im, $eee, 210, 60);
    $px = (imagesx($im) - 7.5 * strlen($string)) / 2;
    imagestring($im, 3, $px, 9, $string, $orange);
    imagepng($im,$file);
    imagedestroy($im);
}
[/code:1]

调用
[code:1]
ccat($_SESSION['a']);
[/code:1]

家里兔爸爸的机器上没有PHP环境,就不能测试了
回复

使用道具 举报

 楼主| 发表于 2006-1-30 13:05:26 | 显示全部楼层
还是不行,调用一次可以,但是如果
a=1;
ccat($_SESSION['a']);
a=255;
ccat($_SESSION['a']);

结果等同于a=1;
回复

使用道具 举报

发表于 2006-1-30 22:36:41 | 显示全部楼层
一个 php 文件只能形成一个图像文件。
因为 php 是通过 header 来让浏览器认为当前打开的文件是一个 image 而不是一个 html 。
这个可以说是浏览器的问题。不支持一次打开获得两个图片。

要知道 require() 是 php 的函数,而不是浏览器的。
php 不管 require() 多少次,对于浏览器来说都是一次打开。

编写 php 你首先已应该记住等 php 代码完全退出了,才是浏览器的一次完整打开一个网页文件的任务。浏览器打开网页,一个 html 是一次,一个 image 也是一次,一个框架也是需要再读取一次的。

我建议是:
main.php :
<?php
for($i=0;$i<=255;$i++)
{
echo "<image src=\"image.php?id=$i\">"
}
?>

image.php:

<?php

header("Content-type: image/png");
$eee=$_GET["id"];
$file=$eee;
$string = '123';
//$im = imagecreatefrompng("images/button1.png");
$im=imagecreate(640,580);
$orange = imagecolorallocate($im, $eee, 210, 60);
$px = (imagesx($im) - 7.5 * strlen($string)) / 2;
imagestring($im, 3, $px, 9, $string, $orange);
imagepng($im,$file);
imagedestroy($im);
?>
回复

使用道具 举报

发表于 2006-1-31 11:27:01 | 显示全部楼层
如果是header的发送问题

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

<html>
<?php
/* This will give an error. Note the output
* above, which is before the header() call */
header('Location: http://www.example.com/');
?>  

As of PHP 4, you can use output buffering to get around this problem, with the overhead of all of your output to the browser being buffered in the server until you send it. You can do this by calling ob_start() and ob_end_flush() in your script, or setting the output_buffering configuration directive on in your php.ini or server configuration files.

要在header发送之前保持“干净”

[code:1]
ob_start("ccat");

ob_end_flush();
[/code:1]
回复

使用道具 举报

 楼主| 发表于 2006-2-2 09:41:16 | 显示全部楼层
最后用的这个办法,解决的问题。图片一个接着一个的在一个浏览器里声称,看起来困难一点,但至少能用。
[code:1]echo "<image src=\"image.php?id=$i\">" [/code:1]
回复

使用道具 举报

发表于 2006-2-2 12:31:36 | 显示全部楼层
cool
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-4-20 17:56 , Processed in 0.055382 second(s), 15 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表