[PHP]Création image à partir d'un JPG

Statut
N'est pas ouverte pour d'autres réponses.

Gh0$T

Elite
Bonjour,

Je suis en train de créer une image avec la librairie GD.

Je compte faire une image de 200*400 mais inserer un logo dedans d'un JPG déjà existant (80*70)

Mais je trouve pas la fonctione adéquate pour importer l'image dans le code.

J'ai pensé à imagecopy mais je comprend pas la syntaxe à utiliser.

Le fichier .JPG que je veux injecter se trouve dans le même repertoire que la page PHP qui va générer le PNG.

Merci.
 
1er
OP
Gh0$T

Gh0$T

Elite
Bon, mon logo fait 100*58

Mon image de base fait 400*200

La syntaxe :

Code:
ImageCopy ($im, $logo_klb, 0, 0,0,0,100,58);
Je veux donc que mon logo commence en haut a gauche de mon image et qu'il prenne l'entierté de mon logo.

Mais en retour j'ai :

Code:
L'image “http://www.***/~yves/klb/php_logo_klb.php” ne peut être affichée car elle contient des erreurs.
 

Jereck

Α & Ω
Staff
Code:
<?php
// Globals
define("pathim", '../images/');
 
define("w_wat", 42); // Largeur watermark
define("h_wat", 28); // Hauteur watermark
 
// ----------------------------------------------------- Fonctions
// ----------------------------------------------------- ---------
 
function im_croix(){
// Création de l'image
	 $n_im = @imagecreate (189, 39);
 
	 // Fond + Contour
	 $background_color = imagecolorallocate ($n_im, 51, 51, 51);
	 imagefilledrectangle ($n_im, 2, 2, 186, 36, imagecolorallocate ($n_im, 0, 0, 0));
 
	 // Image croix
	 imagefilledrectangle ($n_im, 6, 6, 19, 20, imagecolorallocate ($n_im, 128, 128, 128));
	 imagefilledrectangle ($n_im, 7, 7, 19, 21, imagecolorallocate ($n_im, 51, 51, 51));
	 imagefilledrectangle ($n_im, 7, 7, 18, 20, imagecolorallocate ($n_im, 192, 192, 192));
	 imagefilledrectangle ($n_im, 7, 7, 17, 19, imagecolorallocate ($n_im, 255, 255, 255));
	 imagefilledrectangle ($n_im, 10, 11, 15, 15, imagecolorallocate ($n_im, 255, 0, 0));
	 imageline ($n_im, 12, 11, 13, 11, imagecolorallocate ($n_im, 255, 255, 255)); 
	 imageline ($n_im, 12, 15, 13, 15, imagecolorallocate ($n_im, 255, 255, 255)); 
	 imageline ($n_im, 10, 13, 11, 13, imagecolorallocate ($n_im, 255, 255, 255)); 
	 imageline  ($n_im, 14, 13, 15, 13, imagecolorallocate ($n_im, 255, 255, 255)); 
	 imagesetpixel ($n_im, 10, 12, imagecolorallocate ($n_im, 255, 255, 255));
	 imagesetpixel ($n_im, 15, 12, imagecolorallocate ($n_im, 255, 255, 255));
	 imagesetpixel ($n_im, 10, 14, imagecolorallocate ($n_im, 255, 255, 255));
	 imagesetpixel ($n_im, 15, 14, imagecolorallocate ($n_im, 255, 255, 255));
	 imagesetpixel ($n_im, 6, 21, imagecolorallocate ($n_im, 51, 51, 51));
 
	 // Texte
	 $text_color = imagecolorallocate ($n_im, 46, 186, 141); 
	 $font = imageloadfont(pathim . 'hootie.gdf');
	 imagestring ($n_im, $font, 26, 10, "Pas d'image", $text_color); 
 
	 // Création de l'image
	 header ("Content-type: image/gif");
	 imagegif ($n_im);
	 // Destruction des ressources images
	 imagedestroy($n_im);
 
	 return True;
}
 
function GetSource() {
// Récupère le fichier source
	 $src = $_GET['src'];
	 if (strtolower((substr($src, 0, 4))) != "http") { $src = '../' . $src; }
	 $src = str_replace(' ','%20',$src); //REPLACE THE SPACES
 
	 return $src;
}
 
function ReSizes ($s_width, $s_height, $s_src){
	 if ($s_width > 220){
		  $mul = 220 / $s_width;
		  $n_width = 220;
		  $n_height = $s_height * $mul;
 
		  if ($n_height > 220){
			   $mul = 220 / $n_height;
			   $n_height = 220;
			   $n_width = $n_width * $mul;
		  }
 
		  $n_im = @imagecreatetruecolor ($n_width, $n_height);
		  imagecopyresized ($n_im, $s_src, 0, 0, 0, 0, $n_width, $n_height, $s_width, $s_height);
	 }else{
		  $n_width = $s_width;
		  $n_height = $s_height;
 
		  $n_im = @imagecreatetruecolor ($n_width, $n_height);
		  imagecopy ($n_im, $s_src, 0, 0, 0, 0, $n_width, $n_height);
	 }
 
	 $RetVal = array (
		  'n_width' => $n_width,
		  'n_height' => $n_height,
		  'n_im' => $n_im);
	  
	 imagedestroy($s_src);
 
	 return $RetVal; 
}
 
function doWaterM ($width, $height, $n_im){
	 $l_logo = imagecreatefrompng (pathim . "logo.png");
 
	 $l_x = $width - w_wat - 5;
	 $l_y = $height - h_wat - 5;
	 imagecopy ($n_im, $l_logo, $l_x, $l_y, 0, 0, w_wat, h_wat);
 
	 imagedestroy($l_logo);
}
 
// ----------------------------------------------------- Page
// ----------------------------------------------------- ----
 
if (!isset ($_GET['src'])){
	 im_croix();
	 exit;
}
 
$src = GetSource();
 
list($s_width, $s_height, $s_type, $s_attr) = getimagesize($src);
 
switch ($s_type){
case 1 : // GIF
	 // Création des ressrouces
	 $s_src = imagecreatefromgif ($src);
	 imagealphablending($s_src,true);
	 // Changements de tailles
	 $ret = ReSizes ($s_width, $s_height, $s_src);
	 // Water mark
	 doWaterM ($ret['n_width'], $ret['n_height'], $ret['n_im']);
	 // Création de l'image
	 header ("Content-type: image/gif");
	 imagegif ($ret['n_im']);
	 // Destruction des images
	 imagedestroy($ret['n_im']);
	 break;
 
case 2 : // JPEG
	 // Création des ressrouces
	 $s_src = imagecreatefromjpeg ($src);
	 imagealphablending($s_src,true);
	 // Changements de tailles
	 $ret = ReSizes ($s_width, $s_height, $s_src);
	 // Water mark
	 doWaterM ($ret['n_width'], $ret['n_height'], $ret['n_im']);
	 // Création de l'image
	 header ("Content-type: image/jpeg");
	 imagejpeg ($ret['n_im']);
	 // Destruction des images
	 imagedestroy($ret['n_im']);
	 break;
 
case 3: // PNG
	 // Création des ressrouces
	 $s_src = imagecreatefrompng ($src);
	 imagealphablending($s_src,true);
	 // Changements de tailles
	 $ret = ReSizes ($s_width, $s_height, $s_src);
	 // Water mark
	 doWaterM ($ret['n_width'], $ret['n_height'], $ret['n_im']);
	 // Création de l'image
	 header ("Content-type: image/png");
	 imagepng ($ret['n_im']);
	 // Destruction des images
	 imagedestroy($ret['n_im']);
	 break;
 
case 6 : // BMP
	 $n_im = @imagecreate (189, 39);
	 // Fond + Contour
	 $background_color = imagecolorallocate ($n_im, 51, 51, 51);
	 imagefilledrectangle ($n_im, 2, 2, 186, 36, imagecolorallocate ($n_im, 0, 0, 0));
 
	 $text_color = imagecolorallocate ($n_im, 46, 186, 141);
 
	 imagestring ($n_im, 1, 10, 10, "Pas de .BMP svp", $text_color); 
	 imagestring ($n_im, 1, 10, 25, "(Cliquez ici)", $text_color); 
	 // Création de l'image
	 header ("Content-type: image/gif");
	 imagegif ($n_im);
	 // Destruction des images
	 imagedestroy($n_im);
	 break;
 
default :
	 $n_im = @imagecreate (189, 39);
	 // Fond + Contour
	 $background_color = imagecolorallocate ($n_im, 51, 51, 51);
	 imagefilledrectangle ($n_im, 2, 2, 186, 36, imagecolorallocate ($n_im, 0, 0, 0));
 
	 $text_color = imagecolorallocate ($n_im, 46, 186, 141);
 
	 imagestring ($n_im, 1, 10, 10, "Format d'image incorrect", $text_color); 
	 imagestring ($n_im, 1, 10, 25, "(Cliquez ici)", $text_color); 
	 // Création de l'image
	 header ("Content-type: image/gif");
	 imagegif ($n_im);
	 // Destruction des images
	 imagedestroy($n_im);
	 break;
}
?>
 

SkYlEsS

Elite
Pour faire plus simple et cibler ton énigme :

Code:
<?php
header ("Content-type: image/jpeg"); // L'image que l'on va créer est un jpeg

// On charge d'abord les images
$source = imagecreatefrompng("logo.png"); // Le logo est la source
$destination = imagecreatefromjpeg("photo.jpg"); // La photo est la destination

// Les fonctions imagesx et imagesy renvoient la largeur et la hauteur d'une image
$largeur_source = imagesx($source);
$hauteur_source = imagesy($source);
$largeur_destination = imagesx($destination);
$hauteur_destination = imagesy($destination);

// On veut placer le logo en haut à gauche, on calcule les coordonnées où on doit placer le logo sur la photo
$destination_x = 0; //A gauche
$destination_y =  0; //En haut

// On met le logo (source) dans l'image de destination (la photo)
imagecopymerge($destination, $source, $destination_x, $destination_y, 0, 0, $largeur_source, $hauteur_source, 60);

// On affiche l'image de destination qui a été fusionnée avec le logo
imagejpeg($destination);
?>
 
Statut
N'est pas ouverte pour d'autres réponses.
Haut