在PHP中使用imagejpeg生成图像
当我从在PHP中使用imagejpeg生成图像,php,image,Php,Image,当我从http://localhost/image.php?application=first一切正常 session_start(); $name = $_GET['application']; $text = rand(10000,99999); $_SESSION['application'][$name] = $text; $height = 25; $width = 65; $image_p = imagecreate($width, $height); $black
http://localhost/image.php?application=first
一切正常
session_start();
$name = $_GET['application'];
$text = rand(10000,99999);
$_SESSION['application'][$name] = $text;
$height = 25;
$width = 65;
$image_p = imagecreate($width, $height);
$black = imagecolorallocate($image_p, 0, 0, 0);
$white = imagecolorallocate($image_p, 255, 255, 255);
$font_size = 14;
imagestring($image_p, $font_size, 5, 5, $text, $white);
imagejpeg($image_p, null, 80);
但是当我改变了代码的时候
if (isset($_GET['application']) && !empty($_GET['application'])) {
if (isset($_GET['image']) && $_GET['image'] == 'get'){
session_start();
$name = $_GET['application'];
$text = rand(10000,99999);
$_SESSION['application'][$name] = $text;
$height = 25;
$width = 65;
$image_p = imagecreate($width, $height);
$black = imagecolorallocate($image_p, 0, 0, 0);
$white = imagecolorallocate($image_p, 255, 255, 255);
$font_size = 14;
imagestring($image_p, $font_size, 5, 5, $text, $white);
imagejpeg($image_p, null, 80);
}
}
然后我在浏览器中获得原始格式,所以我添加
header('Content-type: image/jpeg');
imagejpeg($image_p, null, 80);
对于代码,现在我得到消息,我的图片被中断,但当我将其保存在驱动器上并从IrfanView启动时,它会正常打开
我想补充一点,我正在测试ob_start()函数,但它没有改变任何东西。当我点击此URL时,它对我来说非常有用:
http://localhost/image.php?application=first&image=get
下面是我在image.php中使用的确切代码:
if (isset($_GET['application']) && !empty($_GET['application'])) {
if (isset($_GET['image']) && $_GET['image'] == 'get'){
session_start();
$name = $_GET['application'];
$text = rand(10000,99999);
$_SESSION['application'][$name] = $text;
$height = 25;
$width = 65;
$image_p = imagecreate($width, $height);
$black = imagecolorallocate($image_p, 0, 0, 0);
$white = imagecolorallocate($image_p, 255, 255, 255);
$font_size = 14;
imagestring($image_p, $font_size, 5, 5, $text, $white);
header('Content-type: image/jpeg');
imagejpeg($image_p, null, 80);
}
}
?>
好吧,我想出来了。问题是我在main IF statment之前添加了
require'common.php'
。我的整个代码都是这样的
<?php
require 'common.php';
if (isset($_GET['application']) && !empty($_GET['application'])) {
if (isset($_GET['image']) && $_GET['image'] == 'get'){
session_start();
$name = $_GET['application']; $text = rand(10000,99999); $_SESSION['application'][$name] = $text;
$height = 25; $width = 65; $image_p = imagecreate($width, $height); $black = imagecolorallocate($image_p, 0, 0, 0); $white = imagecolorallocate($image_p, 255, 255, 255); $font_size = 14;
imagestring($image_p, $font_size, 5, 5, $text, $white); header('Content-type: image/jpeg');
imagejpeg($image_p, null, 80);
}
.
.
.
是否将正确的GET参数传递给脚本?