PHP GD Image - 用作内联img src传递变量

时间:2014-12-12 02:51:45

标签: php html gd

我花了4天的时间来解决一个问题。我是这个人的新手。

我的一般问题是

<?php

$art = $_POST['source_file'];
$bag = $_POST['destination_file'];

$x1=100;
$y1 = 400;

$x2 = 700;
$y2 = 800;

?>

<img src = 'image.php'>

<?php
// other codes
?>

我需要将$ bag,$ art,$ x1,$ y1,$ x2,$ y2传递给image.php并将其用于创建GD图像​​。如果我使用变量的值并在image.php中说明,它可以工作。如果我按原样使用$ a(我认为变量是image.php的全局变量),它就不起作用。

如果我更换了img src =&#39; image.php&#39;使用有效的php require_once(&#39; image.php&#39;),屏幕上不会显示任何内容,因为图像未创建,并且由于image.php中的标题,页面表现为遗漏/未找到的图像。< / p>

Image.php的内容是:

<?php

$bag=$_GET['bag'];
// this is the variable to be passed either by get or by post or by global variable

header ('Content-Type: image/png');

//$im = imagecreatefrompng('./bag_templates/bag1.PNG');
//this comented $im works. I need to pass content from variable

$im = imagecreatefrompng($bag);

//$source = imagecreatefromJPEG('./thumbnails/2014_12_10_23_53_48.JPG');
$source = imagecreatefromJPEG($art);
//$art variable must be passed from main page

list($source_width, $source_height)=getimagesize($art);

//variables x1,y1,x2,y2 must be passed from main file.
$frame_l = $x2-$x1;
$frame_h = $y2-$y1;

$start_x = $x1 + (($frame_l -$source_width)/2);
$start_y = $y1 + (($frame_h -$source_height)/2);

$destination = imagecreatefrompng($bag);

imagecopy($destination, $source, $start_x, $start_y, 0, 0, $source_width, $source_height); 
//bool imagecopy ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h )

imagepng($destination);
imagedestroy($destination);
?>

应从主文件传递的变量:

$bag
$art
$x1, $y1
$x2, $y2

此图像的结果(合并图像 - $ destination)应显示在主页面中 - 只需将image.php调用为 -

img src ='image.php'

1 个答案:

答案 0 :(得分:0)

您可以通过查询字符串传递变量,将它们作为键值对传递,并用&符号分隔它们。
这意味着您不会按原样引用image.php,而是使用image.php?key1=value1&key2=value2&...
虽然这是HTML,但您必须使用&amp;而不是普通&,例如:

<?php
// ...
echo "<img src='image.php?bag=$bag&amp;art=$art&amp;x1=$x1'>"

然后,您可以通过访问image.php超全球来从$_GET检索它们:

$bag = $_GET['bag'];
$art = $_GET['art'];
// and so on

如果我理解正确,您还尝试使用require_once而不是通过<img src='image.php'加载图片。这会产生将图像数据直接转储到HTML文件中的效果,但不能正确呈现(虽然可能以内联方式加载图像,但我不推荐这种方法)。