GD Library Bug Repair - $10

Discussion in 'Programming' started by mcmuney, May 27, 2008.

  1. #1
    I have a minor issue with my gd library file and need a quick repair. I value the job at about $10 for a foreigner. Below is a summary of the problem.

    I use GD library to create and display thumbnails of images that are uploaded by site visitors. All images, jpg or gif, are uploaded and renamed as .jpg. This works fine in most cases, EXCEPT when the image is an animated gif file. In this situation, the gd library creates a thumb for every frame. So if the animated gif has 100 frames, it'll create 100 different thumbs. At this point, I do not want to change the structure of the script, so in the scenario above, I want it to produce just 1 thumb. Below is an example of how it works:

    Example of working jpg upload:
    -if I upload file abc.jpg
    -it will rename the file with the next available sequence, lets say 8001.jpg
    -the thumb will be 8001-85x66-filled-enlarged.jpg

    Example of working gif (not animated file) upload:
    -if I upload file abc.gif
    -it will rename the file with the next available sequence, lets say 8002.jpg
    -the thumb will be 8002-85x66-filled-enlarged.jpg

    Example of NON-working gif animated upload:
    -if I upload file abcd.gif with 3 frames
    -it will rename the file with the next available sequence, lets say 8003.jpg
    -the thumb will be 8003-85x66-filled-enlarged-0.jpg, 8003-85x66-filled-enlarged-1.jpg, 8003-85x66-filled-enlarged-2.jpg
    -in this example, i want the gd library script to create only 8003-85x66-filled-enlarged.jpg

    This is the script:
    <?
    $image_magick_exists=true;
    //$mp="d:\\usr\\local\\imagemagick\\"; //image magick path
    /* FOR DEPLOYMENT ONLY */
    $DOCUMENT_ROOT=$_SERVER['DOCUMENT_ROOT']."";//"/home/aegeanmx/public_html/";
    //echo $DOCUMENT_ROOT;
    //$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*******************************************
    //echo $pic;
    //$img_info=getimagesize($pic);
    while(substr($pic,0,1)=="/"){$pic=substr($pic,1);}
    //$pic="../".$pic;
    //echo $pic;
    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
    //echo $pic;
    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."/")) {
    //echo "making dir ".$DOCUMENT_ROOT.$thumb_dir."/";
    	if(!mkdir($thumb_dir,0777)) die("Can't make thumbs dir");
    }
    else
    {
    //echo "dir exists: ".$DOCUMENT_ROOT.$thumb_dir;
    }
    //$rfn=$thumb_dir."/".$fnm."-".$w."x".$h."-".($fill=="yes"?"filled":"not_filled")."-".($enlarge=="yes"?"enlarged":"not_enlarged").$extension;
    $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))
    	{
    	//	if($type=='')$type=$img_info2[2];
    		//Header('Content-type: '.image_type_to_mime_type($type));
    		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;
    	//echo "r1=$r1, r2=$r2<br>";
    	if($r1>=$r2)
    	{
    		//echo "by width<br>";
    		$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
    	{
    		//echo "by height<br>";
    		$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;
    	}
    	//echo "w=$w, h=$h<br>img_w=$img_w, img_h=$img_h<br>new_w=$new_w, new_h=$new_h";
    }
    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;
    }
    //echo $new_w."x".$new_h;
    //***********************************Get new size aspects***************************************************
    
    
    if($b=="") $b="#ffffff";
    //$s=$mp."convert -resize ".$new_w."x".$new_h."! ".$pic." ".$rfn;
    $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;
    $s=$mp."convert -crop ".$w."x".$h."+".$off_y."+".$off_x." \"".$rfn."\" \"".$rfn."\"";
    exec($s);
    $time=time();
    //@touch($pic, $time);
    //@touch($rfn, $time);
    
    //header("Content-Length: ".filesize($rfn));
    //echo $rfn;
    $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;
    	$x=$mp."identify -format \"%wx%hx%m\" \"".$filename."\"";
    	exec($x, $ans);
    	$ans=join("",$ans);
    	$ans=explode("x",$ans);
    	return $ans;
    }
    ?>
    Code (markup):
     
    mcmuney, May 27, 2008 IP
  2. relixx

    relixx Active Member

    Messages:
    946
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    70
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #2
    Have found solution, have PMed you :)
     
    relixx, May 27, 2008 IP
  3. mcmuney

    mcmuney Well-Known Member

    Messages:
    834
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    128
    As Seller:
    100% - 0
    As Buyer:
    100% - 3
    #3
    Thanks relixx!! I've send you payment and iTrader.
     
    mcmuney, May 27, 2008 IP