IMPORTANT!

Snipt is going open source. We've toyed with this idea for quite a while, and have finally decided it's the right way to move forward.

A few things:
  • The entire Snipt source code will be released on GitHub under the 3-clause BSD License on Friday, September 10th.
  • While we'd like to think we're perfect, we realize we're only human. By open sourcing the software that runs this website, certain bugs or security flaws may be discovered that could compromise the privacy of your snipts.
  • Only the Lion Burger team will be able to push commits to the Snipt.net site. Contributors should send a pull request to add new features or submit patches.
  • By using this site, you agree not to be too angry or take any legal action against Lion Burger should this whole thing go up in flames some day.
  • Follow us on Twitter for updates.
I agree, close this message
Sign up to create your own snipts, or login.

Latest 100 public snipts » gd The latest public gd snipts.

showing 1-4 of 4 snipts for gd
  • scale and crop an image to best fit a defined set of dimensions
    function autoCrop($file, $height, $width){
    
         //load the image file
    
         $img = false;
    
         $img = imagecreatefromjpeg($file);
    
         //quit if not loaded
    
         if(!$img){
    
              return false;
    
         }
    
         //get the image dimensions
    
         $curr = @getimagesize($file);
    
     
         $perc_w = $width / $curr[0];
    
         $perc_h = $height / $curr[1];
    
     
    
         if(($width > $curr[0]) || ($height > $curr[1])){
    
              return;
    
         }
    
     
    
         if($perc_h > $perc_w){
    
              //taller
    
              $width = $width;
    
              $height = round($curr[1] * $perc_w);
    
         } else {
    
              //wider
    
              $height = $height;
    
              $width = round($curr[0] * $perc_h);
    
         }
    
         //output
    
         $nwimg = imagecreatetruecolor($width, $height);
    
         imagecopyresampled($nwimg, $img, 0, 0, 0, 0, $width, $height, $curr[0], $curr[1]);
    
         imagejpeg($nwimg, $file);
    
     
    
         imagedestroy($nwimg);
    
         imagedestroy($img);
    
     
    
    }
    

    copy | embed

    0 comments - tagged in  posted by damonky on Jul 24, 2009 at 4:28 a.m. EDT
  • PNG transparent 1x1px generator
    <?php
    /*****************************************************************
     * Script for automatic generation of one pixel
     * alpha-transparent images for non-RGBA browsers.
     * @author Lea Verou
     * @version 1.0 beta
     * Licensed under a MIT license
     *****************************************************************/
    
    ######## SETTINGS ##############################################################
    
    /**
     * Enter the directory in which to store cached color images.
     * This should be relative and SHOULD contain a trailing slash.
     * The directory you specify should exist and be writeable (CHMOD 666 or 777).
     * If you want to store the pngs at the same directory, leave blank ('').
     */
    define('COLORDIR', 'rgba/');
    
    
    /**
     * If you requently use a color with varying alphas, you can name it
     * below, to save you some typing and make your CSS easier to read.
     */
    $color_names = array(
    	'white' => array(255,255,255),
    	'black' => array(0,0,0)
    	// , 'mycolor' => array(red value, green value, blue value)
    );
    
    
    /**
     * If you don't want the generated pngs to be stored in the server, set the following to
     * false. This is STRONGLY NOT RECOMMENDED. It's here only for testing/debugging purposes.
     */
    define('CACHEPNGS', true);
    
    
    
    ######## NO FURTHER EDITING, UNLESS YOU REALLY KNOW WHAT YOU'RE DOING ##########
    
    $dir = substr(COLORDIR,0,strlen(COLORDIR)-1);
    if(!is_writable($dir))
    {
    	die("The directory '$dir' either doesn't exist or isn't writable.");
    }
    
    $rgb = $color_names[$_GET['name']];
    
    $red	= is_array($rgb)? $rgb[0] : intval($_GET['r']);
    $green	= is_array($rgb)? $rgb[1] : intval($_GET['g']);
    $blue	= is_array($rgb)? $rgb[2] : intval($_GET['b']);
    $alphaurl = $_GET['a'];
    
    // "A value between 0 and 127. 0 indicates completely opaque while 127 indicates completely transparent."
    // http://www.php.net/manual/en/function.imagecolorallocatealpha.php
    $alpha = intval(127 - 127 * ($alphaurl / 100));
    
    // Does it already exist?
    $filepath = COLORDIR . "r{$red}_g{$green}_b{$blue}_a{$alphaurl}.png";
    
    if(/*CACHEPNGS and */file_exists($filepath))
    {
    $url = "http://{$_SERVER['HTTP_HOST']}/{$filepath}";
    header("Status: 301");
    die(header("Location: {$url}"));
    }
    else
    {
    	// Send headers
    header('Content-Type: image/png');
    header("Expires: ".gmdate("D, d M Y H:i:s", strtotime('+5 years'))." GMT"); 
    
    
    	$img = @imagecreatetruecolor(1,1) or die('Cannot Initialize new GD image stream');
    	
    	// This is to allow the final image to have actual transparency
    	// http://www.php.net/manual/en/function.imagesavealpha.php
    	imagealphablending($img, false);
    	imagesavealpha($img, true);
    	
    	// Allocate our requested color
    	$color = imagecolorallocatealpha($img, $red, $green, $blue, $alpha);
    	
    	// Fill the image with it
    	imagefill($img,0,0,$color);
    
        // Save the file
    	imagepng($img,$filepath,0,NULL);
    	
    	// Serve the file
    	imagepng($img);
    	flush();
    	
    	// Free up memory
    	imagedestroy($img);
    }
    ?>
    

    copy | embed

    0 comments - tagged in  posted by aidilfbk on May 25, 2009 at 7:49 a.m. EDT
  • Symfony - Action for text image generation (using GD)
    <?php
      public function executeGenerateText (sfWebRequest $request) {
        $font = '/images/ROTISSAS.TTF';
        $bbox = imageftbbox(20, 0, $font, $this->getRequestParameter('text') );
        
        $width = $bbox[2] - $bbox[0] + 10;
        $height = $bbox[1] - $bbox[7] + 10;
        
        $im = imagecreatetruecolor($width, $height);
        $black = imagecolorallocate($im, 0, 0, 0);
        $white = imagecolorallocate($im, 255, 255, 255);
        
        // Set the background to be white
        // Path to our font file
        // First we create our bounding box
        
        
        imagefilledrectangle($im, 0, 0, $width, $height, $black);
    
        // This is our cordinates for X and Y
        $x = $bbox[0] + (imagesx($im) / 2) - ($bbox[4] / 2);
        //$y = $bbox[1] + (imagesy($im) / 2) - ($bbox[5] / 2) + 3 ;
        $y = 28;
        
        imagefttext($im, 20, 0, 0, $y, $white, $font, $this->getRequestParameter('text'));
        
        // Output to browser
        header('Content-type: image/png');
        
        imagepng($im);
        imagedestroy($im);
    
        return sfView::NONE;
      }//executeGenerateText
    ?>
    
    
    # USAGE
    
    <img src="<? echo url_for('modulename/generateText?text=My text'); ?>" alt="" title="" />
    

    copy | embed

    0 comments - tagged in  posted by Tram on Jan 15, 2009 at 10:11 a.m. EST
  • Proportional image resizing
    function resize_image( $file, $width = 0, $height = 0, $proportional = false, $output = 'file', $delete_original = true )
    {
    	if ( $height <= 0 && $width <= 0 ) return false;
    
    	$info = getimagesize($file);
    	$image = '';
    
    	$final_width = 0;
    	$final_height = 0;
    	list($width_old, $height_old) = $info;
    
       if ($proportional) {
    		   if ($width == 0) $factor = $height/$height_old;
    		   elseif ($height == 0) $factor = $width/$width_old;
    		   else $factor = min ( $width / $width_old, $height / $height_old);
    
    		   $final_width = round ($width_old * $factor);
    		   $final_height = round ($height_old * $factor);
       } else {
    		   $final_width = ( $width <= 0 ) ? $width_old : $width;
    		   $final_height = ( $height <= 0 ) ? $height_old : $height;
       }
    
       switch ($info[2]) {
    		   case IMAGETYPE_GIF:
    				   $image = imagecreatefromgif($file);
    		   break;
    		   case IMAGETYPE_JPEG:
    				   $image = imagecreatefromjpeg($file);
    		   break;
    		   case IMAGETYPE_PNG:
    				   $image = imagecreatefrompng($file);
    		   break;
    		   default:
    				   return false;
       }
    
       $image_resized = imagecreatetruecolor( $final_width, $final_height );
    
       if(($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG)) {
    		   $trnprt_indx = imagecolortransparent($image);
    
    		   // If we have a specific transparent color
    		   if($trnprt_indx >= 0) {
    
    				   // Get the original image's transparent color's RGB values
    				   $trnprt_color    = imagecolorsforindex($image, $trnprt_indx);
    
    				   // Allocate the same color in the new image resource
    				   $trnprt_indx    = imagecolorallocate($image_resized, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
    
    				   // Completely fill the background of the new image with allocated color.
    				   imagefill($image_resized, 0, 0, $trnprt_indx);
    
    				   // Set the background color for new image to transparent
    				   imagecolortransparent($image_resized, $trnprt_indx);
    
    
    		   }
    		   // Always make a transparent background color for PNGs that don't have one allocated already
    		   elseif ($info[2] == IMAGETYPE_PNG) {
    
    				   // Turn off transparency blending (temporarily)
    				   imagealphablending($image_resized, false);
    
    				   // Create a new transparent color for image
    				   $color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127);
    
    				   // Completely fill the background of the new image with allocated color.
    				   imagefill($image_resized, 0, 0, $color);
    
    				   // Restore transparency blending
    				   imagesavealpha($image_resized, true);
    		   }
       }
    
       imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $final_width, $final_height, $width_old, $height_old);
    
       if($delete_original) @unlink($file);
    
       switch (strtolower($output)) {
    		   case 'browser':
    				   $mime = image_type_to_mime_type($info[2]);
    				   header("Content-type: $mime");
    				   $output = NULL;
    		   break;
    		   case 'file':
    				   $output = $file;
    		   break;
    		   case 'return':
    				   return $image_resized;
    		   break;
    		   default:
    		   break;
       }
    
       switch ($info[2]) {
    		   case IMAGETYPE_GIF:
    				   imagegif($image_resized, $output);
    		   break;
    		   case IMAGETYPE_JPEG:
    				   imagejpeg($image_resized, $output);
    		   break;
    		   case IMAGETYPE_PNG:
    				   imagepng($image_resized, $output);
    		   break;
    		   default:
    				   return false;
       }
    
       return true;
    }
    

    copy | embed

    0 comments - tagged in  posted by evansims on Dec 20, 2008 at 7:39 p.m. EST
Sign up to create your own snipts, or login.