phpBB Italia chiude!
phpBB Italia ringrazia tutti gli utenti che hanno dato fiducia al nostro progetto per ben 9 anni, e che, grazie al grande lavoro fatto da tutto lo Staff (rigorosamente a titolo gratuito), hanno portato il portale a diventare il principale punto di riferimento italiano alla piattaforma phpBB.

Purtroppo, causa motivi personali, non ho più modo di gestirlo e portarlo avanti. Il forum viene ora posto in uno stato di sola lettura, nonché un archivio storico per permettere a chiunque di fruire di tutte le discussioni trattate.

Il nuovo portale di assistenza per l'Italia di phpBB diventa phpBB-Store.it, cui ringrazio per aver deciso di portare avanti questo grande progetto.

Grazie ancora,
Carlo - Amministratore di phpBB Italia

Resize Avatar

Area di discussione relativa al linguaggio di programmazione web più conosciuto. Il forum è dedicato anche a MySQL, la piattaforma di database più utilizzata con il PHP.
Rispondi
Avatar utente
Haku
Utente
Utente
Messaggi: 2529
Iscritto il: 22/09/2009, 22:36
Sesso: Maschio
Versione: 3.0.9
Server: UNIX/Linux
Contatta:

Resize Avatar

Messaggio da Haku » 28/06/2010, 18:51

Ho trovato questo codice che permette di fare un auto resize dell'avatar se questo è troppo grosso:

Codice: Seleziona tutto

<?php
class Image
{
	private $check_ext;
	private $max_width, $max_height, $max_filesize;
	
	private $img;
	private $infos;
	
	private $q, $lo_q;
	
	function Image($img)
	{
		$this->img = $img;
		$this->check_ext = array("jpg", "jpe", "jpeg", "gif", "png");
		
		$this->get_infos();
	}
	
	function set_max_height($mh)
	{
		$this->max_height = $mh;
	}
	
	function no_max_height()
	{
		$this->max_height = 0;
	}
	
	function set_max_width($mw)
	{
		$this->max_width = $mw;
	}
	
	function no_max_width()
	{
		$this->max_width = 0;
	}
	
	function set_max_filesize($ms)
	{
		$this->max_filesize = $ms;
	}
	
	function no_max_filesize()
	{
		$this->max_filesize = 0;
	}
	
	function ext_accept($extensions) // str
	{
		$this->check_ext = explode(",", $extensions);
	}
	
	function get_extension()
	{
		$filename = basename($this->img);
		$ext = substr($filename, strrpos($filename, "."), strlen($filename));
		return $ext;
	}
	
	function get_content_type()
	{
		if(empty($this->infos))
		{
			$this->get_infos();
		}
		
		if($this->infos['width']!=0 && $this->infos['height']!=0)
		{
			return image_type_to_mime_type($this->infos['type']);
		}
		else
		{
			return false;
		}
	}
	
	function get_infos()
	{
		list($this->infos['width'], $this->infos['height'], $this->infos['type']) = getimagesize($this->img);
		$this->max_width = 0;
		$this->max_height = 0;
		$this->min_width = 10;
		$this->q = 100;
		$this->lo_q = 30;
	}
	
	function get_new_dimensions($width, $height, $max_width, $max_height = 0)
	{
		$new_width = $width;
		$new_height = $height;
		
		if($max_height!=0 && $max_width==0)
		{
			if($height > $max_height)
			{
				$new_height = $max_height;
				$new_width = round($width * $max_height / $height, 1);
			}
		}
		else if($max_width!=0 && $max_height==0)
		{
			if($width > $max_width)
			{
				$new_width = $max_width;
				$new_height = round($height * $max_width / $width, 1);
			}
		}
		else
		{
			if($max_height!=0 && $max_width!=0)
			{
				if($width >= $height
				&& $width >= $max_width)
				{
					$new_width = $max_width;
					$new_height = round($height * $new_width / $width, 1);
					if($new_height > $max_height)
					{
						$new_height2 = $max_height;
						$new_width2 = round($new_width * $new_height2 / $new_height, 1);
						$new_height = $new_height2;
						$new_width = $new_width2;
					}
				}
				else if($height >= $width
				&& $height >= $max_height)
				{
					$new_height = $max_height;
					$new_width = round($width * $new_height / $height, 1);
					if($new_width > $max_width)
					{
						$new_width2 = $max_width;
						$new_height2 = round($new_height * $new_width2 / $new_width, 1);
						$new_width = $new_width2;
						$new_height = $new_height2;
					}
				}
			}
		}
		
		return array($new_width, $new_height);
	}
	
	function create_thumb($source, $w_source, $h_source, $th_width, $th_height)
	{
		$thumb = $this->generate_thumb($source, $w_source, $h_source, $th_width, $th_height);
		
		// Reduce filesize
		if($this->max_filesize)
		{
			$thumb = $this->reduce_filesize($source, $thumb, $w_source, $h_source, $th_width, $th_height);
		}
		
		return $thumb;
	}
	
	function generate_thumb($source, $w_source, $h_source, $th_width, $th_height)
	{
		$thumb = imagecreatetruecolor($th_width, $th_height);
		imagecopyresampled($thumb, $source, 0, 0, 0, 0, $th_width, $th_height, $w_source, $h_source);
		return $thumb;
	}
	
	function get_filesize($img, $remote = 0)
	{
		if($remote)
		{
			// Not available yet
			return 0;
		}
		else
		{
			return filesize($img);
		}
	}
	
	function get_thumb_filesize($thumb)
	{
		$img = md5(time());
		switch($this->get_content_type())
		{
			case "image/jpeg":
				imagejpeg($thumb, $img, $this->q);
			break;
			case "image/gif";
				imagegif($thumb, $img);
			break;
			case "image/png";
				imagepng($thumb, $img);
			break;
		}
		if(file_exists($img))
		{
			$fs = $this->get_filesize($img);
			unlink($img);
			return $fs;
		}
		else
		{
			return 0;
		}
	}
	
	function reduce_filesize($source, $thumb, $w_source, $h_source, $th_width, $th_height)
	{
		$img = $this->img;
		switch($this->get_content_type())
		{
			case "image/jpeg":
				$tmp_imgs = array();
				
				do
				{
					$img = md5($img);
					$tmp_imgs[] = $img;
					imagejpeg($thumb, $img, $this->q);
					$this->q--;
				}
				while($this->get_filesize($img)>$this->max_filesize && $this->q>$this->lo_q);
				$last_filesize = $this->get_filesize($img);
				foreach($tmp_imgs as $ti)
				{
					unlink($ti);
				}
				
				// After reducing image quality, the filesize is still bigger than the limit 
				if($last_filesize>$this->max_filesize)
				{
					return $this->reduce_dimensions_by_filesize($source, $w_source, $h_source, $last_filesize);
				}
				else
				{
					return $thumb;
				}
			break;
			case "image/gif";
				return $thumb;
			break;
			case "image/png";
				return $thumb;
			break;
		}
	}
	
	function reduce_dimensions_by_filesize($source, $w_source, $h_source, $filesize)
	{
		$max_width = $this->max_width;
		
		while($filesize>$this->max_filesize && $max_width>$this->min_width)
		{
			$max_width -= 5;
			list($new_width, $new_height) = $this->get_new_dimensions($w_source, $h_source, $max_width, 0);
			$thumb = $this->generate_thumb($source, $w_source, $h_source, $new_width, $new_height);
			$filesize = $this->get_thumb_filesize($thumb);
		}
		
		return $thumb;
	}
	
	function resize($save_to_source = 1)
	{
		list($new_width, $new_height) = $this->get_new_dimensions($this->infos['width'], $this->infos['height'], $this->max_width, $this->max_height);
		if($new_height!=$this->infos['height'] || $new_width!=$this->infos['width'])
		{
			switch($this->get_content_type())
			{
				case "image/jpeg":
					$source = imagecreatefromjpeg($this->img);
					$thumb = $this->create_thumb($source, $this->infos['width'], $this->infos['height'], $new_width, $new_height);
					
					if($save_to_source)
					{
						imagejpeg($thumb, $this->img, $this->q);
						return true;
					}
					else
					{
						header("Content-Type: ".$this->get_content_type());
						imagejpeg($thumb, null, $this->q);
					}
				break;
				case "image/gif":
					$source = imagecreatefromgif($this->img);
					$thumb = $this->create_thumb($source, $this->infos['width'], $this->infos['height'], $new_width, $new_height);
					
					if($save_to_source)
					{
						imagegif($thumb, $this->img);
						return true;
					}
					else
					{
						header("Content-Type: ".$this->get_content_type());
						imagegif($thumb);
					}
				break;
				case "image/png":
					$source = imagecreatefrompng($this->img);
					$thumb = $this->create_thumb($source, $this->infos['width'], $this->infos['height'], $new_width, $new_height);
					
					if($save_to_source)
					{
						imagepng($thumb, $this->img);
						return true;
					}
					else
					{
						header("Content-Type: ".$this->get_content_type());
						imagepng($thumb);
					}
				break;
				default:
					// Do nothing
					return false;
			}
		}
	}
}
?>
Solo che purtroppo non ridimensiona i file png e gif...come si può risolvere?

Avatar utente
Carlo
Amministratore
Amministratore
Messaggi: 9957
Iscritto il: 19/04/2009, 10:24
Sesso: Maschio
Versione: 3.2.0
Server: UNIX/Linux
PHP: 7.1.0
Database: MySQL(i) 10.0.27-MariaDB-cll-lve
Località: Puglia
Contatta:

Re: Resize Avatar

Messaggio da Carlo » 28/06/2010, 20:32

Mhm... cerca nel forum. Mi ricordo che un paio di mesi fa allegai un codice per ridimensionare le immagini in tutto il forum quando queste sono più grandi di una larghezza impostata.
MODs | Stili | Traduzioni MOD
Ogni MP contenente una richiesta di supporto verrà ignorato.

