The mgiPHP Tag
Tag Behavior
Use the mgiPHP tag to process PHP code and pass information between PHP and MGI.
Tag Syntax
The mgiPHP tag has no required parameters, two optional parameters, a body and an end tag. The tag form is:
<mgiPHP mgiBefore="True/False" mgiAfter="True/False">
PHP Code
</mgiPHP>
Required Parameters:
Optional Parameters:
- mgiBefore - Any MGI code in the body of the mgiPHP tag is processed prior to the PHP code. The default value is "False". This parameter may be used in conjunction with the mgiAfter parameter.
- mgiAfter - Any MGI code in the body of the mgiPHP tag is processed after the PHP code. The default value is "False". This parameter may be used in conjunction with the mgiBefore parameter.
Technical Notes
PHP.INI Configuration File:
Within the Advanced Module Preferences (Server Admin > Domain Management > Domain Preferences > Region Management > Region Preferences > Advanced Module Preferences), you may specify the full path (starting at root) to a php.ini file for the selected domain/region. The main php.ini file is installed with MGI (and may be modified during upgrades). If you specify an example path in the Server Settings, that path will be displayed as part of the instructions.
GD Graphic Extension:
The version of PHP bundled with MGI includes the GD Graphic Extension. The GD Graphic Extension can be used to resize images. Examples of resizing images with the GD Graphic Extension are show below. See the GD Graphic Extension manual for additional details:
GD Graphic Extension Manual
Example Usage and Output
Default Tag
<mgiPHP>
<? print(Date("l F d, Y")); ?>
</mgiPHP>
In this example, the PHP date code is processed and displayed.
mgiBefore Parameter
<mgiPHP mgiBefore="True">
<?
srand(time());
$random = (rand()%<mgiGet name="HighNumber">);
print("Random number between 0 and <mgiGet name="HighNumber"> is: $random");
?>
</mgiPHP>
In this example, a number is embedded in the PHP code using an MGI variable.
mgiAfter Parameter
<mgiPHP mgiAfter="True">
<mgi<?php printf ("Date"); ?>>
</mgiPHP>
In this example, the mgiDate tag is created dynamically with PHP code, then the mgiDate tag is processed displaying the date.
mgiBefore And mgiAfter Parameters
<mgiPHP mgiBefore="True" mgiAfter="True">
<mgi<?php printf ("Math"); ?>>10 *
<?
srand(time());
$random = (rand()%<mgiGet name="HighNumber">);
print("$random");
?>
</mgi<?php printf ("Math"); ?>>
</mgiPHP>
When this mgiPHP tag is processed, the mgiGet tag is first processed embedding a number in the PHP random number code. Next the PHP code is processed creating the mgiMath tag and displaying a random number. Finally, the mgiMath tag is processed.
GD Graphic Extension Resizing Images Dynamically And Displaying In The Browser
<mgiPHP><?php
// The file
$filename = 'test.jpg';
$percent = 0.5;
// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;
// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// Output
imagejpeg($image_p);
?></mgiPHP>
GD Graphic Extension Resizing Images And Saving The Image To A File
<mgiPHP><?php
// The file
$filename = 'test.jpg';
$percent = 0.5;
// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;
// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// Output
imagejpeg($image_p, 'testsm.jpg');
?></mgiPHP>
Suggested Usage
- Exchange information between MGI and PHP
- Dynamically Build MGI Tags with PHP
- Dynamically Build PHP Tags with MGI
- Create Thumbnail Images
|