Thumbnail

Discussion in 'PHP' started by harryoxford, Sep 18, 2009.

  1. #1
    Is it possible to generate thumbnail of websites in php ???
     
    harryoxford, Sep 18, 2009 IP
  2. phprightnow

    phprightnow Peon

    Messages:
    296
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Yep, you can using GD2. It's a module that's compiled with PHP on most web hosts.

    function createthumb($name,$filename,$new_w,$new_h)
    {
    	$system=explode(".",$name);
    	if (preg_match("/jpg|jpeg/",$system[1])){$src_img=imagecreatefromjpeg($name);}
    	if (preg_match("/png/",$system[1])){$src_img=imagecreatefrompng($name);}
            if (preg_match("/gif/",$system[1])){$src_img=imagecreatefromgif($name);}
    	$old_x=imageSX($src_img);
    	$old_y=imageSY($src_img);
    	$thumb_w=$new_w;
    	$thumb_h=$new_h;
    	$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
    	imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 
    	if (preg_match("/png/",$system[1]))
    	{
    		imagepng($dst_img,$filename); 
    	} elseif (preg_match("/gif/",$system[1])) {
    		imagegif($dst_img,$filename);
    	} else {
    		imagejpeg($dst_img,$filename); 
    	}
    	imagedestroy($dst_img); 
    	imagedestroy($src_img); 
    }
    PHP:
    I have tested this function personally on PHP5 with GD2 compiled, and it works fine.
     
    phprightnow, Sep 19, 2009 IP