// // TraiteImage.php.txt // //VGR15062004 Creation from CopieImage() from BDniouzes - www.bdniouzes.net - //VGR16062004 MOD for logging invalid images references (or access problems) in stead of echoing to browser //VGR09112004 MOD for thumbnail generation on-the-fly //VGR09082005 FIXed transparency "black GIF" problem //VGR10082005 MOD to not resize GIFs //VGR18052006 MOD cleaned the code a bit to show only the function on expertsrt.net // function TraiteImage($nameprefix,$distfile,$minimumwidth,$maxheight,$warninglimit,$theref,&$localfname,&$b,&$validrefs,&$nbimgtooheavy,&$nbimgtoonarrow,&$nbimgtoohigh,&$nbimgok,&$nbimgmissing,&$nbbadtype) { // : Boolean /* algorithm : // file is defined. Now ensure that : // - it has correct type as per extension // - it is reachable and readable // - it has a non-zero size // - it has correct type as per ImageInfo() functions // now generate thumbnails and main image if type OK // - check dimensions before resizing // generate image & thumbnails // resize via resampling and store thumbnail // also make sure maxw and maxh are not reached */ // initialize $okimage=FALSE; $i=$GLOBALS['i']; $imagesrefs=$GLOBALS['imagesrefs']; $DORESIZE=(isset($GLOBALS['DORESIZE']))?$GLOBALS['DORESIZE']:TRUE; // work now $go=(($distfile<>"") AND isset($imagesrefs[$i]['filepath'])); if ($go) $go=($imagesrefs[$i]['filepath']<>""); if ($go) { if ($nameprefix=='FLOORPLAN') $GLOBALS['imagesrefs'][$i]['filepath']='images/floorplans'; //VGR20082009 REM adapt to your needs $filepath=$GLOBALS['imagesrefs'][$i]['filepath']; // algo : // file is defined. Now ensure that : // - it has correct type as per extension $ww=explode('.',$distfile); $last=$ww[count($ww)-1]; switch(strtolower($last)) { case 'jpg' : case 'jpeg' : $ok1='jpg'; break; case 'gif' : $ok1='gif'; break; case 'png' : $ok1='png'; break; default : $ok1='unk'; break; } if ($ok1<>'unk') { // - it is reachable and readable unset($ww[count($ww)-1]); $ww=explode('_',implode('.',$ww)); $last=(isset($ww[2]))?$ww[2]:'1'; $localfname=$theref.'_'."$last.$ok1"; $nfile="$filepath/TEMPO.{$ok1}"; if (CopieImage($distfile,$nfile,FALSE)) { // do not check if pre-exists LogMe("IMAGE OK : copied $nameprefix $distfile into $nfile"); // - it has a non-zero size $size=@filesize($nfile); if ($size!==FALSE) if ($size>0) { // - it has correct type as per ImageInfo() functions $info=@getimagesize($nfile); // tableau:0=largeur,1=hauteur,2=type,3='width= heigh=' ;-) if ($info!==FALSE) { $loctype=$info[2]; // get textual description switch($loctype) { case 1 : $letype='GIF'; break; case 2 : $letype='JPG'; break; case 3 : $letype='PNG'; break; case 4 : $letype='SWF'; break; case 5 : $letype='PSD'; break; case 6 : $letype='BMP'; break; case 7 : $letype='TIFF'; break; case 8 : $letype='TIFF'; break; case 9 : $letype='JPC'; break; case 10 : $letype='JP2'; break; case 11 : $letype='JPX'; break; case 12 : $letype='JB2'; break; case 13 : $letype='SWC'; break; case 14 : $letype='IFF'; break; case 15 : $letype='WBMP'; break; case 16 : $letype='XBM'; break; default : $letype="UNKNOWN ($loctype)"; break; } $ok2=strtolower($letype); if ($ok1<>$ok2) { LogMe("EDGE ERROR : image $distfile ($localfname) has wrong NAME ! ($ok1 while it is a $letype)"); $localfname=$theref.'_'."$last.$ok2"; $cand="$filepath/TEMPO.{$ok2}"; if (@rename($nfile,$cand)) { // ok $nfile=$cand; } else LogMe("FS ERROR : could not rename '$nfile' to '$cand' ( for file $distfile ($localfname)"); } // else NOP if ($DORESIZE) { // else NOP, done above // now generate thumbnails and main image if (in_array($loctype,array(1,2,3))) { // type OK $locwidth=$info[0]; $locheight=$info[1]; // - check dimensions before resizing if ($locwidth<$minimumwidth) { $nbimgtoonarrow++; LogMe("IMAGE WARNING : original image $distfile ($localfname) has less ($locwidth) than the minimum width ($minimumwidth), it may be illegible on screen"); } // else NOP // generate image & thumbnails // $maxwidth=800; $maxheight=600; $thumbpref=''; $warninglimit=80*1024; // full size image $source_x=$locwidth; $source_y=$locheight; $source_type=$loctype; $filename=$nfile; // copy-paste from resize.php // $jpegquality=80; $thumbname="$filepath/{$thumbpref}$localfname"; // resize via resampling and store thumbnail // also make sure maxw and maxh are not reached if ($source_x>$source_y) { // paysage if ($source_x>$maxwidth) $ratio=(1.0*$maxwidth/$source_x); else $ratio=1; } else { // portrait if ($source_y>$maxheight) $ratio=(1.0*$maxheight/$source_y); else $ratio=1; } $dest_x=$source_x*$ratio; $dest_y=$source_y*$ratio; LogMe("dimensions1 : $source_x X $source_y -> $dest_x X $dest_y (minW=$minimumwidth,maxH=$maxheight,maxW=$maxwidth)"); $dopalette=FALSE; switch($source_type) { //$filename case 1 : $source=imagecreatefromgif($distfile); $funcgen='imagegif'; $extraparam=''; $dopalette=TRUE; break; default : case 2 : $source=imagecreatefromjpeg($filename); $funcgen='imagejpeg'; $extraparam=",$jpegquality"; break; case 3 : $source=imagecreatefrompng($filename); $funcgen='imagepng'; $extraparam=''; break; } //VGR12112004 ADDed sécurité contre imagegif() manquante... if (function_exists($funcgen)) { // let's go //VGR09082005 FIXed transparency "black GIF" problem if ($dopalette) { LogMe("slow palette stuff (GIF)"); // or PNG later $tpcolor = imagecolorat($source, 0, 0); // in the real world, you'd better test all four corners, not just one! $target = imagecreate($dest_x, $dest_y); // $dest automatically has a black fill... imagepalettecopy($target, $source); imagecopyresized($target, $source, 0, 0, 0, 0, $dest_x, $dest_y, $source_x, $source_y); $pixel_over_black = imagecolorat($target, 0, 0); // ...but now make the fill white... $bg = imagecolorallocate($target, 255, 255, 255); imagefilledrectangle($target, 0, 0, $dest_x, $dest_y,$bg); imagecopyresized($target, $source, 0, 0, 0, 0, $dest_x, $dest_y, $source_x, $source_y); $pixel_over_white = imagecolorat($target, 0, 0); // ...to test if transparency causes the fill color to show through: if ($pixel_over_black != $pixel_over_white) { LogMe("image WAS transparent !"); // Background IS transparent imagefilledrectangle($target, 0, 0, $dest_x, $dest_y, $tpcolor); imagecopyresized($target, $source, 0, 0, 0, 0, $dest_x, $dest_y, $source_x, $source_y); imagecolortransparent($target, $tpcolor); } else LogMe("image was probably not transparent !"); // else NOP, Background (most probably) NOT transparent } else { LogMe("classical & faster resampling (not GIF)"); // nor PNG later if ($target=imagecreatetruecolor($dest_x,$dest_y)) { if ($pipo=imagecopyresampled($target,$source,0,0,0,0,$dest_x,$dest_y,$source_x,$source_y)) { LogMe("resampling ok"); } else LogMe("error imagecopyresized"); } else LogMe("error imagecreatetruecolor"); } // if check transparency or not eval('$toto='.$funcgen.'($target,$thumbname'.$extraparam.');'); // send to file if ($toto) LogMe("overwrote image '$thumbname'"); else LogMe("FAILED TO OVERWRITE image '$thumbname' !!!"); // check generated size $size=@filesize(realpath($thumbname)); if ($size>$warninglimit) { $nbimgtooheavy++; LogMe("IMAGE WARNING : generated file '$thumbname' ($size bytes) is over $warninglimit bytes"); } $info=@getimagesize(realpath($thumbname)); $locwidth=$info[0]; $locheight=$info[1]; if ($locheight>$maxheight) { $nbimgtoohigh++; LogMe("IMAGE WARNING : generated file '$thumbname' has more ($locheight) than the maximum height ($maxheight)"); } LogMe("IMAGE $distfile ($localfname) IS OK FOR USE as $nameprefix"); $nbimgok++; $okimage=TRUE; $GLOBALS['imagesrefs'][$i]['filename']=$localfname; $validrefs[]=$GLOBALS['imagesrefs'][$i]; $b++; // clean previous imagedestroy($target); // here I generate thumbnails for decreasing sizes, the same way // from code line : main image 400px wide // from code line $maxwidth=400; $maxheight=300; $thumbpref='tn_'; $warninglimit=30*1024; //to code line : clean previous //to code line : imagedestroy($target); // cleanup imagedestroy($source); imagedestroy($target); } else { LogMe("ATTENTION no thumbnail creation possible with function '$funcgen' for image '$distfile' (SKIPPED)"); $warninglimit=50*1024; foreach(array('','tn_','ttn_') as $thumbpref) { $thumbname="$filepath/{$thumbpref}$localfname"; $poubelle=@copy($filename,$thumbname); // check generated size $size=@filesize(realpath($thumbname)); if ($size>$warninglimit) { $nbimgtooheavy++; LogMe("IMAGE WARNING : copied file '$thumbname' ($size bytes) is over $warninglimit bytes"); } } LogMe("WARNING copied three times that file"); $okimage=TRUE; $GLOBALS['imagesrefs'][$i]['filename']=$localfname; $validrefs[]=$GLOBALS['imagesrefs'][$i]; $b++; // register as bad type of image $nbbadtype++; // cleanup imagedestroy($source); } // if funcgen available //EoAdd } else { LogMe("EDGE ERROR : image '$nfile' has wrong type! ($letype)"); // register as bad type of image $nbbadtype++; } } else { // no resizing, he copy is already done LogMe("ATTENTION you chose to not resize this image '$distfile' (at your own risk)"); $warninglimit=50*1024; $cand="$filepath/$localfname"; if (@rename($nfile,$cand)) { // ok $nfile=$cand; } else LogMe("FS ERROR : could not rename '$nfile' to '$cand' ( for file $distfile ($localfname)"); // check generated size $size=@filesize(realpath($nfile)); if ($size>$warninglimit) { $nbimgtooheavy++; LogMe("IMAGE WARNING : copied file '$nfile' ($size bytes) is over $warninglimit bytes"); } $okimage=TRUE; $GLOBALS['imagesrefs'][$i]['filename']=$localfname; $validrefs[]=$GLOBALS['imagesrefs'][$i]; $b++; } // if DORESIZE or not } else { // bad type LogMe("EDGE ERROR : file '$nfile' IS NOT AN IMAGE"); // register as bad type of file $nbbadtype++; } } else { LogMe("EDGE ERROR : image '$nfile' has size=0"); // register as missing $nbimgmissing++; // try to delete local zero-size file $toto=@unlink($nfile); if (! $toto) LogMe("FS ERROR : could not DELETE '$nfile' of size=0"); } else { // register as missing $nbimgmissing++; LogMe("EDGE/FS ERROR : file '$nfile' IS NOT READABLE"); } } else { // register as missing $nbimgmissing++; LogMe("EDGE ACCESS ERROR : failed copy of $distfile to $nfile"); } } // else NOP, skipped and lost forever ! ;-) else { // register as missing $nbimgmissing++; $nbbadtype++; LogMe("EDGE DATA ERROR : bad file type ($last)"); } } // else NOP, skipped and lost else { // register as missing $nbimgmissing++; LogMe("EDGE DATA ERROR : bad file reference"); } return($okimage); } // TraiteImage Boolean Function