Pagina 1 din 1

Tutorial avatar random

Scris: 15-Iun-2007, 17:56:30
de Marian96
Am facut acest tutorial pentru ca multi se intreaba cum se face acest lucru si ori nu il fac bine ori nu ii reusesc.

1)Creati un folder pe ftp.

2)Salvati asta intr-un fisier .php cu numele index.php

Cod: Selectaţi tot

<?php
$folder = ''; 
$exts = 'jpg jpeg png gif'; 
$files = array(); $i = -1; 
if ('' == $folder) $folder = './'; 
$handle = opendir($folder); 
$exts = explode(' ', $exts); 
while (false !== ($file = readdir($handle))) { 
foreach($exts as $ext) { 
if (preg_match('/\.'.$ext.'$/i', $file, $test)) { 
$files[] = $file; 
++$i; 
} } } 
closedir($handle); 
mt_srand((double)microtime()*1000000); 
$rand = mt_rand(0, $i); 
header('Location: '.$folder.$files[$rand]); 
?>
2)Editati sau creeati fisierul .httaccess cu urmatoarele linii:

Cod: Selectaţi tot

RewriteEngine ON

RewriteRule ^poza.gif/$ index.php
RewriteRule ^poza.gif$ index.php
3)Aceste 2 fisiere le puneti in folderul care l-ati creat pe ftp.

4)Puneti imaginile cu avatare in folderul care l-ati creat.

5)Numiti folderul:
cevretivoi.jpg

Cam asta a fost tot.
ATENTIE:SCRIPTUL RANDOM NU MERGE PE HOSTURI CARE NU AU MOD_REWRITE ACTIVAT

Scris: 15-Iun-2007, 18:18:32
de octaviansan
Sau aici mai simplu.

Scris: 15-Iun-2007, 19:56:43
de Marian96
Aici e mai detaliat :)

Scris: 23-Iun-2007, 17:20:35
de 53rg1u
poi da si eu la campu' imagine asociata de la profil ce bag ??

Scris: 23-Iun-2007, 18:04:10
de orynider
O să vă placă şi scriptul meu cu GD:

1. Creiaţi un fişier image.php care îl puneţi în root:

Cod: Selectaţi tot

<?php
header("Content-type: image/png");

$avatars[] = "avatars/image.png"; 
$avatars[] = "avatars/image1.png"; 
$avatars[] = "avatars/image2.png"; 
$avatars[] = "avatars/image3.png"; 
$avatars[] = "avatars/image4.png";
$avatars[] = "avatars/image5.png"; 

$avatar = $avatars[mt_rand(0, count($avatars)-1)];

$newavatar = imagecreatefrompng($avatar);
imagepng($newavatar);
imagedestroy($newavatar);
?>
2. Creeaţi un folder "avatars" in root (rădăcina site-ului), şi uploadaţi imaginile PNG cu avatare.

3. Editati sau creeati fisierul .httaccess cu urmatoarele linii:

Cod: Selectaţi tot

RewriteEngine On
RewriteRule image.png image.php
4. În profil scrieţi http://numesite.ro/image.png

Notă: Imaginea image.png va fi validă doar dacă MOD_REWRITE este suporttat de serverul pe care găzduiţi site-ul. Pentru a verifica dacă scriptul fucţionează intrţi în http://numesite.ro/image.php şi daţi refresh.

edit:

Am mai perfecţionat codul pentru image.php:

Cod: Selectaţi tot

<?php

$fullpath = 'avatars/'; //directorul cu avatare
$thumbnail_size = 110; //mărimea maximă admisă a avatarului în pixeli

if( isset($_GET['pic_name']) )
{
	$pic_name = $_GET['pic_name'];
}
else
{
	$avatars[] = "image.png"; 
	$avatars[] = "image1.png"; 
	$avatars[] = "image2.png"; 
	$avatars[] = "image3.png"; 
	$avatars[] = "image4.png";
	$avatars[] = "image5.png"; 

	$pic_name = $avatars[mt_rand(0, count($avatars)-1)];
}

 
$pic_fullpath = $fullpath . $pic_name; 
$pic_filetype = strtolower(substr($pic_name, strlen($pic_name) - 4, 4));

switch ($pic_filetype)
{
	case '.gif':
		$file_header = 'Content-type: image/gif';
		$read_function = 'imagecreatefromgif';
		break;
	case '.jpeg':
	case '.jpg':
	case '.pjpeg':
		$file_header = 'Content-type: image/jpeg';
		$read_function = 'imagecreatefromjpeg';
		break;
	case '.png':
		$file_header = 'Content-type: image/png';
		$read_function = 'imagecreatefrompng';
		break;
	default:
		header('Content-type: image/jpeg');
		$file_header = 'Content-type: image/jpeg';
		$read_function = 'imagecreatefromjpeg';
}


header($file_header);

$pic_size = @getimagesize($pic_fullpath);
$pic_width = $pic_size[0];
$pic_height = $pic_size[1];


