Posted By

trepmal on 07/31/10


Tagged

thumbnail


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

osirisinternet
IMPGTX


ThumbFly: On-the-fly thumbnails


 / Published in: PHP
 

URL: http://trepmal.com/scripts/thumbfly-on-the-fly-thumbnails/

  1. <?php
  2. /* ThumbFly 2.0 - by kailey lampert ([email protected], trepmal.com)
  3.  
  4.   to include your on-the-fly thumbnail do this:
  5.   <img src="<?php echo thumbfly( array( 'src'=>'image.jpg' , 'w' => 250 , 'clean' => true ) ); ?>" />
  6.  
  7.   free to use & modify, you could probably make it better anyway - in fact, if you do, let me know!
  8.  
  9.   params (array):
  10.   src = relative/server path to image (required)
  11.   w = desired width (optional, default = null)
  12.   h = desired height (optional, default = null)
  13.   name = name of thumbnail (optional, default = src)
  14.   clean = boolean (optional, default = false) if true, remakes thumbs with same name
  15.   warn = boolean (optional, default = false) if true, creates a 'brkn img' image where it can't create thumbnail
  16.  
  17.   (width & height: if one is defined - image will be scaled, if neither - height & width will be halved) */
  18.  
  19. function thumbfly( $params ) {
  20. $defaults = array('src' => null, 'w' => null, 'h' => null, 'name' => null, 'clean' => false, 'warn' => false);
  21. $params = array_merge($defaults, $params);
  22. extract($params); //get $src, $w, $h, $name, $clean, $warn
  23. $w = isset($w) ? $w : false;
  24. $h = isset($h) ? $h : false;
  25.  
  26. $tmpdir = 'tmp-tf/'; if (!is_dir($tmpdir)) mkdir($tmpdir);
  27. $newname = (isset($name) ? $name : $src);
  28. $subdir = (dirname($newname)) ? dirname($newname) : '' ;
  29. if (!is_dir($tmpdir.$subdir)) mkdir($tmpdir.$subdir);
  30. $newname = $tmpdir.$newname;
  31. $type = @exif_imagetype($src);
  32.  
  33. if($warn) $clean = false; if($clean) { if (is_file($newname)) unlink($newname); }
  34.  
  35. $ok_types = array('1','2','3'); // not perfect, but what is?
  36. if (!isset($src) || !is_file($src) || !in_array($type,$ok_types) || $warn ) : //unusable src
  37.  
  38. if ( !$w && !$h ) { $w = 135; $h = 45; } // if no width or height given, use defaults
  39. else if ( !$w && $h ) { $w = $h; } // if 1 dimension given, make it square
  40. else if ( !$h && $w ) { $h = $w; } // if 1 dimension given, make it square
  41. $img = imagecreatetruecolor($w, $h);
  42. $text_color = imagecolorallocate($img, 200, 200, 200);
  43. imagestring($img, 2, 5, 10, 'brkn img', $text_color);
  44. if ($warn) { imagejpeg($img,$newname); }
  45. else { $newname = ''; }
  46. imagedestroy($img);
  47.  
  48. elseif (is_file($newname)) : // if tmp img exists..
  49. /* nothing happening... */
  50. else : //src is real and thumb isn't cached... let's start resizing!
  51.  
  52. list($ow, $oh) = getimagesize($src); //get dimensions of src img
  53. $or = $ow/$oh; //original ratio
  54.  
  55. if ( !$w && !$h ) { $w = $ow*(0.5); $h = $oh*(0.5); } // if no width or height given - change defaults here!
  56. else if ( !$w && $h ) { $w = ($h*$or); } //only height? scale
  57. else if ( !$h && $w ) { $h = ($w/$or); } //only width? scale
  58. $r = $w/$h; //here to prevent division by zero
  59.  
  60. $image_thumb = imagecreatetruecolor($w,$h); //create blank canvas in defined proportions
  61.  
  62. $modwidth = $ow; $modheight = $oh; //scaled size to work from
  63. $off_w = 0; $off_h = 0; //offsets
  64. if ($r > $or) : //if new is more landscape-y than original
  65. $modheight = $ow/$r; //slice off some of top & bottom
  66. $off_h = ($oh-$modheight)/2; //get .5 the diff for centering (even slicing)
  67. elseif ($ratio < $or) : //and reverse...
  68. $modwidth = $oh*$r;
  69. $off_w = ($ow-$modwidth)/2;
  70. endif;
  71.  
  72. switch ($type) {
  73. case 1: /*gif*/
  74. $image_original = imagecreatefromgif($src);
  75. $trns_ind = imagecolortransparent($image_original);
  76. if ($trns_ind >= 0) {
  77. $trns_color = imagecolorsforindex($image_original, $trns_ind);
  78. $trns_ind = imagecolorallocate($image_thumb, $trns_color['red'], $trns_color['green'], $trns_color['blue']);
  79. imagefill($image_thumb, 0, 0, $trns_ind);
  80. imagecolortransparent($image_thumb, $trns_ind);
  81. }
  82. imagecopyresampled($image_thumb, $image_original, '0', '0', $off_w, $off_h, $w, $h, $modwidth, $modheight);
  83. $the_thumb .= '.gif'; $thumb_loc .= '.gif';
  84. imagegif($image_thumb, $newname);
  85. break;
  86. case 2: /*jpg*/
  87. $image_original = imagecreatefromjpeg($src);
  88. imagecopyresampled($image_thumb, $image_original, '0', '0', $off_w, $off_h, $w, $h, $modwidth, $modheight);
  89. imagejpeg($image_thumb, $newname, 100);
  90. break;
  91. case 3: /*png*/
  92. $image_original = imagecreatefrompng($src);
  93. imagealphablending( $image_thumb, false );
  94. imagesavealpha( $image_thumb, true );
  95. imagecopyresampled($image_thumb, $image_original, '0', '0', $off_w, $off_h, $w, $h, $modwidth, $modheight);
  96. imagepng($image_thumb, $newname);
  97. break;
  98. }//end switch
  99. imagedestroy($image_thumb);
  100. endif;
  101. return $newname;
  102. }// end function
  103. ?>

Report this snippet  

You need to login to post a comment.