Introduction
This article provides information about using PHP Wrapper Classes in PHP5. Aspose doesn't provide any special Aspose.Slides component that can be used with PHP. But, PHP developers can use Aspose.Slides for Java to create and manipulate their presentations with the help of PHP/Java Bridge. There are Two Approaches that can be used by PHP developers to work with Aspose.Slides for Java. Discussing the comparison and use of these two approaches is the main objective of this article.
PHP Wrapper Classes & PHP5
For PHP5 developers, Aspose.Slides for Java includes special wrapper classes. These wrapper classes are created in PHP. So, developers can directly use these wrapper classes to work with their presentations and these wrapper classes make use of Aspose.Slides classes at the backend. These wrapper classes are available to PHP developers in the form of a PHP file. Since these wrapper classes are open source so, developers can also modify these wrapper classes according to their needs.
Wrapper classes are very useful to PHP developers but currently, these wrapper classes can only be used with PHP5. The most important benefit of using wrapper classes is that it reduces your PHP code. However, developers can also work with their presentations without using wrapper classes.
So, there are two approaches that can be used by PHP developers to create presentations as follows:
- Creating Presentations without Wrapper Classes
- Creating Presentations with Wrapper Classes
To provide a better comparison of these two approaches, we have discussed each approach individually with the help of example code snippets.
1. Creating Presentations Without Wrapper Classes
Whether you want to create presentations using PHP4 or PHP5, this approach can be used. All of the code snippets in the Aspose.Slides wiki are provided using this approach so that all kinds of PHP programmers may use the same code to use Aspose.Slides for Java in their PHP applications.
This approach requires PHP developers to work directly with the classes of Aspose.Slides for Java. Using PHP/Java Bridge, PHP developers can instantiate Java classes of Aspose.Slides and work with them to create and manipulate their presentations.
Creating presentations without using wrapper classes is not tough but it requires developers to write more code for working with Java classes directly. Developers can get a better idea by reviewing the example code snippet given below that opens an existing PPT file and adds a rectangle shape to a slide of the presentation.
Example:
[PHP]
<?php
//Using aspose.slides.jar file so that the classes inside the jar file
//can be used
java_require("aspose.slides.jar");
try
{
//Creating a file input stream to read the PPT file
$fistream=new Java("java.io.FileInputStream","C:\\demo.ppt");
//Instantiate a Presentation object that represents a PPT file
$pres=new Java("com.aspose.slides.Presentation",$fistream);
//Accessing a slide using its slide position
$slide=$pres->getSlideByPosition(2);
//Creating a Point object to define the starting point of the line
$point1=new Java("java.awt.Point",1000,2000);
//Creating a Point object to define the ending point of the line
$point2=new Java("java.awt.Point",4700,2000);
//Adding a line shape into the slide with its start and end points
$slide->getShapes()->addLine($point1,$point2);
//Creating a file output stream to write the output file
$fostream=new Java("java.io.FileOutputStream","C:\\modified.ppt");
//Writing the presentation as a PPT file
$pres->write($fostream);
//Closing the streams
$fistream->close();
$fostream->close();
}
catch(JavaException $ex)
{
echo $ex->toString();
}
?>
2. Creating Presentations With Wrapper Classes
Currently, this approach can only be used with PHP5. This approach has siginificant advantages of reducing code and programming complexities. Normally, wrapper classes are created in AsposeSlides.php file that comes with Aspose.Slides for Java.
Developers can easily include AsposeSlides.php file in their applications and use the wrapper classes defined in it. To help you in making a better comparison between the two approaches (i.e. Using & Without Using Wrapper Classes), we have created the same example code snippet as we used in the first approach except that this time we have used wrapper classes to perform the same task.
In the example below, we have made use of a base wrapper class, ClassFactory that provides various useful functions to deal with presentations. We have only used NewPresentation function of the ClassFactory base class to open an existing presentation and then we added a rectangle to a slide of the presentation.
Example:
[PHP]
<?php
require_once("AsposeSlides.php");
//Instantiate a Presentation object that represents a PPT file
$pres = Classfactory::NewPresentation("C:\\demo.ppt");
//Accessing a slide using its slide position
$slide=$pres->getSlideByPosition(2);
//Adding a rectangle shape into the slide by defining its X,Y postion,
//width and height
$slide->getShapes()->addRectangle(1400,1100,3000,2000);
//Creating a file output stream to write the output file
$pres->write("C:\\modified.ppt");
?>
Conclusion
By experimenting above two approaches to perform the same task, we can see that 2nd approach is much better, easier, flexible and efficient for PHP developers. Unlike 1st approach, it is not required to create and close streams for opening and writing presentations. Moroever, many other programming steps are eliminated that results in reducing the size of the application. So, if you are are using PHP5 then 2nd approach is a better choice for you. But, if you are looking for such a code that can be used on any version of PHP (that is PHP4 or PHP5) then 1st approach is suitable for you.
About Author
This article is written by Khawaja Salman Sarfraz (Team Leader, Aspose Pty Ltd.). He has obtained a Masters degree in Computer Sciences from a reputable university. He has also been providing training and consultancy services on J2EE and .NET platforms.
Contact Author
Download Complete Source Code for this Article