if ($pic_width > $pic_height)
{
	$thumbnail_width = $thumbnail_size;
	$thumbnail_height = $thumbnail_size * ($pic_height / $pic_width);
}
else
{
	$thumbnail_height = $thumbnail_size;
	$thumbnail_width = $thumbnail_size * ($pic_width / $pic_height);
} 

$thumbnail = @imagecreatetruecolor($thumbnail_width, $thumbnail_height);

$resize_function = 'imagecopyresampled';

$source = $read_function($pic_fullpath);

@$resize_function($thumbnail, $source, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $pic_width, $pic_height);


switch ( $pic_filetype )
{
	case '.gif':
		imagegif($thumbnail);
		break;
	case '.jpeg':
	case '.jpg':
	case '.pjpeg':
		imagejpeg($thumbnail);
		break;
	case '.png':
		imagepng($thumbnail);
		break;
	default:
		return false;
}

imagedestroy($thumbnail);

?>
Acum ar mai trebui unite cele două abordări a lui Marin şi a mea să ia şi pozele automat dintr-un folder şi să le şi redimensioneze automat ...

edit: Gata:

Cod: Selectaţi tot

<?php

//$fullpath = 'Poze_Masini/Novedad/original/';
$fullpath = 'avatars/';
$thumb_size = 120;


$exts = 'jpg jpeg png gif';

if ( empty($fullpath) )
{ 
	$fullpath = './';
}

if( isset($_GET['pic_size']) )
{
	$thumbnail_size = ( ($_GET['pic_size'] < '256') ? $_GET['pic_size'] : '256' );
}
else
{
	$thumbnail_size = ( !empty($thumb_size) ? $thumb_size : '120' );
}

if( isset($_GET['pic_name']) )
{
	$pic_name = $_GET['pic_name'];
}
else
{
	$avatars = array(); $i = -1;
 
	$handle = opendir($fullpath);

	$exts = explode(' ', $exts);
 
	while (false !== ($file = readdir($handle))) 
	{ 
		foreach($exts as $ext) { 
			if (preg_match('/\.'.$ext.'$/i', $file, $test)) 
			{ 
				$avatars[] = $file; 
				++$i; 
			} 
		}
 	} 
	closedir($handle);

	// $pic_name = $avatars[mt_rand(0, count($avatars)-1)];
	$pic_name = $avatars[mt_rand(0, $i)];
}

 
$pic_fullpath = $fullpath . $pic_name; 
$pic_filetype = strtolower(substr($pic_name, strlen($pic_name) - 4, 4));

switch ($pic_filetype)
{
	case '.gif':
		$file_header = 'Content-type: image/gif';
		$read_function = 'imagecreatefromgif';
		break;
	case '.jpeg':
	case '.jpg':
	case '.pjpeg':
		$file_header = 'Content-type: image/jpeg';
		$read_function = 'imagecreatefromjpeg';
		break;
	case '.png':
		$file_header = 'Content-type: image/png';
		$read_function = 'imagecreatefrompng';
		break;
	default:
		header('Content-type: image/jpeg');
		$file_header = 'Content-type: image/jpeg';
		$read_function = 'imagecreatefromjpeg';
}


header($file_header);

$pic_size = @getimagesize($pic_fullpath);
$pic_width = $pic_size[0];
$pic_height = $pic_size[1];


if ($pic_width > $pic_height)
{
	$thumbnail_width = $thumbnail_size;
	$thumbnail_height = $thumbnail_size * ($pic_height / $pic_width);
}
else
{
	$thumbnail_height = $thumbnail_size;
	$thumbnail_width = $thumbnail_size * ($pic_width / $pic_height);
} 

$thumbnail = @imagecreatetruecolor($thumbnail_width, $thumbnail_height);

$resize_function = 'imagecopyresampled';

$source = $read_function($pic_fullpath);

@$resize_function($thumbnail, $source, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $pic_width, $pic_height);


switch ( $pic_filetype )
{
	case '.gif':
		imagegif($thumbnail);
		break;
	case '.jpeg':
	case '.jpg':
	case '.pjpeg':
		imagejpeg($thumbnail);
		break;
	case '.png':
		imagepng($thumbnail);
		break;
	default:
		return false;
}

imagedestroy($thumbnail);

?>
Demo:
:: Normal image3.php
:: _GET image.php?pic_name=ory_84_100_summer2.jpg
:: Rewrite image3.png
:: Resize image3.png?pic_size=200

Scris: 01-Iul-2007, 21:58:38
de octaviansan
Parca al tau e mai complex .

Scris: 01-Iul-2007, 22:55:43
de orynider
E mai complexă untima variantă dar are mai multe facilităţi.

Scris: 10-Iul-2007, 11:41:17
de mariushca
da... :lol:

Scris: 10-Iul-2007, 11:47:11
de CaTaNhA
Marian, ziceai ca e facut de tine ?

http://photomatt.net/scripts/randomimage/

Scris: 10-Iul-2007, 22:37:41
de Marian96
e facut de mine ti-am zis si pe myforum ca scripturile nu sunt facute de mine dar tutorialul eu l-am creat
edit:lex eu sunt dkk de pe forumu bimbizone ca sa stii :D