Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

thumbnail with gd

Function that makes a thumbnail of a imagen and keeps it's proportion. $the fourth parameter ($fill) makes that, if the image is smaller than the size we want, the function expands it. It shows the image directly to the browser, but can be easily modificed to save the imagen on a directory.

Funcion que hace una miniatura de una imagen manteniendo su proporcion individual. El cuarto parametro ($fill) hace que, si la imagen es mas pequeña del tamaño deseado, la expande. Muestra la imagen directamente en el navegador, pero puede ser facilmente modificada para guardarla en un directorio.

<?php
function thumb($img, $w, $h, $fill = true) {
	if (!extension_loaded('gd') && !extension_loaded('gd2')) {
		trigger_error("No dispones de la libreria GD para generar la imagen.", E_USER_WARNING);
		return false;
	}

	$imgInfo = getimagesize($img);
	switch ($imgInfo[2]) {
		case 1: $im = imagecreatefromgif($img); break;
		case 2: $im = imagecreatefromjpeg($img);  break;
		case 3: $im = imagecreatefrompng($img); break;
		default:  trigger_error('Tipo de imagen no reconocido.', E_USER_WARNING);  break;
	}

	if ($imgInfo[0] <= $w && $imgInfo[1] <= $h && !$fill) {
		$nHeight = $imgInfo[1];
		$nWidth = $imgInfo[0];
	}else{
		if ($w/$imgInfo[0] < $h/$imgInfo[1]) {
			$nWidth = $w;
			$nHeight = $imgInfo[1]*($w/$imgInfo[0]);
		}else{
			$nWidth = $imgInfo[0]*($h/$imgInfo[1]);
			$nHeight = $h;
		}
	}
  
	$nWidth = round($nWidth);
	$nHeight = round($nHeight);

	$newImg = imagecreatetruecolor($nWidth, $nHeight);

	imagecopyresampled($newImg, $im, 0, 0, 0, 0, $nWidth, $nHeight, $imgInfo[0], $imgInfo[1]);

	header("Content-type: ". $imgInfo['mime']);

	switch ($imgInfo[2]) {
		case 1: imagegif($newImg); break;
		case 2: imagejpeg($newImg);  break;
		case 3: imagepng($newImg); break;
		default:  trigger_error('Imposible mostrar la imagen.', E_USER_WARNING);  break;
	}
  
	imagedestroy($newImg);
}
?>


Usage/Uso:
thumb("image.png", 200, 200);

Random validation (CAPTCHA) image

Use this script in your contact form, for you whois query tool or just there where some extra validation is needed. A session will be created inside a dynamic image file (requires GD library). The random value of this image appears inside the generated CAPTCHA image. The user has to enter this value into formfield. This value will be checked while processing the form. Without entering this value a form will not be processed.

<?php
/*
example of usage:

inside your form
<input type="text" name="validator" id="validator" size="4" />
<img src="random.php" alt="CAPTCHA image" width="60" height="20" vspace="1" align="top" />

and test the value of the "validator" form field like:
if (!empty($_POST['validator']) && $_POST['validator'] == $_SESSION['rand_code']) {
    process your form here
    at least destroy the session
    unset($_SESSION['rand_code']);
*/

// save this code in your random script
session_start();

if (empty($_SESSION['rand_code'])) {
    $str = "";
    $length = 0;
    for ($i = 0; $i < 4; $i++) {
        // this numbers refer to numbers of the ascii table (small-caps)
        $str .= chr(rand(97, 122));
    }
    $_SESSION['rand_code'] = $str;
}

$imgX = 60;
$imgY = 20;
$image = imagecreatetruecolor(60, 20);

$backgr_col = imagecolorallocate($image, 238,239,239);
$border_col = imagecolorallocate($image, 208,208,208);
$text_col = imagecolorallocate($image, 46,60,31);

imagefilledrectangle($image, 0, 0, 60, 20, $backgr_col);
imagerectangle($image, 0, 0, 59, 19, $border_col);

$font = "VeraSe.ttf"; // it's a Bitstream font check www.gnome.org for more
$font_size = 10;
$angle = 0;
$box = imagettfbbox($font_size, $angle, $font, $_SESSION['rand_code']);
$x = (int)($imgX - $box[4]) / 2;
$y = (int)($imgY - $box[5]) / 2;
imagettftext($image, $font_size, $angle, $x, $y, $text_col, $font, $_SESSION['rand_code']);

header("Content-type: image/png");
imagepng($image);
imagedestroy ($image);
?>
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS