/php/lib/Gloo/Image/Helper.php
PHP | 157 lines | 81 code | 23 blank | 53 comment | 9 complexity | e3e66413c18e96eb99e7324af3b66cfd MD5 | raw file
- <?php
- /**
- *
- * @author rajeevj
- */
- class Gloo_Image_Helper {
- function __construct() {
- }
- /**
- *
- * @param <type> $imageURI - The URI on internet where this image is available
- * @return <type> - image binary data fetched from this URI
- *
- */
- static function getDataFromURI($imageURI) {
- //use curl to pull image data
- $ch = curl_init($imageURI);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
- $imageData = curl_exec($ch);
- curl_close($ch);
- return $imageData;
- }
- /**
- *
- * @param <type> $imageData - image binary data, stored as a string
- * @return <type> - Dimensions of this image binary
- *
- */
- static function getDimensions($imageData) {
- //created an image from raw data using GD2 library
- $oSourceImage = imagecreatefromstring($imageData);
- if ($oSourceImage == false) {
- //unrecoverable error
- $errorMsg = "GD2 : Not able to create source image from supplied file data ";
- trigger_error($errorMsg, E_USER_ERROR);
- }
- //original image dimensions
- $nWidth = imagesx($oSourceImage);
- $nHeight = imagesy($oSourceImage);
- $dimensions = array('height' => $nHeight, 'width' => $nWidth);
- return $dimensions;
- }
- /**
- *
- * @param <type> $width original image width
- * @param <type> $height original image height
- * @param <type> $frameWidth scale original image width to this width
- * @return <type> new dimensions
- *
- *
- */
- static function getScaledDimensions($width, $height, $frameWidth, $frameHeight=NULL) {
- if (empty($frameHeight)) {
- //Determine frame height from original aspect ratio
- $frameHeight = ($height / $width) * ($frameWidth);
- }
- //first try original dimensions for frame
- $newHeight = $height;
- $newWidth = $width;
- //calculate new height/ width using aspect-ratio
- if ($newHeight > $frameHeight) {
- $aspectRatio = ($frameHeight / $newHeight);
- $newHeight = $aspectRatio * $newHeight;
- $newWidth = $aspectRatio * $newWidth;
- }
- if ($newWidth > $frameWidth) {
- $aspectRatio = ($frameWidth / $newWidth);
- $newHeight = $aspectRatio * $newHeight;
- $newWidth = $aspectRatio * $newWidth;
- }
- //Round up to nearest integer
- $newWidth = floor($newWidth);
- $newHeight = floor($newHeight);
- $dimensions = array('width' => $newWidth, 'height' => $newHeight);
- return $dimensions;
- }
- /**
- *
- * Take an image binary and resize it to fit frame width and height
- *
- * @param <type> $sImageData source Image file binary data
- * @param <type> $frameWidth frame width to resize
- * @param <type> $frameHeight frame height to resize
- * @return <type> - New resized image binary
- *
- */
- static function getResizedImage($sImageData, $frameWidth, $frameHeight) {
- $oSourceImage = imagecreatefromstring($sImageData);
- if ($oSourceImage == false) {
- //unrecoverable error
- $errorMsg = "GD2 : Not able to create source image from supplied file data ";
- trigger_error($errorMsg, E_USER_ERROR);
- }
- //original width and height
- $nWidth = imagesx($oSourceImage);
- $nHeight = imagesy($oSourceImage);
- //we try original dimensions for frame
- $nDestinationHeight = $nHeight;
- $nDestinationWidth = $nWidth;
- //calculate new height/ width using aspect-ratio
- if ($nDestinationHeight > $frameHeight) {
- $aspectRatio = ($frameHeight / $nDestinationHeight);
- $nDestinationHeight = $aspectRatio * $nDestinationHeight;
- $nDestinationWidth = $aspectRatio * $nDestinationWidth;
- }
- if ($nDestinationWidth > $frameWidth) {
- $aspectRatio = ($frameWidth / $nDestinationWidth);
- $nDestinationHeight = $aspectRatio * $nDestinationHeight;
- $nDestinationWidth = $aspectRatio * $nDestinationWidth;
- }
- //Round up to nearest integer
- $nDestinationWidth = floor($nDestinationWidth);
- $nDestinationHeight = floor($nDestinationHeight);
- //an image identifier representing a blank image of specified size
- $oDestinationImage = imagecreatetruecolor($nDestinationWidth, $nDestinationHeight);
- //http://in2.php.net/manual/en/function.imagecopyresized.php
- // resize the image
- imagecopyresized($oDestinationImage,
- $oSourceImage, 0, 0, 0, 0,
- $nDestinationWidth, $nDestinationHeight,
- $nWidth, $nHeight);
- ob_start();
- //default is 75. is Quality 75 good enough for us?
- imageJPEG($oDestinationImage, NULL, 75);
- $sNewImage = ob_get_contents();
- ob_end_clean();
- // Free up memory
- imagedestroy($oDestinationImage);
- return $sNewImage;
- }
- }
- ?>