/*======================================================================================================================
image manipulation function (resize and save to hard drive [resample where it is possible])
author/mantainter:Bell Cristian
Description and help... maybe someday, but I very much doubt it
Ver 1.2c
changes from ver 0.5 (init ver.):
- merge si cu GD mai mic de 2.0 care are imagecreatetruecolor. Am incercat initial sa vad daca functia imagecreatetruecolor exista si daca e sa o folosesc si daca nu, sa folosesc imagecreate simplu, insa functia exista desi nu functiona (FATAL bla bla).
- adaugat SMART RESIZE care se vede prin $s.
- daca e GD 2.0 sau mai mult face imagecopyresampled nu resized rezultand un thumb care arata bine, (desi si inainte copiam paleta de culori deci nu ar fi trebuit sa existe diferente notabile)
- am schimbat mesajele de eroare ca sa nu mai fie atat de criptice
- stie sa faca jpeg din aproape orice :) (EXCEPT BMP) - still looking into this
- added CROP option, enablet throught $crop =1 var. It now crops the picture after resizeing, so you'll not have any more different size thumbs
- works without 'register globals' turned On
- .c = mended comments, added some new comments, translated all commends to english, changed all variable names to english, and cleared a little bit the code
Obs.: $jpeg_quality, $crop, $crop_o_x, $crop_o_y can be transmited as arguments, but I set globally
//=======================================================================================================================*/
//>
function helpDebug($string){
echo '';
}
function dothumb($pic_orig,$pic_rez,$x,$y, $hard_drive, $crop){
global $jpeg_quality;
global $init_x, $init_y, $init_type;
//if type=NULL means file cannot be oppened, or file is not a known picture format
if(empty($init_type))
die("Unable to perform (Incorect picture format, no picture at all or no access)");
else{
$mime_type = (image_type_to_mime_type($init_type));
//if(eregi('bmp', $mime_type)){
// $rez = @imagecreate
$handle=fopen($pic_orig, 'r') or die("Unable to perform (Incorect picture format, no picture at all or no acces)");
$pic_orig=fread($handle, filesize($pic_orig)) or die("read");
//echo data string for testing puriposes
//echo $pic_orig;
$src = @imagecreatefromstring($pic_orig) or die('img_create_from_string');
$dst = @imagecreatetruecolor($x, $y) or $dst = @imagecreate($x, $y) or die('GD library not installed '.__LINE__);
//flood fill if needed, not really implemented yed, but if you uncomment it, it will do the trick (16.11.2004@1:14)
/*
$red = 255;
$green = 200;
$blue = 200;
$x_fill=0;
$y_fill=0;
$color = ImageColorAllocate( $rez, $red, $green, $blue);
imagefill($rez, $x_fill,$y_fill, $color) or die('dead');
*/
//filled
imagepalettecopy($dst,$src);
if(!empty($crop) && $crop==1){
global $crop_o_x, $crop_o_y;
$init_x=$crop_o_x;
$init_y=$crop_o_y;
}
imagecopyresampled($dst,$src,0,0,0,0,$x,$y,$init_x,$init_y) or imagecopyresized($dst,$src,0,0,0,0,$x,$y,$init_x,$init_y) or die('image_copy_smaller');
//END OF RESIZE START OUTPUT (TO BROWSER/HARD DRIVE)
if($hard_drive==1){
@imagejpeg($dst, $pic_rez, $jpeg_quality);
echo "
si am facut si thumbnail: ".$pic_rez."
";
}else{
@imagejpeg($dst, '', $jpeg_quality);
@imagedestroy($dst);
}
}
}
//simulate registering of global variables for GET
foreach (array_keys($_GET) as $key) $$key=$_GET[$key];
//settings
if(empty($qual))
$jpeg_quality=82;
else
$jpeg_quality=$qual;
$pic_orig = "../upload/".$numepoza; //put your relative path here instead of "" (ex: "../pic_dir/" or "pic_dir/")
//IF $hard_drive==1 it will save the file to the hard disk
if(!empty($hard_drive) && $hard_drive==1){
if(empty($pic_rez))
$pic_rez = $pic_orig."_";
}else
$pic_rez='';
//prevent notice_errors interference with output string/picture=>no picture display ||
//this is only needed on sysytems that run with error_reporting(E_ALL) (including 'Notice: Undefined var...'), very few cases
if(empty($hard_drive))
$hard_drive=0;
if(empty($crop))
$crop=0;
//end settings][start resize
//obtain photo size (px), and type
$prop = getimagesize($pic_orig);
$init_x=$prop[0];
$init_y=$prop[1];
$init_type=$prop[2];
//start SMART resize:
if($init_x<=$x && $init_y<=$y){ //smaller than desired thumbanail => no resize needed
$pic_new_x=$init_x;
$pic_new_y=$init_y;
}else{
if(!empty($x) && !empty($y)){ //both dimensions set, it will do a resize for the bigger one, crop for the smaller one
$scalx=$init_x/$x;
$scaly=$init_y/$y;
if(!empty($crop)){
$pic_new_x=$x;
$pic_new_y=$y;
if($scalx>$scaly){
$crop_o_x=$init_x-ceil($init_x/$scaly);
$crop_o_y=$init_y;
}else{
$crop_o_y=$init_y-ceil($init_y/$scalx);
$crop_o_x=$init_x;
}
}else{
if($scalx>$scaly)
$y=0;
else
$x=0;
}
}
//end of resize precalculation, finalize calculations for resize WITHOUT crop
if(empty($x)){ //only Y set - resize after HEIGHT
$scale=$y/$init_y;
$pic_new_x=ceil($scale*$init_x);
$pic_new_y=$y;
}elseif(empty($y)){ //only X set - resize after WIDTH
$scale=$x/$init_x;
$pic_new_x=$x;
$pic_new_y=ceil($scale*$init_y);
}
}
//final, call image manipulating function
dothumb($pic_orig,$pic_rez,$pic_new_x,$pic_new_y, $hard_drive, $crop);
?>