PHP Classes

File: functions.imageio.php

Recommend this page to a friend!
  Classes of Uku-Kaarel Jo~esaar   My Image Filter   functions.imageio.php   Download  
File: functions.imageio.php
Role: Auxiliary script
Content type: text/plain
Description: functions.imageio.php
Class: My Image Filter
Apply several types of effects on images
Author: By
Last change:
Date: 15 years ago
Size: 10,337 bytes
 

Contents

Class file image Download
<?php /** * Load existing image or create new truecolor image * * PHP 4.3.2 and GD 2.0.1 is reccomended but work on PHP 4.0.6? Tested on PHP 5.1.4 WXP. * * Always image convert to truecolor, if not * Trensparent color allocated with alpha channel 127. * * * @param mixed $imgres Image resource, width, path, string or string base64 encoded * @param array $imgres2 heigh or Transparent color * @return resource , bool FALSE */ function imagecreatefrom_( $imgres,$imgres2=0 ) { $imgresout = NULL;//output placeholder $imgres_ty = gettype( $imgres );//input data type $imgres_istc = FALSE;//is truecolor $imgres_cc = 0;//color count $functionname='imagecreatefrom_(): '; switch( $imgres_ty ) { case 'resource': $imgresout =& $imgres; break; case 'integer': // 4.0.6, GD 2.0.1 if($imgres<1) $imgres=1; elseif($imgres>3000) $imgres=3000; if($imgres<1) $imgres2=$imgres1; elseif($imgres2>3000) $imgres2=3000; $imgresout = imagecreatetruecolor($imgres,$imgres2); break; case 'string': //is_filename if( is_file($imgres) ) { if( is_readable( $imgres ) ) { if( $imgres_info = @getimagesize( $imgres ) ); else {$debugi= __LINE__; break; } $types= array( 1 => 'gif' ,2 => 'jpeg' ,3 => 'png', 6 => 'bmp' ,15 => 'wbmp' ,16 => 'xbm' ,17 => 'ico' ); $imgres_type = 'imagecreatefrom'.$types[$imgres_info[2]]; if( function_exists( $imgres_type ) ) { if( $imgresout = @$imgres_type($imgres) )break; else {$debugi= __LINE__; break; } } else {$debugi= __LINE__; break; } } else {$debugi= __LINE__; break; } } else { /* is_string_image imagecreatefromstring() supported types: $types2= array( 2=>'jpeg', 3=>'png', 1=>'gif', 15=>'wbmp', 18=>'gd2' ); */ if( $imgresout = @imagecreatefromstring($imgres) ); elseif( $imgresout = @imagecreatefromstring( base64_decode($imgres) )); else {$debugi= __LINE__; break; } } break; } //end switch input type if($debugi) return FALSE; //$debugi; if( function_exists('imageistruecolor')) { //PHP 4.3.2, GD 2.0.1 $imgres_istc = imageistruecolor( $imgresout ); } else { $imgres_cc = imagecolorstotal( $imgresout ); if( $imgres_cc <= 256 ) $imgres_istc = TRUE; else $imgres_istc = FALSE; } //mk_truecolor if( $imgres_istc === FALSE ) { $resW = imagesx( $imgresout ); $resH = imagesy( $imgresout ); // 4.0.6, GD 2.0.1 $im = @imagecreatetruecolor($resW, $resH); //truecolor variant of image: set_transparent/alpha $trcol_rgba=array(); $index_trcol = imagecolortransparent($imgresout); if($index_trcol>=0) $trcol_rgba = imagecolorsforindex($imgresout, $index_trcol); if( !empty( $trcol_rgba ) ) { if( function_exists('imagecolorallocatealpha') ) { // 4.0.6, GD 2.0.1 $transcoli = ImageColorResolveAlpha($im, $trcol_rgba['red'], $trcol_rgba['green'],$trcol_rgba['blue'], $trcol_rgba['alpha'] ); } else { $transcoli = imagecolorresolve($im, $trcol_rgba['red'],$trcol_rgba['green'],$trcol_rgba['blue'] ); } ImageColorTransparent( $im, $transcoli ); imagefill($im, 0, 0, $transcoli); } // 4.0.6 if(function_exists( 'imagecopymerge' )) @imagecopymerge ( $im ,$imgresout, 0,0,0,0, $resW,$resH, 100 ); else @imagecopy($im,$imgresout, 0,0,0,0, $resW,$resH); imagedestroy($imgresout); $imgresout =& $im; } //return return $imgresout; } //end imagecreatefrom_() /** * Output interlaced/progressive image * PNG is default * * Usage: * * @param resource image * @param string $f filetype: gif,jpg,jpeg,jpe,png,bmp,xbm,wbm,gd,gd2 * @param string $fp filepath * @param mixed $param optional parameter for jpeg,xbm,wbm.. * @return mixed or boolean */ function image___(&$imgres, $f='png',$fp='',$param=NULL) { if(!is_resource( $imgres ))return FALSE; $f=strtolower($f); if($f=='jpg' or $f=='jpe' or $f=='jp')$f='jpeg'; elseif($f=='tif' or $f=='tiff')$f='png'; if( !function_exists( "image$f" ) ) { $f='png'; } if($fp!='') { $fp=str_replace("\\",'/',$fp); if(strstr($fp,'/')=='' ){ $fp .= 'image.'.$f; } $imagename = basename($fp); } else { $imagename = 'image.' . $f; } $jpegquality = 85; if( $f=='jpeg' ) { $param = (int) $param; if($param>100)$jpegquality=100; elseif($param<20)$jpegquality=20; else $jpegquality=$param; } elseif ($param=='') { $param = 0; } // header('Content-Length: '.$size); $cts=array( 'gif'=>'image/gif' ,'jpeg'=>'image/jpeg' ,'png'=>'image/png' ,'bmp'=>'image/bmp' ,'xbm'=>'image/xbm' ,'wbmp'=>'image/vnd.wap.wbmp' //,'ico'=>'image/x-icon' ); // image... function name $pfu=array( 'gif'=>'gif' ,'jpeg'=>'jpeg' ,'png'=>'png' ,'bmp'=>'bmp' ,'xbm'=>'xbm' ,'wbmp'=>'wbmp' //,'ico'=>'ico' ); // image out function 3' parameter $p3= array( 'gif'=>'' ,'jpeg'=> ','.$jpegquality ,'png'=>'' ,'bmp'=> ','. $param ,'xbm'=> ','.$param ,'wbmp'=> ','.$param //,'ico'=>'' ); $return = FALSE; $image_fun = 'image' . $pfu[$f]; if( function_exists( 'imageinterlace' ) ) @imageinterlace($imgres,1); if($fp=='') { $useragent = strtolower( $_SERVER['HTTP_USER_AGENT'] ); if( strpos($useragent,'mozilla')!==FALSE or strpos($useragent,'gecko')!==FALSE or strpos($useragent,'firefox')!==FALSE or strpos($useragent,'netscape')!==FALSE );else header('Content-Disposition: attachment; filename='.$imagename); header('Content-type: '.$cts[$f]); if($p3[$f]=='') { eval("\$return = @".$image_fun."(\$imgres); "); } else { eval("\$return = @".$image_fun."(\$imgres ,'' ".$p3[$f]." ); "); } } else { if($p3[$f]=='') { //echo "IIK1 " . $image_fun .' '. $fp . ' ' . $p3[$f]; eval("\$return = @".$image_fun."(\$imgres , \$fp ); "); } else { //echo "IIK2 " . $image_fun .' '. $fp . ' ' . $p3[$f]; eval("\$return = @".$image_fun."(\$imgres , \$fp ".$p3[$f]." ); "); } } return $return; } /** * Output image to browser or file * * Based on imageBmp method cropped from class GifSplit(2006-07-09) http://www.phpclasses.org/ * http://gifs.hu * Created by László Zsidi * zsidi.laszlo@chello.hu * * AND * * Another similar is class bmp_image (2003-05-23) * author : troels@kyberfabrikken.dk * * Usage: * ------ * imagebmp($img, $file='', $RLE=0); * * $img = imagecreatefromgif( '../reader-icon-2002.gif' ); * header('Content-type: image/bmp'); * imagebmp($img,'',1); * * @param resource $img * @param string $file To skip the file argument in order to provide a RLE argument just use an empty string (''). * @param int $RLE 0|1 * @return bool * @author Uku-Kaarel J&otilde;esaar http://ukj.pri.ee */ function imagebmp($img, $file='', $RLE=0) { if(!is_resource( $img )) return FALSE; $int_to_word = create_function( "\$n", "return chr(\$n & 255).chr((\$n >> 8) & 255);" ); $int_to_dword = create_function("\$n", "return chr(\$n & 255).chr((\$n >> 8) & 255).chr((\$n >> 16) & 255).chr((\$n >> 24) & 255);" ); $decbinx = create_function("\$d,\$n", "\$bin = decbin(\$d);\$sbin = strlen(\$bin);for(\$j = 0;\$j<\$n-\$sbin;\$j++)\$bin=\"0\$bin\";return \$bin;"); $ColorCount = imagecolorstotal($img); $Transparent = imagecolortransparent($img); $IsTransparent = $Transparent != -1; if($IsTransparent) $ColorCount--; if($ColorCount == 0) { $ColorCount = 0; $BitCount = 24; } if(($ColorCount > 0) && ($ColorCount <= 2)) { $ColorCount = 2; $BitCount = 1; } if(($ColorCount > 2) && ($ColorCount <= 16)) { $ColorCount = 16; $BitCount = 4; } if(($ColorCount > 16) && ($ColorCount <= 256)) { $ColorCount = 0; $BitCount = 8; } $Width = imageSX($img); $Height = imageSY($img); $Zbytek = (4 - ($Width / (8 / $BitCount)) % 4) % 4; if($BitCount < 24) $palsize = pow(2, $BitCount) * 4; $size = (floor($Width / (8 / $BitCount)) + $Zbytek) * $Height + 54; $size += $palsize; $offset = 54 + $palsize; // Bitmap File Header $ret = 'BM'; $ret .= $int_to_dword($size); $ret .= $int_to_dword(0); $ret .= $int_to_dword($offset); // Bitmap Info Header $ret .= $int_to_dword(40); $ret .= $int_to_dword($Width); $ret .= $int_to_dword($Height); $ret .= $int_to_word(1); $ret .= $int_to_word($BitCount); $ret .= $int_to_dword($RLE); $ret .= $int_to_dword(0); $ret .= $int_to_dword(0); $ret .= $int_to_dword(0); $ret .= $int_to_dword(0); $ret .= $int_to_dword(0); // image data $CC = $ColorCount; $sl1 = strlen($ret); if($CC == 0) $CC = 256; if($BitCount < 24) { $ColorTotal = imagecolorstotal($img); if($IsTransparent) $ColorTotal--; for($p = 0; $p < $ColorTotal; $p++) { $color = imagecolorsforindex($img, $p); $ret .= chr($color["blue"]); $ret .= chr($color["green"]); $ret .= chr($color["red"]); $ret .= chr(0); } $CT = $ColorTotal; for($p = $ColorTotal; $p < $CC; $p++) { $ret .= chr(0); $ret .= chr(0); $ret .= chr(0); $ret .= chr(0); } } if($BitCount <= 8) { for($y = $Height - 1; $y >= 0; $y--) { $bWrite = ""; for($x = 0; $x < $Width; $x++) { $color = imagecolorat($img, $x, $y); $bWrite .= $decbinx($color, $BitCount); if(strlen($bWrite) == 8) { $retd .= chr(bindec($bWrite)); $bWrite = ""; } } if((strlen($bWrite) < 8) and (strlen($bWrite) != 0)) { $sl = strlen($bWrite); for($t = 0; $t < 8 - $sl; $t++) $sl .= "0"; $retd .= chr(bindec($bWrite)); } for($z = 0; $z < $Zbytek; $z++) $retd .= chr(0); } } if(($RLE == 1) and ($BitCount == 8)) { for($t = 0; $t < strlen($retd); $t += 4) { if($t != 0) if(($t) % $Width == 0) $ret .= chr(0).chr(0); if(($t + 5) % $Width == 0) { $ret .= chr(0).chr(5).substr($retd, $t, 5).chr(0); $t += 1; } if(($t + 6) % $Width == 0) { $ret .= chr(0).chr(6).substr($retd, $t, 6); $t += 2; } else $ret .= chr(0).chr(4).substr($retd, $t, 4); } $ret .= chr(0).chr(1); } else $ret .= $retd; if($BitCount == 24) { for($z = 0; $z < $Zbytek; $z++) $Dopl .= chr(0); for($y = $Height - 1; $y >= 0; $y--) { for($x = 0; $x < $Width; $x++) { $color = imagecolorsforindex($img, ImageColorAt($img, $x, $y)); $ret .= chr($color["blue"]).chr($color["green"]).chr($color["red"]); } $ret .= $Dopl; } } if(!$file) echo $ret; else if(fwrite(fopen($file, "wb"), $ret)) return true; else return false; return true; } ?>