script de floutage en GD, problème

Répondre


Cette question est un moyen d’empêcher des soumissions automatisées de formulaires par des robots.
Smileys
:D :) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: =D> #-o =P~ :^o :non: :priere: 8-|
Voir plus de smileys
  Revue du sujet
 

  Étendre la vue Revue du sujet : script de floutage en GD, problème

par VaN » 24 janv. 2007, 19:30

J'ai plutot bien avancé. J'ai réussi a enlever les bugs qui bloquait le script, et j'ai commencé a l'ajuster.

Voici l'image que j'arrive a obtenir, au bout de mes 3 etapes :

Image

Etape 1 : je récupere la grande photo, je la resample, et j'en copie un carré de 174*174 (vers le centre de la photo) dans une nouvelle image grise (que je modifierai en blanc tout a la fin) de 190*190, au centre, en laissant donc 8px de marge partout autour.

Etape 2 : A coté de ça, je crée mon ombre, sur une nouvelle image de 190*190

Etape 3 : Je floute l'ombre (utilise des fonctions qui bougent les pixels, ou change leur couleur, je sais pas trop)

Etape 4 : je merge les deux images, pour acoller l'ombre a la photo polaroid.

Comme on le voit sur l'image, le script du floutage bug encore, et me crée une sorte de bordure autour de l'image de l'ombre. je n'arrive pas à résoudre ce problème, quelqu'un peut t'il m'aider ?

Voici la totalité de mon script, pour ceux qui auraient le courage d'y jeter un oeil (je suis a peu près sur que l'erreur intervient sur l'étape du floutage, car l'ombre en elle même, quand je l'affiche a part, est nickel (sauf qu'elle est moche, car pas floutée).

Une solution alternative serait re copier cette image sur une nouvelle image de 199*199, et ainsi rogner la vilairen bordure, mais c'est pas très propre comme méthode : /
if(isset($_POST['submit']))
{
	// OETAPE 1 : ON RECUPERE LE CADRAGE DE LA PHOTO ORIGINALE, ON LA COPIE SUR UNE BASE POLAROID
	// on recupère la taille de l'image source
	list($width, $height, $type, $attr) = getimagesize($_FILES['photo']['tmp_name']);			
	$new_w = ($width / 2) - 100;
	$new_h = ($height / 2) - 100;
	//echo $new_w;
	
	$dst_img = imagecreatetruecolor(800, 600);
	imagecolorallocate($dst_img, 255, 255, 255);
	
	imagecopyresampled($dst_img, imagecreatefromjpeg($_FILES['photo']['tmp_name']), 0, 0, 0, 0, 800, 600, $width, $height);
			
	// nouvelle image 190*190
	$im = imagecreatetruecolor(190, 190);
	$white = imagecolorallocatealpha($im, 200, 200, 200, 150);
	imagefill($im, 0, 0, $white);
	imagecopy($im, $dst_img, 8, 8, 300, 200, 174, 174);
	
	// ETAPE 2 : ON CREE L'OMBRE SUR UN 2e FICHIER (succession de carré qui devient de plus en plus clair
	/* offset of drop shadow from top left */
	define("DS_OFFSET", 5);
	 
	/* number of steps from black to background color */
	define("DS_STEPS", 10);
	
	/* distance between steps */
	define("DS_SPREAD", 1);
	
	/* define the background color */
	$background = array("r" => 255, "g" => 255, "b" => 255);
	
	//$src = isset($_REQUEST['src']) ? urldecode($_REQUEST['src']) : null;
	if(isset($_FILES['photo']['tmp_name']) && file_exists($_FILES['photo']['tmp_name'])) 
	{
		/* create a new canvas.  New canvas dimensions should be larger than the original's */
	  	//list($o_width, $o_height) = getimagesize($_FILES['photo']['tmp_name']);
	  	$width  = 197;
	  	$height = 197;
	  	$image = imagecreatetruecolor($width, $height);
		imagecolorallocatealpha($image, 255, 255, 255, 100);
	
	  	/* determine the offset between colors */
	  	$step_offset = array("r" => ($background["r"] / DS_STEPS), "g" => ($background["g"] / DS_STEPS), "b" => ($background["b"] / DS_STEPS));
	
	  	/* calculate and allocate the needed colors */
	  	$current_color = $background;
	  	for ($i = 0; $i <= DS_STEPS; $i++) 
		{
			$colors[$i] = imagecolorallocate($image, round($current_color["r"]), round($current_color["g"]), round($current_color["b"]));
	
			$current_color["r"] -= $step_offset["r"];
			$current_color["g"] -= $step_offset["g"];
			$current_color["b"] -= $step_offset["b"];
	  	}
	
	  	/* floodfill the canvas with the background color */
	  	imagefilledrectangle($image, 0,0, $width, $height, $colors[0]);
	
	  	/* draw overlapping rectangles to create a drop shadow effect */
	  	for ($i = 0; $i < count($colors); $i++) 
		{
			imagefilledrectangle($image, DS_OFFSET, DS_OFFSET, $width, $height, $colors[$i]);
			$width -= DS_SPREAD;
			$height -= DS_SPREAD;
	  	}
		/* output the image */
	 	//header("Content-type: image/jpeg");
	  	//imagepng($image);
		
		// ETAPE 3 : ON FAIT LE FLOUTAGE (ON BOUGE LES PIXELS)
		
		// Tiré de http://www.gamedev.net/reference/programming/features/imageproc/page2.asp
		
		$coeffs = array (
						array ( 1),
						array ( 1, 1), 
						array ( 1, 2, 1),
						array ( 1, 3, 3, 1),
						array ( 1, 4, 6, 4, 1),
						array ( 1, 5, 10, 10, 5, 1),
						array ( 1, 6, 15, 20, 15, 6, 1),
						array ( 1, 7, 21, 35, 35, 21, 7, 1),
						array ( 1, 8, 28, 56, 70, 56, 28, 8, 1),
						array ( 1, 9, 36, 84, 126, 126, 84, 36, 9, 1),
						array ( 1, 10, 45, 120, 210, 252, 210, 120, 45, 10, 1),
						array ( 1, 11, 55, 165, 330, 462, 462, 330, 165, 55, 11, 1)
						);
		
		$src = array();
		$src['index'] = 5;
		$src['sum'] = pow (2, $src['index']);
		
		// On se procure les infos sur l'image (taille/type mime)
		$src['infos'][0] = 197;
		$src['infos'][1] = 197;
		
		// On charge l'image de départ en fonction de son type
		$src['img'] = $image;
		
		$src['temp1'] = imagecreatetruecolor ($src['infos'][0], $src['infos'][1]);
		$src['temp2'] = imagecreatetruecolor ($src['infos'][0], $src['infos'][1]);
		// Traitement
		for ( $i=0 ; $i < $src['infos'][0] ; ++$i ) {
			for ( $j=0 ; $j < $src['infos'][1] ; ++$j ) {
				$sumr=0;
				$sumg=0;
				$sumb=0;
				for ( $k=0 ; $k <= $src['index'] ; ++$k ) {
					//$color = imagecolorat($src['img'], $i, $j);
					if( ($i-(($src['index'])/2)+$k) >= 0 && ($i-(($src['index'])/2)+$k) <= 197 )
					{
						$color = imagecolorat($src['img'], $i-(($src['index'])/2)+$k, $j);
						$r = ($color >> 16) & 0xFF;
						$g = ($color >> 8) & 0xFF;
						$b = ($color) & 0xFF;
						$sumr += $r*$coeffs[$src['index']][$k];
						$sumg += $g*$coeffs[$src['index']][$k];
						$sumb += $b*$coeffs[$src['index']][$k];
					}
					
				}
				$color = imagecolorallocate ($src['temp1'], $sumr/$src['sum'], $sumg/$src['sum'], $sumb/$src['sum'] );
				imagesetpixel($src['temp1'],$i,$j,$color);
			} 
		}
		imagedestroy($src['img']);
		
		for ( $i=0 ; $i < $src['infos'][0] ; ++$i ) {
			for ( $j=0 ; $j < $src['infos'][1] ; ++$j ) {
				$sumr=0;
				$sumg=0;
				$sumb=0;
				for ( $k=0 ; $k <= $src['index'] ; ++$k ) {
					//$color=imagecolorat($src['temp1'], $i, $j);
					if( ($j-(($src['index'])/2)+$k) >= 0 && ($j-(($src['index'])/2)+$k) <= 197 )
					{
						$color=imagecolorat($src['temp1'], $i, $j-(($src['index'])/2)+$k);
						$r=($color >> 16) & 0xFF;
						$g=($color >> 8) & 0xFF;
						$b=($color) & 0xFF;
						$sumr += $r*$coeffs[$src['index']][$k];
						$sumg += $g*$coeffs[$src['index']][$k];
						$sumb += $b*$coeffs[$src['index']][$k];
					}
					
				}
				$color = imagecolorallocate ($src['temp2'], $sumr/$src['sum'], $sumg/$src['sum'], $sumb/$src['sum'] );
				imagesetpixel($src['temp2'],$i,$j,$color);
			} 
		}
		imagedestroy($src['temp1']);
		
		//header("Content-Type: image/png");
		//imagepng($src['temp2']);
		//imagedestroy($src['temp2']);
		//readfile($src['cache_file']);
		
		// ETAPE 4 : ON MERGE LE POLAROID ET L'OMBRE
		
	  	/* overlay the original image on top of the drop shadow */
	  	//imagecopymerge($image, $im, 0,0, 0,0, 190, 190, 100);
		imagecopymerge($src['temp2'], $im, 0,0, 0,0, 190,190, 100);
	
	  	/* output the image */
	 	//header("Content-type: image/jpeg");
	  	//imagejpeg($image, "", 100);
	  
	  	/* clean up the image resources */
	  	//imagedestroy($image);
	  	//imagedestroy($original_image);
	}
	
	// fond blanc et texte bleu
	//$bg = imagecolorallocatealpha($im, 0, 0, 0, 0);
	//$textcolor = imagecolorallocatealpha($im, 0, 0, 0, 100);
	//imagestring($im, 5, 0, 80, "Hello !", $textcolor);
	
	
	
	// on applique une rotation random
	
	// affichage de l'image
	imagepng($src['temp2']);
}

script de floutage en GD, problème

par VaN » 24 janv. 2007, 17:11

Bonjour,

Je suis actuellement en train de créer des images à la volée avec GD. J'ai réussi a créer une ombre portée, mais j'aimerais aussi ajouter un flou sur cette ombre, pour la rendre plus esthétique.

J'ai trouvé un script qui devrait m'y aider, mais je rencontre un problème.
$coeffs = array (
						array ( 1),
						array ( 1, 1), 
						array ( 1, 2, 1),
						array ( 1, 3, 3, 1),
						array ( 1, 4, 6, 4, 1),
						array ( 1, 5, 10, 10, 5, 1),
						array ( 1, 6, 15, 20, 15, 6, 1),
						array ( 1, 7, 21, 35, 35, 21, 7, 1),
						array ( 1, 8, 28, 56, 70, 56, 28, 8, 1),
						array ( 1, 9, 36, 84, 126, 126, 84, 36, 9, 1),
						array ( 1, 10, 45, 120, 210, 252, 210, 120, 45, 10, 1),
						array ( 1, 11, 55, 165, 330, 462, 462, 330, 165, 55, 11, 1)
						);
		
		$src = array();
		$src['index'] = 8;
		$src['sum'] = pow (2, $src['index']);
		
		// On se procure les infos sur l'image (taille/type mime)
		$src['infos'][0] = 200;
		$src['infos'][1] = 200;
		
		// On charge l'image de départ en fonction de son type
		$src['img'] = $image;
		
		$src['temp1'] = imagecreatetruecolor ($src['infos'][0], $src['infos'][1]);
		$src['temp2'] = imagecreatetruecolor ($src['infos'][0], $src['infos'][1]);
		// Traitement
		for ( $i=0 ; $i < $src['infos'][0] ; ++$i ) {
			for ( $j=0 ; $j < $src['infos'][1] ; ++$j ) {
				$sumr=0;
				$sumg=0;
				$sumb=0;
				for ( $k=0 ; $k <= $src['index'] ; ++$k ) {
					$color = imagecolorat($src['img'], $i-(($src['index'])/2)+$k, $j);
					$r = ($color >> 16) & 0xFF;
					$g = ($color >> 8) & 0xFF;
					$b = ($color) & 0xFF;
					$sumr += $r*$coeffs[$src['index']][$k];
					$sumg += $g*$coeffs[$src['index']][$k];
					$sumb += $b*$coeffs[$src['index']][$k];
					
				}
				$color = imagecolorallocate ($src['temp1'], $sumr/$src['sum'], $sumg/$src['sum'], $sumb/$src['sum'] );
				imagesetpixel($src['temp1'],$i,$j,$color);
			} 
		}
		imagedestroy($src['img']);
		
		for ( $i=0 ; $i < $src['infos'][0] ; ++$i ) {
			for ( $j=0 ; $j < $src['infos'][1] ; ++$j ) {
				$sumr=0;
				$sumg=0;
				$sumb=0;
				for ( $k=0 ; $k <= $src['index'] ; ++$k ) {
					$color=imagecolorat($src['temp1'], $i, $j-(($src['index'])/2)+$k);
					$r=($color >> 16) & 0xFF;
					$g=($color >> 8) & 0xFF;
					$b=($color) & 0xFF;
					$sumr += $r*$coeffs[$src['index']][$k];
					$sumg += $g*$coeffs[$src['index']][$k];
					$sumb += $b*$coeffs[$src['index']][$k];
					
				}
				$color = imagecolorallocate ($src['temp2'], $sumr/$src['sum'], $sumg/$src['sum'], $sumb/$src['sum'] );
				imagesetpixel($src['temp2'],$i,$j,$color);
			} 
		}
		imagedestroy($src['temp1']);
		
		//header("Content-Type: image/png");
		imagepng($src['temp2']);
J'obtiens un message d'erreur de ce type sous IE :
<b>Notice</b>: imagecolorat(): -4,0 is out of bounds in <b>c:\program files\easyphp1-8\www\...\...\gallery_create.php</b> on line <b>120</b><br />
Il s'agit du colorallocat() de la premiere boucle. (et j'imagine bien que le problème existe aussi sur la deuxième boucle)/
Pour info, le $image de $src['img'] = $image; est la résultante d'un autre script, dans lequel je crée l'ombre elle-meme.

D'ou provient cette erreur avec le imagecolorat() ?[/code]