Buying Image Magik GD Quality Fix

Discussion in 'Programming' started by mcmuney, Jul 2, 2011.

  1. #1
    I'm using this code for image magic gd.php file, which works, but I'd like to improve quality. Here's my code. Let me know if you can quickly modify the code to improve quality.

    
    <?
    $image_magick_exists=true;
    $DOCUMENT_ROOT=$_SERVER['DOCUMENT_ROOT']."";//"/home/aegeanmx/public_html/";
    $mp="/usr/bin/";
    //$mp="/usr/local/bin/";
    /* ==================== */
    $maxw=$_GET['maxw'];
    $maxh=$_GET['maxh'];
    $back=$GET['b'];
    $w=$_GET['w'];
    $h=$_GET['h'];
    $pic=$_GET['pic'];
    
    $mode=$_GET['mode'];
    $type=$_GET['type'];
    $quality=$_GET['q']==''?100:$_GET['q'];
    if($pic=='')Exit();
    $enlarge=$_GET['enlarge']; //yes/no/ - enlarge smaller images
    if(!(($enlarge=="yes") or ($enlarge=="no"))) $enlarge="yes";
    $fill=$_GET['fill']; //yes/no - fill whole image or not
    if(!(($fill=="yes") or ($fill=="no"))) $fill="yes";
    $always_create=($_GET['refresh']=="yes");
    //***********************************Load and create image*******************************************
    while(substr($pic,0,1)=="/"){$pic=substr($pic,1);}
    if(!file_exists($pic)) $pic="images/cross.gif";
    $img_info=IMgetimagesize($pic);
    $img_w=$img_info[0];
    $img_h=$img_info[1];
    if($img_w==0 and $img_h==0) $image_magick_exists=false;//return original
    if(!$image_magick_exists)
    {
    	header("Content-Length: ".filesize($pic));
    	$fh=fopen($pic, "rb");
    	$s=fread ($fh, filesize ($pic));
    	fclose($fh);
    	echo $s;
    	exit();
    }
    if($h=="")
    {
    	$h=ceil(($w*$img_h)/$img_w);
    }
    elseif($w=="")
    {
    	$w=ceil(($img_w*$h)/$img_h);
    }
    $path_parts = pathinfo($pic);
    $dir=$path_parts["dirname"];
    $extension=".".$path_parts["extension"];
    $fnm=basename($pic, $extension);
    
    $thumb_dir = $dir.'/thumbs';
    
    if(!is_dir($thumb_dir."/")) {
    	if(!mkdir($thumb_dir,0777)) die("Can't make thumbs dir");
    }
    else
    {
    }
    
    $rfn=$thumb_dir."/".$fnm."-".$w."x".$h."-".($fill=="yes"?"filled":"not_filled")."-".($enlarge=="yes"?"enlarged":"not_enlarged").$extension;
    if(($enlarge=='no') and ($img_w<$w and $img_h<$h)) 
    {
    	copy ($pic, $rfn);
    }
    	if(file_exists($rfn)and (!$always_create))// and filemtime($pic)==filemtime($rfn))
    	{
    		header("Content-Length: ".filesize($rfn));
    		$fh=fopen($rfn, "rb");
    		$s=fread ($fh, filesize ($rfn));
    		fclose($fh);
    		echo $s;
    		exit();
    	}
    
    
    if($fill=="yes")
    {
    	$r1=$w/$h;
    	$r2=$img_w/$img_h;
    
    	if($r1>=$r2)
    	{
    
    		$new_w=$w;
    		$new_h=ceil($new_w*$img_h/$img_w);
    		$off_y=0;
    		$off_x=ceil(($new_h-$h)/2);
    		$g=$w;
    	}
    	else
    	{
    		$new_h=$h;
    		$g="x".$w;
    		$new_w=ceil($img_w*$new_h/$img_h);
    		$off_y=ceil(($new_w-$w)/2);
    		$off_x=0;
    	}
    }
    else
    {
    	if($img_w>$img_h)
    	{
    		$new_w=$w;
    		$new_h=ceil($new_w*$img_h/$img_w);
    	}
    	else
    	{
    		$new_h=$h;
    		$new_w=ceil($img_w*$new_h/$img_h);
    	}
    	$off_x=0;
    	$off_y=0;
    }
    //***********************************Get new size aspects***************************************************
    
    if($b=="") $b="#ffffff";
    $s=$mp."convert -resize  ".$new_w."x".$new_h."! \"".$pic."\" \"".$rfn."\"";
    exec($s);
    $s=$mp."convert -crop ".$w."x".$h."+".$off_y."+".$off_x." \"".$rfn."\" \"".$rfn."\"";
    exec($s);
    $time=time();
    $fh=fopen($rfn, "rb");
    $s=fread($fh, filesize ($rfn));
    fclose($fh);
    echo $s;
    exit();
    function IMgetimagesize($filename)
    {
    	global $mp, $image_magick_exists;
    	$x=$mp."identify -format \"%wx%hx%m\" \"".$filename."\"";
    	exec($x, $ans);
    	$ans=join("",$ans);
    	$ans=explode("x",$ans);
    	return $ans;
    }
    ?>
    
    Code (markup):
     
    mcmuney, Jul 2, 2011 IP