Pagina 1 di 1

Resize Avatar

Inviato: 28/06/2010, 18:51
da Haku
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?

Re: Resize Avatar

Inviato: 28/06/2010, 20:32
da Carlo
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.

Re: Resize Avatar

Inviato: 28/06/2010, 21:34
da Micogian
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) : '',
}

Re: Resize Avatar

Inviato: 28/06/2010, 22:02
da Haku
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?

Re: Resize Avatar

Inviato: 29/06/2010, 9:05
da Micogian
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 .

Re: Resize Avatar

Inviato: 19/10/2013, 10:01
da ilsuonodelcuore
Ma dove trovo il file viewtopic.php? Se vado su template lo vedo...ma quando ci clicco non mi fa entrare su viewtopic.php.

Re: Resize Avatar

Inviato: 19/10/2013, 11:29
da brunino
Il file viewtopic.php si trova nella cartella principale in cui è istallato il forum, ovvero dove si trova anche la index.php