User's Manual

Resizing Images
Beta Draft Loading Images 7-9
5.
In the Extension sub-tab page, expand the Zend Core Extensions tree control.
Locate the gd -GD (Image Manipulation) entry and change its switch to on or
enabled.
6. In the Extension sub-tab page, to save the configuration changes click the Save
Setting link.
7. In the Extension sub-tab page, to restart the web server click the Restart Server
link.
8. To logout of the Zend Core for Oracle Console, click the Logout link.
9. Edit anyco_db.inc. To resize the image if it is larger than a thumbnail, add the
following code before the call to $lob->savefile($imgfile) in
db_insert_thumbnail():
$r = oci_execute($stmt, OCI_DEFAULT);
if (!$r) {
$e = db_error($stid, __FILE__, __LINE__);
return false;
}
// Resize the image to a thumbnail
define('MAX_THUMBNAIL_DIMENSION', 100);
$src_img = imagecreatefromjpeg($imgfile);
list($w, $h) = getimagesize($imgfile);
if ($w > MAX_THUMBNAIL_DIMENSION || $h > MAX_THUMBNAIL_DIMENSION)
{
$scale = MAX_THUMBNAIL_DIMENSION / (($h > $w) ? $h : $w);
$nw = $w * $scale;
$nh = $h * $scale;
$dest_img = imagecreatetruecolor($nw, $nh);
imagecopyresampled($dest_img, $src_img, 0, 0, 0, 0, $nw, $nh, $w, $h);
imagejpeg($dest_img, $imgfile); // overwrite file with new thumbnail
imagedestroy($src_img);
imagedestroy($dest_img);
}
if (!$lob->savefile($imgfile)) {
...
The imagecreatefromjpeg() function reads the JPEG file and creates an
internal representation used by subsequent GD functions. Next, new dimensions
are calculated with the longest side no larger than 100 pixels. A template image
with the new size is created using imagecreatetruecolor(). Data from the
original image is sampled into it with imagecopyresampled() to create the
thumbnail. The thumbnail is written back to the original file and the internal
representations of the images are freed.
The existing code in db_insert_thumbnail() uploads the image file to the
database as it did in the previous section.
10. Enter the following URL in your browser to test the changes in your application:
http://localhost/~<username>/chap7/anyco.php
11. In the Departments page, navigate to the employees page by clicking Show
Employees: