liberty
[ class tree: liberty ] [ index: liberty ] [ all elements ]

Source for file processor.gd.php

Documentation is available at processor.gd.php

  1. <?php
  2. /**
  3.  * $Header$
  4.  *
  5.  * Image processor - extension: php-gd
  6.  * @package  liberty
  7.  * @subpackage plugins_processor
  8.  * @author   spider <spider@steelsun.com>
  9.  */
  10.  
  11. /**
  12.  * liberty_gd_resize_image
  13.  *
  14.  * @param array $pFileHash 
  15.  * @access public
  16.  * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  17.  */
  18. function liberty_gd_resize_image( &$pFileHash ) {
  19.     global $gBitSystem;
  20.     $ret = NULL;
  21.     list($iwidth, $iheight, $itype, $iattr) = @getimagesize( $pFileHash['source_file'] );
  22.     list($type, $ext) = explode( '/', strtolower( $pFileHash['type'] ) );
  23.     if( ( empty( $pFileHash['max_width'] ) || empty( $pFileHash['max_height'] ) ) || ( $iwidth <= $pFileHash['max_width'] && $iheight <= $pFileHash['max_height'] && ( $ext == 'gif' || $ext == 'png'  || $ext == 'jpg'   || $ext == 'jpeg' ) ) ) {
  24.         // Keep the same dimensions as input file
  25.         $pFileHash['max_width'] = $iwidth;
  26.         $pFileHash['max_height'] = $iheight;
  27.     } elseif( $iheight && (($iwidth / $iheight) > 0) && !empty( $pFileHash['max_width'] ) && !empty( $pFileHash['max_height'] ) ) {
  28.         // we have a portrait image, flip everything
  29.         $temp = $pFileHash['max_width'];
  30.         $pFileHash['max_height'] = $pFileHash['max_width'];
  31.         $pFileHash['max_width'] = $temp;
  32.     }
  33.  
  34.     // we need to scale and/or reformat
  35.     $fp = fopen( $pFileHash['source_file'], "rb" );
  36.     $data = fread( $fp, filesize( $pFileHash['source_file'] ) );
  37.     fclose ($fp);
  38.     if( function_exists( "ImageCreateFromString" ) ) {
  39.         $img = @imagecreatefromstring($data);
  40.     }
  41.  
  42.     if( !empty( $img ) ) {
  43.         $size_x = imagesx($img);
  44.         $size_y = imagesy($img);
  45.     }
  46.  
  47.     if( !empty( $img ) && $size_x && $size_y ) {
  48.         if( $size_x > $size_y && !empty( $pFileHash['max_width'] ) ) {
  49.             $tscale = ( (int)$size_x / $pFileHash['max_width'] );
  50.         } elseif( !empty( $pFileHash['max_height'] ) ) {
  51.             $tscale = ( (int)$size_y / $pFileHash['max_height'] );
  52.         } else {
  53.             $tscale = 1;
  54.         }
  55.         $tw = ( (int)( $size_x / $tscale ));
  56.         $ty = ( (int)( $size_y / $tscale ));
  57.         if( get_gd_version() > 1 ) {
  58.             $t = imagecreatetruecolor( $tw, $ty );
  59.             imagesavealpha( $t, TRUE );
  60.             imagealphablending( $t, FALSE );
  61.             imagecopyresampled( $t, $img, 0, 0, 0, 0, $tw, $ty, $size_x, $size_y );
  62.         } else {
  63.             $t = imagecreate( $tw, $ty );
  64.             //$imagegallib->ImageCopyResampleBicubic($t, $img, 0, 0, 0, 0, $tw, $ty, $size_x, $size_y);
  65.         }
  66.  
  67.  
  68.         // override $mimeExt if we have a custom setting for it
  69.         if( $gBitSystem->isFeatureActive( 'liberty_thumbnail_format' )) {
  70.             $mimeExt = $gBitSystem->getConfig( 'liberty_thumbnail_format' );
  71.         } else {
  72.             // make sure we have image_type_to_extension available
  73.             include_once( UTIL_PKG_PATH.'PHP_Compat/Compat/Function/image_type_to_mime_type.php' );
  74.             list( $type, $mimeExt ) = explode( '/', strtolower( image_type_to_mime_type( $itype )));
  75.         }
  76.  
  77.         if( $mimeExt = preg_replace( "!^(x-)?(jpeg|png|gif)$!", "$2", $mimeExt )) {
  78.             $targetType = $mimeExt;
  79.             $destExt = '.'.$mimeExt;
  80.         }
  81.         if( !$mimeExt || $mimeExt == 'jpeg' ) {
  82.             $targetType = 'jpeg';
  83.             $destExt = '.jpg';
  84.         }
  85.  
  86.         if( !empty( $pFileHash['dest_file'] ) ) {
  87.             $destFile = $pFileHash['dest_file'];
  88.         } else {
  89.             $destFile = STORAGE_PKG_PATH.$pFileHash['dest_branch'].$pFileHash['dest_base_name'].$destExt;
  90.         }
  91.  
  92.         switch( $targetType ) {
  93.             case 'png':
  94.                 if( imagetypes() & IMG_PNG ) {
  95.                     // png alpha stuff - needs more testing - spider
  96.                     //     imagecolorallocatealpha ( $t, 0, 0, 0, 127 );
  97.                     //     $ImgWhite = imagecolorallocate($t, 255, 255, 255);
  98.                     //     imagefill($t, 0, 0, $ImgWhite);
  99.                     //     imagecolortransparent($t, $ImgWhite);
  100.                     imagepng( $t, $destFile );
  101.                     break;
  102.                 }
  103.             case 'gif':
  104.                 // This must go immediately before default so default will be hit for PHP's without gif support
  105.                 if( imagetypes() & IMG_GIF ) {
  106.                     imagecolortransparent( $t );
  107.                     imagegif( $t, $destFile );
  108.                     break;
  109.                 }
  110.             default:
  111.                 imagejpeg( $t, $destFile );
  112.                 break;
  113.         }
  114.  
  115.         // set permissions if possible - necessary for some wonky shared hosting environments
  116.         if( chmod( $pFileHash['source_file'], 0644 )){
  117.             // does nothing, but fails elegantly
  118.         }
  119.  
  120.         $pFileHash['name'] = $pFileHash['dest_base_name'].$destExt;
  121.         $pFileHash['size'] = filesize( $destFile );
  122.         $ret = $destFile;
  123.     } elseif( $iwidth && $iheight ) {
  124.         $ret = liberty_process_generic( $pFileHash, FALSE );
  125.     }
  126.  
  127.     return $ret;
  128. }
  129.  
  130. /**
  131.  * liberty_gd_rotate_image
  132.  *
  133.  * @param array $pFileHash 
  134.  * @param array $pFormat 
  135.  * @access public
  136.  * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  137.  */
  138. function liberty_gd_rotate_image( &$pFileHash, $pFormat = NULL ) {
  139.     if( !function_exists( 'imagerotate' ) ) {
  140.         $pFileHash['error'] = "Rotate is not available on this webserver.";
  141.     } elseif( empty( $pFileHash['degrees'] ) || !is_numeric( $pFileHash['degrees'] ) ) {
  142.         $pFileHash['error'] = tra( 'Invalid rotation amount' );
  143.     } else {
  144.         // we need to scale and/or reformat
  145.         $fp = fopen( $pFileHash['source_file'], "rb" );
  146.         $data = fread( $fp, filesize( $pFileHash['source_file'] ) );
  147.         fclose ($fp);
  148.         if( function_exists("ImageCreateFromString") ) {
  149.             $img = @imagecreatefromstring($data);
  150.         }
  151.  
  152.         if( !empty( $img ) ) {
  153.             // image rotate degrees seems back ass words.
  154.             $rotateImg = imagerotate ( $img, (-1 * $pFileHash['degrees']), 0 );
  155.             if( !empty( $rotateImg ) ) {
  156.                 imagejpeg( $rotateImg, $pFileHash['source_file'] );
  157.             } else {
  158.                 $pFileHash['error'] = "Image rotation failed.";
  159.             }
  160.         } else {
  161.             $pFileHash['error'] = "Image could not be opened for rotation.";
  162.         }
  163.     }
  164.  
  165.     return( empty( $pFileHash['error'] ) );
  166. }
  167.  
  168. /**
  169.  * liberty_gd_can_thumbnail_image
  170.  *
  171.  * @param array $pMimeType 
  172.  * @access public
  173.  * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  174.  */
  175. function liberty_gd_can_thumbnail_image( $pMimeType ) {
  176.     $ret = FALSE;
  177.     if( !empty( $pMimeType )) {
  178.         $ret = preg_match( '/^image/i', $pMimeType );
  179.     }
  180.     return $ret;
  181. }
  182.  
  183. /**
  184.  * get_gd_version
  185.  * 
  186.  * @access public
  187.  * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  188.  */
  189. function get_gd_version( $pFullVersion = FALSE ) {
  190.     if( empty( $_SESSION['gd_version'] )) {
  191.         $gd = gd_info();
  192.         $_SESSION['gd_version'] = preg_replace( "!\D*([\d|\.]*)!", "$1", $gd['GD Version'] );
  193.     }
  194.  
  195.     if( $pFullVersion ) {
  196.         return $_SESSION['gd_version'];
  197.     } else {
  198.         return preg_replace( "!^(\d)+.*$!", "$1", $_SESSION['gd_version'] );
  199.     }
  200. }
  201.  
  202. // nicked from http://at2.php.net/manual/en/function.gd-info.php
  203. if( !function_exists( 'gd_info' )) {
  204.     function gd_info() {
  205.         $array = Array(
  206.             "GD Version"         => "",
  207.             "FreeType Support"   => 0,
  208.             "FreeType Support"   => 0,
  209.             "FreeType Linkage"   => "",
  210.             "T1Lib Support"      => 0,
  211.             "GIF Read Support"   => 0,
  212.             "GIF Create Support" => 0,
  213.             "JPG Support"        => 0,
  214.             "PNG Support"        => 0,
  215.             "WBMP Support"       => 0,
  216.             "XBM Support"        => 0
  217.         );
  218.         $gif_support = 0;
  219.  
  220.         ob_start();
  221.         eval("phpinfo();");
  222.         $info = ob_get_contents();
  223.         ob_end_clean();
  224.  
  225.         foreach(explode("\n", $info) as $line) {
  226.             if(strpos($line, "GD Version")!==false)
  227.                 $array["GD Version"] = trim(str_replace("GD Version", "", strip_tags($line)));
  228.             if(strpos($line, "FreeType Support")!==false)
  229.                 $array["FreeType Support"] = trim(str_replace("FreeType Support", "", strip_tags($line)));
  230.             if(strpos($line, "FreeType Linkage")!==false)
  231.                 $array["FreeType Linkage"] = trim(str_replace("FreeType Linkage", "", strip_tags($line)));
  232.             if(strpos($line, "T1Lib Support")!==false)
  233.                 $array["T1Lib Support"] = trim(str_replace("T1Lib Support", "", strip_tags($line)));
  234.             if(strpos($line, "GIF Read Support")!==false)
  235.                 $array["GIF Read Support"] = trim(str_replace("GIF Read Support", "", strip_tags($line)));
  236.             if(strpos($line, "GIF Create Support")!==false)
  237.                 $array["GIF Create Support"] = trim(str_replace("GIF Create Support", "", strip_tags($line)));
  238.             if(strpos($line, "GIF Support")!==false)
  239.                 $gif_support = trim(str_replace("GIF Support", "", strip_tags($line)));
  240.             if(strpos($line, "JPG Support")!==false)
  241.                 $array["JPG Support"] = trim(str_replace("JPG Support", "", strip_tags($line)));
  242.             if(strpos($line, "PNG Support")!==false)
  243.                 $array["PNG Support"] = trim(str_replace("PNG Support", "", strip_tags($line)));
  244.             if(strpos($line, "WBMP Support")!==false)
  245.                 $array["WBMP Support"] = trim(str_replace("WBMP Support", "", strip_tags($line)));
  246.             if(strpos($line, "XBM Support")!==false)
  247.                 $array["XBM Support"] = trim(str_replace("XBM Support", "", strip_tags($line)));
  248.         }
  249.  
  250.         if($gif_support==="enabled") {
  251.             $array["GIF Read Support"]   = 1;
  252.             $array["GIF Create Support"] = 1;
  253.         }
  254.  
  255.         if($array["FreeType Support"]==="enabled")
  256.             $array["FreeType Support"] = 1;
  257.  
  258.         if($array["T1Lib Support"]==="enabled")
  259.             $array["T1Lib Support"] = 1;
  260.  
  261.         if($array["GIF Read Support"]==="enabled")
  262.             $array["GIF Read Support"] = 1;
  263.  
  264.         if($array["GIF Create Support"]==="enabled")
  265.             $array["GIF Create Support"] = 1;
  266.  
  267.         if($array["JPG Support"]==="enabled")
  268.             $array["JPG Support"] = 1;
  269.  
  270.         if($array["PNG Support"]==="enabled")
  271.             $array["PNG Support"] = 1;
  272.  
  273.         if($array["WBMP Support"]==="enabled")
  274.             $array["WBMP Support"] = 1;
  275.  
  276.         if($array["XBM Support"]==="enabled")
  277.             $array["XBM Support"] = 1;
  278.  
  279.         return $array;
  280.     }
  281. }
  282. ?>

Documentation generated on Wed, 29 Jul 2015 13:57:16 +0000 by phpDocumentor 1.5.0-lsces