Image Class
Can resize, animate, and convert any image. Includes a live demo. Resize your images for the web here!
Creates a new image object.
- Example:
<?php
$image = new Image;
?>
| bool url ( string or array $website [, integer $delay ] ) | top |
Opens an image resource from specified $website. This image will be used in all of the methods that follow, and so must be called first.
- $website:
- The url of the image you would like to work with. If this is an array then all images will be turned into one animated gif.
- $delay:
- The delay in 1/100ths of a second between images if you are making an animated gif. The default is '100' - ie. 1 second.
- Returns:
- True if successful, false if not.
- Example:
<?php
$image->url ('http://www.somesite.com/images/copy.jpg');
/* To make an animated gif:
$urls[] = 'http://www.mysite.com/frame0.jpg';
$urls[] = 'http://www.mysite.com/frame1.jpg';
$urls[] = 'http://www.mysite.com/frame2.gif';
// The frames can be of any image type. They are converted in house.
$image->url ($urls, array(100, 250, 25));
$image->resize (100); // A nice teaser thumbnail size.
$image->save ('/base/directory/mysite.com/images/animated.gif');
unset ($image);
*/ // Easy eh?
?>
This is so that you can know what you are working with.
- Returns:
- Either '.jpg', '.gif', or '.png'.
- Example:
<?php
$type = $image->type();
?>
| bool convert ( string $type ) | top |
If $image->url() is a gif, and you would like to covert it to a jpg for instance, you can do that here.
- $type:
- Either 'jpg', 'gif', or 'png'.
- Returns:
- True if successful, false if not.
- Example:
<?php
$image->convert ('jpg');
?>
| bool resize ( integer $width [, integer $height ] ) | top |
This will resize the specified image in $image->url() within the width and height specified while still maintaining its original proportions. You can call this method as many times as you like, and you will always be working with the original image.
- $width:
- The maximum width (in pixels) you would like this image to be. Default is 500.
- $height:
- The maximum height (in pixels) you would like this image to be. This value defaults to 80% of whatever width is specified.
- Returns:
- True if successful, false if not.
- Example:
<?php
$image->resize (500); // Takes the users massive 10 Megapixel image, and cuts it down to size for the web.
?>
| bool save ( string $location [, integer $quality= 80 ] ) | top |
This will save an image to the location on your server.
- $location:
- Where you would like the image to reside on your server. Be sure to include the full directory path.
- $quality:
- The quality of the image created from 0 (worst quality, smallest file) to 100 (best quality, biggest file). Default is 80.
- Returns:
- True if successful, false if not.
- Example:
<?php
$image->save ('/base/directory/mysite.com/images/copied.jpg')
?>
| bool display ( [, integer $quality= 80 ] ) | top |
This method outputs the image directly to the browser.
- $quality:
- The quality of the image created from 0 (worst quality, smallest file) to 100 (best quality, biggest file). Default is 80.
- Returns:
- True if successful, false if not.
- Example:
<?php
$image->display (80)
?>
This Image Class has marginal gif animation functionality. It is the best that can be done using only the GD Library of functions. If you are resizing an animated gif, it works really good if all of the individual frames are of equal dimensions, and they don't get fancy with the disposal method. But in case they do and you really want to make it work, you can extract (and optionally save) each individual frame.
- $num:
- The frame you would like to extract. Default is 0 - or the first frame in the set.
- $location:
- Where you would like the image to reside on your server. Be sure to include the full directory path. If you don't specify the location then the image will be output directly to the browser.
- Returns:
- True if successful, false if not.
- Example:
<?php
$image = new Image;
$image->url ('http://www.mysite.com/animated.gif');
$image->extract (); // The first frame will be output direct to the browser.
unset ($image);
?>
| An Advanced PHP for Beginners Example: | top |
<?php
$page = new Page;
$page->link('style.css');
$html = '';
$form = new Form;
$form->required(array());
$form->check(array('website'=>'url', 'width'=>'int', 'height'=>'int', 'quality'=>'int', 'convert'=>array('jpg', 'gif', 'png')));
$form->upload(array('upload'=>array('jpg', 'gif', 'png')), 5);
list($vars, $errors, $eject) = $form->validate ('image');
if (!empty($vars) && empty($errors)) {
$width = (empty($vars['width'])) ? 500 : $vars['width'];
$height = (empty($vars['height'])) ? 400 : $vars['height'];
$quality = (empty($vars['quality']) || $vars['quality'] > 100) ? 80 : $vars['quality'];
if (!empty($vars['website'])) {
$image = new Image;
if ($image->url($vars['website'])) {
if (!empty($vars['convert'])) $image->convert ($vars['convert']);
$image->resize ($width, $height);
$type = $image->type();
$image->save (BASE_URI . 'uploads/sample' . $type, $quality);
$info = getimagesize (BASE_URI . 'uploads/sample' . $type);
$html .= '<img style="background-color:orange;" src="/uploads/sample' . $type . '" ' . $info[3] . ' /><br />';
}
unset ($image);
} elseif (!empty($vars['upload'])) {
$image = new Image;
if ($image->url(BASE_URL . 'uploads/' . rawurlencode($vars['upload']))) {
if (!empty($vars['convert'])) $image->convert ($vars['convert']);
$image->resize ($width, $height);
$type = $image->type();
$image->save (BASE_URI . 'uploads/sample' . $type, $quality);
$info = getimagesize (BASE_URI . 'uploads/sample' . $type);
$html .= '<img style="background-color:orange;" src="/uploads/sample' . $type . '" ' . $info[3] . ' /><br />';
unlink (BASE_URI . 'uploads/' . $vars['upload']);
}
unset ($image);
}
$html .= '<pre>' . print_r($vars, true) . '</pre>';
// $page->eject ($eject, 'Sayonara Saddam!');
}
$html .= $form->header ('image');
$tb = new Table('cellpadding=5');
$html .= $tb->row();
$html .= $tb->cell('colspan=4', $form->field ('text', 'website', 'Website: ', array('size'=>50, 'maxlength'=>200)) . ' or . . .');
$html .= $tb->row();
$html .= $tb->cell('colspan=4', $form->field ('file', 'upload', 'Image: ', array('size'=>50)));
$html .= $tb->row('center');
$html .= $tb->cell('', $form->field ('text', 'width', 'Width:<br />', array('size'=>3, 'maxlength'=>3)));
$html .= $tb->cell('', $form->field ('text', 'height', 'Height:<br />', array('size'=>3, 'maxlength'=>3)));
$html .= $tb->cell('', $form->field ('text', 'quality', 'Quality:<br />', array('size'=>3, 'maxlength'=>3)));
$html .= $tb->cell('', $form->field ('select', 'convert', 'Convert:<br />', array('jpg'=>'jpg', 'gif'=>'gif', 'png'=>'png')));
$html .= $tb->row('center');
$html .= $tb->cell('colspan=4', $form->field ('submit', 'Submit'));
$html .= $tb->close();
$html .= $form->close();
unset ($tb, $form);
echo $page->display ($html);
?>