Website Design United States, Website Design California, Website Designing United States, Website Designing California

Image Thumbnail - PHP

Generate thumbnails of images on the fly. The script creates a thumbnail of 164x164 keeping the scale of the image and also displaying the image width/height, size, and name. Requires the GD library and TTF 2 font files for displaying the text. Can work for regular bbcode enabled forums :
[img]http://yoursite/thumbnailscript.php?img=imagename.png[/img]
Also easily customize the thumbnail of changing the background color of the info text and color, and the font and style.

c = is the background color (red, yellow, green, gray, white )
f = is the font name
s = is the font style (b, i, bi, bold, italic, bold_italic)
p = is if the image is in the private folder

ex:
[img]http://yoursite.com/thumbnailscript.php?img=imagename.png&f=arial&s=b&c=red&p=1[/img]

<?php

/*
will need to change the fonts folder, font names, image folders, and the referer checker (very bottom) values to fit your server and likings.

$image_folders['private'] = 'image_bin/private/';
//private images (images that users wouldn't be able to directly ascess)
$image_folders['public'] = 'image_bin/public/';
$font_folder = 'fonts';

$img = $_GET['img'];

$color['black'] = 0;
$color['gray'] = 8421504;
$color['blue'] = 255;
$color['green'] = 65280;
$color['yellow'] = 16776960;
$color['red'] = 16711680;
$color['white'] = 16777215;

$font_default = 'courier_new';

$fonts['arial']['normal'] ='arial.ttf';
$fonts['arial']['bold'] ='arialbd.ttf';
$fonts['arial']['italic'] ='ariali.ttf';
$fonts['arial']['bold_italic'] ='arialbi.ttf';

$fonts['courier_new']['normal'] ='cour.ttf';
$fonts['courier_new']['bold'] ='courbd.ttf';
$fonts['courier_new']['italic'] ='couri.ttf';
$fonts['courier_new']['bold_italic'] ='courbi.ttf';

$fonts['georgia']['normal'] ='georgia.ttf';
$fonts['georgia']['bold'] ='georgiab.ttf';
$fonts['georgia']['italic'] ='georgiai.ttf';
$fonts['georgia']['bold_italic'] ='georgiaz.ttf';

$fonts['times_new_roman']['normal'] ='times.ttf';
$fonts['times_new_roman']['bold'] ='timesbd.ttf';
$fonts['times_new_roman']['italic'] ='timesi.ttf';
$fonts['times_new_roman']['bold_italic'] ='timesbi.ttf';

$fonts['verdana']['normal'] ='verdana.ttf';
$fonts['verdana']['bold'] ='verdanab.ttf';
$fonts['verdana']['italic'] ='verdanai.ttf';
$fonts['verdana']['bold_italic'] ='verdanaz.ttf';

if( isset($_GET['p']) )
{
$current_folder = $image_folders['private'];
}
else
{
$current_folder = $image_folders['public'];
}

$font_style = 'normal';
$bar_background = $color['black'];
$font_color = $color['white'];

$thumb_height = 164;
$thumb_width = 164;

$error = FALSE;

if( isset($_GET['s']) )
{
switch( strtolower($_GET['s']) )
{
case 'b':
$font_style = 'bold';
break;

case 'bold':
$font_style = 'bold';
break;

case 'i':
$font_style = 'italic';
break;

case 'italic':
$font_style = 'italic';
break;

case 'bi':
$font_style = 'bold_italic';
break;
case 'bold_italic':
$font_style = 'bold_italic';
}
}
else{ $font_style = 'italic'; }

$font_name = $fonts[$font_default][$font_style];

if( isset($_GET['f']) )
{
switch( strtolower($_GET['f']) )
{
case 'arial':
$font_name = $fonts['arial'][$font_style];
break;

case 'courier_new':
$font_name = $fonts['courier_new'][$font_style];
break;

case 'georgia':
$font_name = $fonts['georgia'][$font_style];
break;

case 'times_new_roman':
$font_name = $fonts['times_new_roman'][$font_style];
break;

case 'verdana':
$font_name = $fonts['verdana'][$font_style];
break;

case 'a':
$font_name = $fonts['arial'][$font_style];
break;

case 'c':
$font_name = $fonts['courier_new'][$font_style];
break;

case 'g':
$font_name = $fonts['georgia'][$font_style];
break;

case 't':
$font_name = $fonts['times_new_roman'][$font_style];
break;

case 'v':
$font_name = $fonts['verdana'][$font_style];
}
}

if( isset($_GET['c']) )
{
switch( strtolower($_GET['c']) )
{
case 'red':
$bar_background = $color['red'];
$font_color = $color['black'];
break;

case 'blue':
$bar_background = $color['blue'];
$font_color = $color['white'];
break;

case 'green':
$bar_background = $color['green'];
$font_color = $color['black'];
break;

case 'yellow':
$bar_background = $color['yellow'];
$font_color = $color['blue'];
break;

case 'white':
$bar_background = $color['white'];
$font_color = $color['red'];
break;

case 'gray':
$bar_background = $color['gray'];
$font_color = $color['white'];
}
}

if( check_referer() )
{
if( $img != '' )
{
if( check_path )
{
list($img_width, $img_height, $img_type, $img_attr) = getimagesize($current_folder . $img);

switch( $img_type )
{

case 1:
//GIF
$thumb_src = @imagecreatefromgif( $current_folder . $img );
break;

case 2:
//JPG
$thumb_src = imagecreatefromjpeg( $current_folder . $img );
break;

case 3:
//PNG
$thumb_src = @imagecreatefrompng( $current_folder . $img );
}

if( thumb_src != '' )
{
$resized_src = @imagecreatetruecolor( $thumb_width, $thumb_height );
if( function_exists( 'imageantialias' )){ @imageantialias( $resized_src, TRUE ); }

$whitespace_x = 0;
$whitespace_y = 0;
$scale_x = ($thumb_width / $img_width);
$scale_y = ($thumb_height / $img_height);

if( $scale_x > 1 ){ $scale_x = 1; }
if( $scale_y > 1 ){ $scale_y = 1; }

if( $scale_x < $scale_y )
{
$scale = $scale_x;
}
else
{
$scale = $scale_y;
}

$scale = round($scale, 1);

$new_width = $img_width * $scale;
$new_height = $img_height * $scale;

$whitespace_x = round(($thumb_width - $new_width) * 0.5, 1);
$whitespace_y = round(($thumb_height - $new_height) * 0.5, 1);

if( $resized_src != '' )
{
@imagecopyresampled( $resized_src, $thumb_src, $whitespace_x, $whitespace_y, 0, 0, ($thumb_width-$whitespace_x*2), ($thumb_height-$whitespace_y*2), $img_width, $img_height );

@imagedestroy( $thumb_src );
@imagefilledrectangle( $resized_src, 0, $thumb_height-17, $thumb_width, $thumb_height, $bar_background );
@imagefilledrectangle( $resized_src, 0, 0, $thumb_width, 17, $bar_background );

$show_text = $img_width . 'x' . $img_height . ' ' . return_size( $current_folder . $img );
$box = @imagettfbbox( 10, 0, $font_folder . '/' . $font_name, $show_text );
$show_width = $box[2] - $box[0];
$center = ($thumb_width - $show_width) * 0.5 ;
@imagefttext($resized_src, 10, 0, $center, 13, $font_color, $font_folder . '/' . $font_name, $show_text, array());

$img_array = explode( '.', $img );
$img_name = '';
for ( $i=0;$i<count(img_array);$i++ )
{
$img_name .= $img_array[$i];
}
if( strlen( $img_name ) > 15 )
{
$img_name = substr(strtoupper($img_name),0,12) . '...';
}
else{ $img_name = strtoupper($img_name); }

$show_text = $img_name;
$box = @imagettfbbox( 10, 0, $font_folder . '/' . $font_name, $show_text );
$show_width = $box[2] - $box[0];
$center = ($thumb_width - $show_width) * 0.5 ;
@imagefttext($resized_src, 10, 0, $center, $thumb_height-4, $font_color, $font_folder . '/' . $font_name, $show_text, array());

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Content-type: image/png");
@imagepng( $resized_src );
@imagedestroy( $resized_src );

}else{ $error = TRUE; }
}else{ $error = TRUE; }
}else{ $error = TRUE; }
}else{ $error = TRUE; }
}
if( $error == TRUE )
{

@imagedestroy( $thumb_src );
@imagedestroy( $resized_src );

$errimg_src = @imagecreatetruecolor( 200, 18 );
if( function_exists( 'imageantialias' )){ @imageantialias( $errimg_src, TRUE ); }

$show_text = 'error creating thumbnail';
$box = @imagettfbbox( 8, 0, $font_folder . '/' . $font_name, $show_text );
$show_width = $box[2] - $box[0];
$center = (200 - $show_width) * 0.5 ;
@imagefttext($errimg_src, 8, 0, $center, 12, $color['yellow'], $font_folder . '/' . $font_name, $show_text, array());

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Content-type: image/png");
@imagepng( $errimg_src );
@imagedestroy( $errimg_src );

}

function check_path( $path )
{
for( $i=0; $i<=count($path); $i++ )
{
$check = ord(substr( $path, $i, 1));
if( ( $check < 48 ) || ( $check > 57 ) )
{
if( ( $check < 65 ) || ( $check > 90 ) )
{
if( ( $check < 97 ) || ( $check > 122 ) )
{
if( ( $check != 95 ) || ( $check != 46 ) )
{
return false;
}
}
}
}
}
if( !file_exists($current_folder . $img) )
{
return false;
}
return true;
}

function return_size( $filepath )
{
$filesize = @filesize( $filepath );
if ( $filesize == null )
{
return '0 Bytes';
}
if ( $filesize > 1024 )
{
$filesize = $filesize / 1024;
$type = 'KB';
if ( $filesize > 1024 )
{
$filesize = $filesize / 1024;
$type = 'MB';

if ( $filesize > 1024 )
{
$filesize = $filesize / 1024;
$type = 'GB';
}
}
}
else
{
$type = ' Bytes';
}
$filesize = round( $filesize, 2);
return $filesize. $type;
}

function check_referer()
{
$correct_referer = array('wannabegames.com/', 'programmers-corner.com/' );
//can be array of sites, or just one.

$user_referer = strtolower($_SERVER['HTTP_REFERER']);
//get the referer from the browser
if( $user_referer == '' )
{
return TRUE;
}

if( substr($user_referer,0,7) == 'http://' )
{
$user_referer = substr($user_referer,7,strlen($user_referer)-7);
if( substr($user_referer,0,4) == 'www.' )
{
$user_referer = substr($user_referer,4,strlen($user_referer)-4);
}
}
else
{
if( substr($user_referer,0,4) == 'www.' )
{
$user_referer = substr($user_referer,4,strlen($user_referer)-4);
}
}
//cut off http:// and www. if they are there

$referer_error = FALSE;
if( !is_array($correct_referer) )
{
if( substr($user_referer,0,strlen($correct_referer)) != $correct_referer )
{
$referer_error = TRUE;
}
}
else
{
$referer_error = TRUE;
for( $c=0; $c<count($correct_referer); $c++ )
{
if( substr($user_referer,0,strlen($correct_referer[$c])) == $correct_referer[$c] )
{
$referer_error = FALSE;
break;
}
}
}

if( $referer_error == true )
{
return FALSE;
}
else
{
return TRUE;
}
}
?>


WEB DESIGN INDIA
42 B Malviya Nagar , New Delhi-110017

Skype: manmeetsi
Email: support.webdesignindia@gmail.com
Tel: 91-011-40502005, 9810067295

 















 


© 2008-2009 dotnet4all.com