$memoryLimitMB) { $newLimit = $memoryLimit + ceil( ( memory_get_usage() + $memoryNeeded - $memoryLimitMB ) / $MB ); if (@ini_set( 'memory_limit', $newLimit . 'M' ) === false) { return false; } } } else { if ($memoryNeeded + 3*$MB > $memoryLimitMB) { $newLimit = $memoryLimit + ceil(( 3*$MB + $memoryNeeded - $memoryLimitMB ) / $MB ); if (false === @ini_set( 'memory_limit', $newLimit . 'M' )) { return false; } } } return true; } /** * convert shorthand php.ini notation into bytes, much like how the PHP source does it * @link http://pl.php.net/manual/en/function.ini-get.php * * @static * @access public * @param string $val * @return int */ public static function returnBytes($val) { $val = trim($val); if (!$val) { return 0; } $last = strtolower($val[strlen($val)-1]); switch($last) { // The 'G' modifier is available since PHP 5.1.0 case 'g': $val *= 1024; case 'm': $val *= 1024; case 'k': $val *= 1024; } return $val; } /** * Checks if a value exists in an array (case insensitive) * * @static * @access public * @param string $needle * @param array $haystack * @return boolean */ public static function inArrayCaseInsensitive($needle, $haystack) { if (!$haystack || !is_array($haystack)) { return false; } $lcase = array(); foreach ($haystack as $key => $val) { $lcase[$key] = strtolower($val); } return in_array($needle, $lcase); } /** * UTF-8 compatible version of basename() * * @static * @access public * @param string $file * @return string */ public static function mbBasename($file) { $explode = explode('/', str_replace("\\", "/", $file)); return end($explode); } /** * Checks whether the string is valid UTF8 * @static * @access public * @param string $string * @return boolean */ public static function isValidUTF8($string) { if (strlen($string) == 0) { return true; } return (preg_match('/^./us', $string) == 1); } /** * Source: http://pl.php.net/imagecreate * (optimized for speed and memory usage, but yet not very efficient) * * @static * @access public * @param string $filename * @return resource */ public static function imageCreateFromBmp($filename) { //20 seconds seems to be a reasonable value to not kill a server and process images up to 1680x1050 @set_time_limit(20); if (false === ($f1 = fopen($filename, "rb"))) { return false; } $FILE = unpack("vfile_type/Vfile_size/Vreserved/Vbitmap_offset", fread($f1, 14)); if ($FILE['file_type'] != 19778) { return false; } $BMP = unpack('Vheader_size/Vwidth/Vheight/vplanes/vbits_per_pixel'. '/Vcompression/Vsize_bitmap/Vhoriz_resolution'. '/Vvert_resolution/Vcolors_used/Vcolors_important', fread($f1, 40)); $BMP['colors'] = pow(2,$BMP['bits_per_pixel']); if ($BMP['size_bitmap'] == 0) { $BMP['size_bitmap'] = $FILE['file_size'] - $FILE['bitmap_offset']; } $BMP['bytes_per_pixel'] = $BMP['bits_per_pixel']/8; $BMP['bytes_per_pixel2'] = ceil($BMP['bytes_per_pixel']); $BMP['decal'] = ($BMP['width']*$BMP['bytes_per_pixel']/4); $BMP['decal'] -= floor($BMP['width']*$BMP['bytes_per_pixel']/4); $BMP['decal'] = 4-(4*$BMP['decal']); if ($BMP['decal'] == 4) { $BMP['decal'] = 0; } $PALETTE = array(); if ($BMP['colors'] < 16777216) { $PALETTE = unpack('V'.$BMP['colors'], fread($f1, $BMP['colors']*4)); } //2048x1536px@24bit don't even try to process larger files as it will probably fail if ($BMP['size_bitmap'] > 3 * 2048 * 1536) { return false; } $IMG = fread($f1, $BMP['size_bitmap']); fclose($f1); $VIDE = chr(0); $res = imagecreatetruecolor($BMP['width'],$BMP['height']); $P = 0; $Y = $BMP['height']-1; $line_length = $BMP['bytes_per_pixel']*$BMP['width']; if ($BMP['bits_per_pixel'] == 24) { while ($Y >= 0) { $X=0; $temp = unpack( "C*", substr($IMG, $P, $line_length)); while ($X < $BMP['width']) { $offset = $X*3; imagesetpixel($res, $X++, $Y, ($temp[$offset+3] << 16) + ($temp[$offset+2] << 8) + $temp[$offset+1]); } $Y--; $P += $line_length + $BMP['decal']; } } elseif ($BMP['bits_per_pixel'] == 8) { while ($Y >= 0) { $X=0; $temp = unpack( "C*", substr($IMG, $P, $line_length)); while ($X < $BMP['width']) { imagesetpixel($res, $X++, $Y, $PALETTE[$temp[$X] +1]); } $Y--; $P += $line_length + $BMP['decal']; } } elseif ($BMP['bits_per_pixel'] == 4) { while ($Y >= 0) { $X=0; $i = 1; $low = true; $temp = unpack( "C*", substr($IMG, $P, $line_length)); while ($X < $BMP['width']) { if ($low) { $index = $temp[$i] >> 4; } else { $index = $temp[$i++] & 0x0F; } $low = !$low; imagesetpixel($res, $X++, $Y, $PALETTE[$index +1]); } $Y--; $P += $line_length + $BMP['decal']; } } elseif ($BMP['bits_per_pixel'] == 1) { $COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1)); if (($P*8)%8 == 0) $COLOR[1] = $COLOR[1] >>7; elseif (($P*8)%8 == 1) $COLOR[1] = ($COLOR[1] & 0x40)>>6; elseif (($P*8)%8 == 2) $COLOR[1] = ($COLOR[1] & 0x20)>>5; elseif (($P*8)%8 == 3) $COLOR[1] = ($COLOR[1] & 0x10)>>4; elseif (($P*8)%8 == 4) $COLOR[1] = ($COLOR[1] & 0x8)>>3; elseif (($P*8)%8 == 5) $COLOR[1] = ($COLOR[1] & 0x4)>>2; elseif (($P*8)%8 == 6) $COLOR[1] = ($COLOR[1] & 0x2)>>1; elseif (($P*8)%8 == 7) $COLOR[1] = ($COLOR[1] & 0x1); $COLOR[1] = $PALETTE[$COLOR[1]+1]; } else { return false; } return $res; } }