The following example simply detects the MIME type of an image and prints it:
1. <?php
2. require_once 'tutorial_autoload.php';
3. $tutorialPath = dirname( __FILE__ );
4.
5. $image = new ezcImageAnalyzer( "{$tutorialPath}/img/imageanalysis_example_01.jpg" );
6.
7. echo "Image has MIME type <{$image->mime}>.\n";
8. ?>
On line 5 a new ezcImageAnalyzer object is instantiated. This must be done for
each image to analyse. In line 7 you see how to access the MIME type determined
from the image. Here is an example image including the output:
Image has MIME type <image/jpeg>
Besides the MIME type, ImageAnalysis extracts more information if you request it.
The following example shows how this works:
1. <?php
2. require_once 'tutorial_autoload.php';
3. $tutorialPath = dirname( __FILE__ );
4.
5. $image = new ezcImageAnalyzer( $tutorialPath.'/img/imageanalysis_example_02.jpg' );
6.
7. echo "Image data:\n";
8. echo "MIME type:\t{$image->mime}\n";
9. echo "Width:\t\t{$image->data->width} px\n";
10. echo "Height:\t\t{$image->data->height} px\n";
11. echo "Filesize:\t{$image->data->size} b\n";
12.
13. $comment = ( $image->data->comment == '' ) ? 'n/a' : $image->data->comment;
14. echo "Comment:\t{$comment}\n";
15. ?>
The example is basically the same as the first one, except that more data is requested
from ezcImageAnalyzer (lines 8-11). The major difference here is, that in
example 1, ezcImageAnalyzer has only determined the MIME type of the file and
has not performed any further analysis. The analysis of additional data
is performed on the first request to it (line 9). After that, the data is
cached in the ezcImageAnalyzer object.
The width, height and size values are available for every analyzable image.
A comment is not always necessarily be available however. At least, every data has
some sensible default value, even if it unavailable. (Note: Availability of
some data also depends on the availability of PHPs Exif extension.)
The example image and the output printed is shown below:
Image data:
MIME type: image/jpeg
Width: 380 px
Height: 285 px
Filesize: 25984 b
Comment: n/a
Like ezcImageConversion, ezcImageAnalyzer is based on handler classes, which
allow it to utilize different back-ends for image analysis. Currently
implemented are:
- ezcImageAnalyzerHandlerPhp
- This one uses PHP's getimagesize() function (which does not require the GD
extension!) and can optionally use PHP's exif extension.
- ezcImageAnalyzerHandlerImagemagick
- Here ImageMagick's "identify" program is used.
Both handlers are activated by default and are capable of determining if their
preconditions are fulfilled themselves.
It may happen that one needs to configure a handler, for example on some
systems you want to assign the path to the ImageMagick "identify" binary,
because it's not available in the $PATH environment variable. The following
example shows how this is possible and what else is configurable for the
handlers:
1. <?php
2. require_once 'tutorial_autoload.php';
3. $tutorialPath = dirname( __FILE__ );
4.
5. ezcImageAnalyzer::setHandlerClasses(
6. array(
7. 'ezcImageAnalyzerImagemagickHandler' => array( 'binary' => '/usr/bin/identify' ),
8. )
9. );
10.
11. $image = new ezcImageAnalyzer( $tutorialPath.'/img/imageanalysis_example_03.jpg' );
12.
13. echo "Image data:\n";
14. echo "MIME type:\t{$image->mime}\n";
15. echo "Width:\t\t{$image->data->width} px\n";
16. echo "Height:\t\t{$image->data->height} px\n";
17. echo "Filesize:\t{$image->data->size} b\n";
18.
19. $comment = ( $image->data->comment == '' ) ? 'n/a' : $image->data->comment;
20. echo "Comment:\t{$comment}\n";
21. ?>
Basically, the code is the same as in example 2, except that the
ezcImageAnalyzer is being configured to only use its ImageMagick handler and
not the PHP handler. Besides that, the location of the "identify" binary is set
explicitly. See the results below:
Image data:
MIME type: image/jpeg
Width: 320 px
Height: 240 px
Filesize: 26365 b
Comment: San Francisco airport, Oktober 2005.