Problem tłem przy zmniejszaniu obrazków

0

Witam serdecznie,
mam taki kod:

function createThumbnail($path, $imagename, $rozmiar) {      /////// tworzymy miniaturkę
	$wynik = explode(".", $imagename);
	$type = $wynik[1];

	$site_max_thumbnails_size = $rozmiar; // max size
	if ($type== "jpg" || $type== "jpeg"){
	$img = imagecreatefromjpeg( "{$path}{$imagename}" );
	}
	if ($type== "png"){
	$img = imagecreatefrompng( "{$path}{$imagename}" );
	}
	if ($type== "gif"){
	$img = imagecreatefromgif( "{$path}{$imagename}" );
	}
	$width = imagesx( $img );
	$height = imagesy( $img );

	// calculate thumbnail size
	if ($height>=$width) {
		$smallerSize=$width;
		$croppingStartHeight=($height-$smallerSize)/2;
		$croppingStartWidth=0;
	}
	else  {
		$smallerSize=$height;
		$croppingStartHeight=0;
		$croppingStartWidth=($width-$smallerSize)/2;	}
	$croppedImage=imagecreatetruecolor($smallerSize, $smallerSize); // create square image
	$resizedImage=imagecreatetruecolor($site_max_thumbnails_size, $site_max_thumbnails_size);
	imagecopy($croppedImage, $img, 0, 0, $croppingStartWidth, $croppingStartHeight, $smallerSize, $smallerSize); // build cropped, square image
	imagecopyresampled ($resizedImage, $croppedImage, 0, 0, 0, 0, $site_max_thumbnails_size, $site_max_thumbnails_size, $smallerSize, $smallerSize); // copy and resize old image into new image
	$pathToThumbs=$path."";
	if ($type== "jpg" || $type== "jpeg"){
	if (!imagejpeg($resizedImage, "{$pathToThumbs}mini_{$imagename}" )) // save thumbnail into a file
		$result='Nie można zapisać miniaturki w folderze docelowym!';
		ImageDestroy($croppedImage);  
	ImageDestroy($resizedImage);
	return $result;
	}
	if ($type== "png"){
	if (!imagepng($resizedImage, "{$pathToThumbs}mini_{$imagename}" )) // save thumbnail into a file
		$result='Nie można zapisać miniaturki w folderze docelowym!';
		ImageDestroy($croppedImage);  
	ImageDestroy($resizedImage);
	return $result;
	}
	if ($type== "gif"){
	if (!imagegif($resizedImage, "{$pathToThumbs}mini_{$imagename}" )) // save thumbnail into a file
		$result='Nie można zapisać miniaturki w folderze docelowym!';
		ImageDestroy($croppedImage);  
	ImageDestroy($resizedImage);
	return $result;
	}  
} 

przy wgrywaniu i obróbce przeźroczystych PNG mam czarne tło :( jak pozostawić przeźroczyste po zmniejszeniu?

Z góry dziękuje za pomoc,
Northwest

0

Wujek goole skierował mnie do manuala a on do: http://pl.php.net/manual-lookup.php?pattern=transparent&lang=pl

0

kurcze, nie wiem jak to zmienić :( a jak ustawić np. domyslnie zielone tło zamiast czarnego?

0

Twojego kodu nie bede analizowal, poniewaz nie jest zbyt przyjrzysty. Natomiast moge pomoc ci w generowaniu miniaturek, wraz z wyborem koloru tla:

<?php

/**
 * Biblioteka testowa
 */
class Image
{
	const JPG			=	'jpg';
	const JPEG			=	'jpeg';
	const GIF			=	'gif';
	const PNG			=	'png';
	
	private $handle;
	private $width;
	private $height;
	private $imageType;
	
	function __construct($fileName = '')
	{
		if ($fileName)
		{
			$this->open($fileName);
		}
	}
	
	function __destruct()
	{
		if ($this->handle)
		{
			$this->close();
		}
	}
	
	public function setImageType($imageType)
	{
		$this->imageType = $imageType;
		return $this;
	}
	
	public function getImageType()
	{
		return $this->imageType;
	}
	
	public function create($imageType, $width, $height)
	{
		$this->imageType = $imageType;
		$this->handle = imagecreatetruecolor($width, $height);

		$this->width = $width;
		$this->height = $height;
		return $this;
	}

	/**
	 * Otwarcie obrazu
	 * @param string $fileName Sciezka/nazwa pliku
	 */
	public function open($fileName)
	{
		if (!file_exists($fileName))
		{
			throw new Exception("$fileName does not exist");
		}
		
		$this->imageType =  strtolower(end(explode('.', $fileName)));
		list($this->width, $this->height, , ,) = @getimagesize($fileName);

		switch ($this->imageType)
		{
			case 'jpeg':
			case 'jpg':				
				$this->handle = @imagecreatefromjpeg($fileName);  
			break;

			case 'gif':
				$this->handle = @imagecreatefromgif($fileName);
			break;

			case 'png':
				$this->handle = @imagecreatefrompng($fileName);
			break;
			
			default:
				throw new Exception('Unsupported image format');
		}
	}

	public function getHandle()
	{
		return $this->handle;
	}

	/**
	 * Zamyka uchwyt i konczy prace z obrazem
	 */
	public function close()
	{
		@imagedestroy($this->handle);
	}
	
	/**
	 * Zwraca szerokosc obrazu
	 * @return int
	 */
	public function getWidth()
	{
		return $this->width;
	}
	
	/**
	 * Zwraca wysokosc obrazu
	 * @return int
	 */
	public function getHeight()
	{
		return $this->height;
	}
	
	/**
	 * Zmiara rozmiaru obrazu do wartosci podanych w parametrach
	 * @param int $width 
	 * @param int $height
	 */
	public function resize($width, $height)
	{
		$image = @imagecreatetruecolor($width, $height);
		@imagecopyresampled($image, $this->handle, 0, 0, 0, 0, $width, $height, $this->width, $this->height);

		$this->handle = $image;
		$this->width = $width;
		$this->height = $height;
	}
	
	/**
	 * Konwersja do systemu szestnastkowego
	 * @param int $red 
	 * @param int $green
	 * @param int $blue
	 * @return string
	 */
	public function rgb2hex($red, $green, $blue)
	{
		 return sprintf('#%02s%02s%02s', dechex($red), dechex($green), dechex($blue));
	}
	
	/**
	 * Konwersja liczby szestnastkowej okreslajacej kolor, do tablicy liczb RGB
	 * @param string $hex np. #ccc lub #f8f8f8
	 * @return array
	 */
	public function hex2rgb($hex)
	{
		if ($hex{0} == '#')
		{
			$hex = substr($hex, 1);
		}
		if (strlen($hex) == 6)
		{
			list($red, $green, $blue) = array(substr($hex, 0, 2), substr($hex, 2, 2), substr($hex, 4, 2));
		}
		elseif (strlen($hex) == 3)
		{
			list($red, $green, $blue) = array($hex[0] . $hex[0], $hex[1] . $hex[1], $hex[2] . $hex[2]);
		}
		else
		{
			return false;
		}
		
		return array(hexdec($red), hexdec($green), hexdec($blue));
	}
	
	/**
	 * Metoda generuje miniature o podanych rozmiarach
	 * @param int $width Szerokosc w px
	 * @param int $height Wysokosc w px
	 * @param strig $color Kolor tla dla miniatury
	 */
	public function thumbnail($width, $height, $color = '#FFF')
	{
		// jezeli wysokosc jest MNIEJSZA niz szerokosc		
		// obraz poziomy
		if ($this->height < $this->width)
		{
			$ratio = $this->getHeight() / $this->getWidth();
			$cHeight = $width * $ratio;
			
			$this->resize($width, $cHeight);
		}
		// obraz pionowy
		else
		{
			$ratio = $this->getWidth() / $this->getHeight();
			$cWidth = $height * $ratio;
			
			$this->resize($cWidth, $height);			
		}
		
		list($red, $green, $blue) = $this->hex2rgb($color);
		$output = imagecreatetruecolor($width, $height);
		$color = imagecolorallocate($output, $red, $green, $blue);		
		imagefill($output, 0, 0, $color);
				
		imagecopy($output, $this->handle, round(($width - $this->getWidth()) / 2), round(($height - $this->getHeight()) / 2), 0, 0, $this->getWidth(), $this->getHeight());

		$this->handle = $output;		
	}

	/**
	 * Zapis obrazu do pliku
	 * @param $fileName Nazwa (sciezka) pliku
	 */
	public function save($fileName)
	{
		switch ($this->imageType)
		{
			case 'jpg':
			case 'jpeg':	
				 @imagejpeg($this->handle, $fileName, 80);
			break;

			case 'gif':
				@imagegif($this->handle, $fileName);
			break;

			case 'png':
				@imagepng($this->handle, $fileName);
			break;
		}
	}
}
?>

Teraz, wykorzystanie tej klasy:

$image = new Image('foo.png');
$image->thumbnail(150, 150, '#CCC');
$image->save('bar.png'); // miniatura
$image->close();

Zrodlo kodu wziety stad: http://redmine.boduch.net/projects/coyote/repository/entry/trunk/lib/image.class.php

0

kurcze, przy wgrywaniu przeźroczystych plików mam czarny kolor wokół zdjęcia + kolor wybranej obwódki... :/ czyli ciągle ten sam problem

1 użytkowników online, w tym zalogowanych: 0, gości: 1