One step closer to world domination
Posts tagged GD
PHP photo resizing class example
Jan 12th
Here is an example of a PHP class that manages simple photo resizing and saving using the GD library.
<?php /**************************************************** * * Purpose: Photo Resizing Class * Author: Matt West, http://mattdanger.net * Date: January 12th, 2007 * ****************************************************/ class Photo { // Private variables private $orig_image; private $portrait_frame; // 1 = Landscape, 2 = Portrait private $image_size; private $src_width; private $src_height; private $src_image; private $dest_height; private $dest_width; private $dest_image; private $ratio; private $scale; // Public variables public $allowed_types = array('jpeg', 'jpg', 'png', 'gif'); /** * Constructor: Contruct the photo object * @param array $photo Array of photo data, usually $_FILES['photo_name'] */ public function __construct($photo) { $this->photo($photo); } /** * photo(): Create new image resource * @param string $photo Array of photo data */ public function photo($photo) { $this->orig_image = $photo; // Confirm photo is an allowed type; if ( !in_array(ereg_replace('image/', '', $this->orig_image['type']), $this->allowed_types) ) { exit("Photo type is '" . $this->orig_image['type'] . "' which is not allowed"); } // Get image X and Y values in order to determine photo framing $this->image_size = getimagesize($this->orig_image['tmp_name']); $this->src_width = $this->image_size[0]; $this->src_height = $this->image_size[1]; $this->dest_height = 0; // 1 = Landscape, 2 = Portrait $this->photo_framing = ($this->src_width > $this->src_height) ? 1 : 2 ; // Calculate the ratio at which to resize based on the dimentions $this->ratio = $this->src_width / $this->src_height; } /** * scale_to_width(): Sets the width of the resize value * @param int $width Width in pixels * @returns boolean Returns true if successful, false if it was not. */ public function scale_to_width($width) { // Set the destination width value $this->dest_width = $width; // Calculate the value at which to scale the image $this->scale = ($this->ratio) ? $this->dest_width / $this->src_width : $this->dest_height / $this->src_height; if ($this->scale_image()) { return true; } else { return false; } } /** * scale_to_height(): Sets the height of the resize value * @param int $height Height in pixels * @returns boolean Returns true if successful, false if it was not. */ public function scale_to_height($height) { // Set the destination height value $this->dest_height = $height; // Calculate the ratio at which to resize based on the dimentions $this->ratio = $this->src_width / $this->src_height; // Calculate the value at which to scale the image $this->scale = ($this->ratio) ? $this->dest_height / $this->src_height : $this->dest_width / $this->src_width ; if ($this->scale_image()) { return true; } else { return false; } } /** * scale_image(): Resize image proportionally * @returns boolean Returns true if successful, false if it was not. */ private function scale_image () { if ($this->scale > 1) { exit ("The scale width supplied is larger than the original image. Please try a smaller number."); } $this->dest_width = $this->src_width * $this->scale; $this->dest_height = $this->src_height * $this->scale; if ( $this->dest_width >= $this->src_width && $this->dest_width >= $this->src_width ) $this->scale = 1; $this->src_image = imagecreatefromjpeg ($this->orig_image['tmp_name']); $this->dest_image = imagecreatetruecolor ($this->src_width * $this->scale, $this->src_height * $this->scale); if ( imagecopyresampled( $this->dest_image, $this->src_image, 0, 0, 0, 0, $this->dest_width, $this->dest_height, $this->src_width, $this->src_height) ) { return true; } else { return false; } } /** * save_photo(): Saves the resized photo * @param string $filename The path and filename to save the file (ex: "/path/to/image.jpg") * @param int $compression A number from 1-100 representing the level of compression. 100 is the least compression. * @returns boolean Returns true if save was successful, false if it was not. */ public function save_photo ($filename, $compression = 80) { if ( imagejpeg($this->dest_image, $filename, $compression) ) { $return_value = true; } else { $return_value = false; } imagedestroy($this->src_image); imagedestroy($this->dest_image); return $return_value; } } ?>