Avatar utente
Micogian
Leader Programmatori
Leader Programmatori
Messaggi: 3704
Iscritto il: 07/01/2010, 8:51
Versione: 3.2.0
Server: UNIX/Linux
PHP: 5.4.36
Database: MySQL 5.1.70-log
Località: Udine
Contatta:

Re: Resize Avatar

Messaggio da Micogian » 28/06/2010, 21:34

Il codice che si riferisce all'Avatar è il seguente, nel file viewtopic.php:

Codice: Seleziona tutto

'avatar'      => ($user->optionget('viewavatars')) ? get_user_avatar($row['user_avatar'], $row['user_avatar_type'], $row['user_avatar_width'], $row['user_avatar_height']) : '',
Si tratta di stabilire una dimensione massima e attraverso una condizione "IF" ridimensionare l'avatar.
Mettiamo che la dimensione sia 120pixel, si potrebbe fare una cosa del genere:

Codice: Seleziona tutto

if($row['user_avatar_width'] > '120'){
$avatar_width = '120' ;
$avatar_height = ($row['user_avatar_height'] * '120' / ($row['user_avatar_width']) ;
} 
In questo caso la variabile precedente diventerebbe:

Codice: Seleziona tutto

'avatar'      => ($user->optionget('viewavatars')) ? get_user_avatar($row['user_avatar'], $row['user_avatar_type'], $avatar_width, $avatar_height) : '',
}

Avatar utente
Haku
Utente
Utente
Messaggi: 2529
Iscritto il: 22/09/2009, 22:36
Sesso: Maschio
Versione: 3.0.9
Server: UNIX/Linux
Contatta:

Re: Resize Avatar

Messaggio da Haku » 28/06/2010, 22:02

Cioè bingo questa modifica mi fa un uto resize degli avatar superiori a 120 pixel portandoli a 120?
Perchè la modifica da me postata funziona impostando poi i parametri dal PCA,solo che:
Solo che purtroppo non ridimensiona i file png e gif...come si può risolvere?

Avatar utente
Micogian
Leader Programmatori
Leader Programmatori
Messaggi: 3704
Iscritto il: 07/01/2010, 8:51
Versione: 3.2.0
Server: UNIX/Linux
PHP: 5.4.36
Database: MySQL 5.1.70-log
Località: Udine
Contatta:

Re: Resize Avatar

Messaggio da Micogian » 29/06/2010, 9:05

Non l'ho provato ma in via teorica dovrebbe funzionare, in fondo, se modifichi il valore width e height nel tag <img> di una immagine la visualizzi con i valori inseriti.
Puoi fare una semplice prova: individua nella tabella "users" un avatar del tipo gif o png che superi una determinata dimensione. (oppure inserisci un tuo nuovo Avatar gif o png di dimensioni superiori alla norma)
Modifichi le dimensioni di user_avatar_width e user_avatar_height tenendo presente che per avere la giusta proporzione devi fare questo calcolo (se la larghezza deve essere 120 px):

width : 120 = height : x
ne consegue che il nuovo valore di height sarà = 120 * height / width
Se le attuali misure sono width=150 e height=140 e vuoi portare a 120 la larghezza, l'operazione da fare è questa:
new_height= 120 * 140 / 150 e cioè height=112
In sostanza la nuova dimensione sarà 120x112 pixel al posto di 150x140

Con questa modifica dovresti visualizzare gli avatar con la dimensione indicata nella tabella users, indipendentemente dalla reale dimensione del file e del tipo di immagine.

Se questo funziona hai due possibilità: inserire una condizione IF nel file viewtopic.php nel codice che crea la variabile 'avatar' come spiegato all'inizio oppure modificare manualmente la tabela user.
Questo per gli Avatar attuali, se gli over-size non sono troppi.
Per i futuri Avatar metti un limite alle dimensioni nel PCA .

ilsuonodelcuore
Utente
Utente
Messaggi: 24
Iscritto il: 18/10/2013, 20:08
Sesso: Maschio
Versione: 3.0.12
Server: UNIX/Linux

Re: Resize Avatar

Messaggio da ilsuonodelcuore » 19/10/2013, 10:01

Ma dove trovo il file viewtopic.php? Se vado su template lo vedo...ma quando ci clicco non mi fa entrare su viewtopic.php.

Avatar utente
brunino
Moderatore
Moderatore
Messaggi: 2819
Iscritto il: 12/01/2013, 16:35
Sesso: Maschio
Versione: 3.1.2
Server: UNIX/Linux
PHP: php 5.3
Database: mysql
Località: Toscana, Firenze
Contatta:

Re: Resize Avatar

Messaggio da brunino » 19/10/2013, 11:29

Il file viewtopic.php si trova nella cartella principale in cui è istallato il forum, ovvero dove si trova anche la index.php
E' sempre gradito un grazie

Creo estensioni per phpBB 3.1 su misura: contattami via MP.
Mie Estensioni: Top Ten Topics | Ban List | Topic List
Visita il mio forum di test e sviluppo stili e estensioni phpBB 3.1

Rispondi

Torna a “PHP - MySQL”

Chi c’è in linea

Visitano il forum: Nessuno e 44 ospiti