Création et manipulation d`images

Download Report

Transcript Création et manipulation d`images

PHP
Création et manipulation
d'images
Jérôme CUTRONA
[email protected]
12:35:47
Programmation Web 2011-2012
1
Préambule
 L'utilisation habituelle de PHP consiste à produire des
pages HTML.
 Grâce à la bibliothèque GD, PHP peut produire des
images enregistrées sur disque ou directement
transmises au navigateur du client.
 Formats d'images accessibles sont GIF, JPEG et PNG
 Possibilités offertes par la création/manipulation
d'images en PHP :



12:35:47
redimensionnement à la volée
CAPTCHA (Completely Automated Public Turing test to tell
Computers and Humans Apart)
insertion de texte dynamique
Programmation Web 2011-2012
2
Algorithme général de création
 Création d'une image :


nouvelle
à partir d'un fichier
 Manipulations de l'image



dessin
texte
…
 Production de l'image finale :


dans un fichier
envoi vers le navigateur
 Libération mémoire
12:35:47
Programmation Web 2011-2012
3
Algorithme général de création
<?php
// Création
$im = imageCreateTrueColor(100, 100) ;
// Manipulations
$red = imageColorAllocate($im, 255, 0, 0) ;
imageFilledRectangle($im, 0, 0,
99, 99, $red) ;
// Envoi vers le navigateur
header('Content-Type: image/png') ;
imagePNG($im) ;
// Libération mémoire
imageDestroy($im) ;
12:35:47
Programmation Web 2011-2012
4
Création d'une image (vide)
resource imageCreate ( int width, int height )
construit une image vide de largeur width et de hauteur
height et retourne son identifiant.
L'image est à couleurs indexées.
Retourne false en cas d'échec.
resource imageCreateTrueColor ( int width, int height )
construit une image vide de largeur width et de hauteur
height et retourne son identifiant.
L'image est à couleurs "réelles" (24bits).
Retourne false en cas d'échec.
bool imageDestroy ( resource image )
libère la mémoire associée à image
12:35:47
Programmation Web 2011-2012
5
Création d'une image (fichier)
resource imageCreateFromGIF ( string nom )
construit une image à partir du fichier GIF nom
resource imageCreateFromJPEG ( string nom )
construit une image à partir du fichier JPEG nom
resource imageCreateFromPNG ( string nom )
construit une image à partir du fichier PNG nom
resource imageCreateFromGD[2] ( string nom )
construit une image à partir du fichier GD[2] nom
12:35:47
Programmation Web 2011-2012
6
Production d'une image
bool imageGIF (resource image [, string fname])
construit un fichier GIF fname à partir de image
bool imageJPEG (resource image [, string fname])
construit un fichier JPEG fname à partir de image
bool imagePNG (resource image [, string fname])
construit un fichier PNG fname à partir de image
bool imageGD[2] (resource image [, string fname])
construit un fichier GD[2] fname à partir de image
 Si fname est absent, le contenu est transmis
12:35:47
Programmation Web 2011-2012
7
Allocation de couleurs
int imageColorAllocate ( resource im,
int R, int G, int B )
construit une couleur (R, G, B) pour l'image im et
retourne son identifiant.
0 ≤ R ≤ 255
0 ≤ G ≤ 255
0 ≤ B ≤ 255
12:35:47
Programmation Web 2011-2012
8
Allocation de couleurs
int imageColorAllocateAlpha ( resource im,
int R, int G, int B,
int A )
construit une couleur (R, G, B, A) pour l'image im
et retourne son identifiant.
0 ≤ R ≤ 255
0 ≤ G ≤ 255
0 ≤ B ≤ 255
0 ≤ A ≤ 127 (transparence de la couleur)
12:35:47
Programmation Web 2011-2012
9
Copie redimensionnée d'images
bool imageCopyResized (
resource dst_im, resource src_im,
int dst_x, int dst_y, int src_x, int src_y,
int dst_w, int dst_h, int src_w, int src_h )
copie une portion rectangulaire de src_im dans
une portion rectangulaire dst_im. Si les portions
sont de taille différente, l'approximation se fait à
l'échantillon le plus proche.
bool imageCopyResampled ( … ) remplace
l'approximation par un ré-échantillonnage
12:35:47
Programmation Web 2011-2012
10
Dessiner dans une image
 int imageColorAllocate ( resource image,
int red, int green, int blue )
 bool imageArc ( resource image,
int cx, int cy,
int width, int height,
int start, int end, int color )
 bool imageLine ( resource image,
int x1, int y1,
int x2, int y2, int color )
 bool imageString ( resource image, int font,
int x, int y, string s, int color )
12:35:47
Programmation Web 2011-2012
11
Création d'images à la volée
<img src="img.php"
alt="Mon image"
height="152" width="210">
Peut dépendre de paramètres
GET :
<?php
// Création
$im = imageCreate(100, 100) ;
•img.php?t=Coucou
•img.php?r=10&v=120&b=255
// Manipulations
$red = imageColorAllocate($im,
255, 0, 0) ;
imageFilledRectangle($im, 0, 0,
99, 99, $red) ;
•img.php?i=im.jpg
•…
// Envoi vers le navigateur
header('Content-Type: image/png') ;
imagePNG($im) ;
// Libération mémoire
imageDestroy($im) ;
12:35:47
Programmation Web 2011-2012
12
Introduction d’une vision objet ?
 resource imageCreateTrueColor ( int width, int
height )




int imageColorAllocate ( resource image, …
bool imageArc ( resource image, …
bool imageLine ( resource image, …
bool imageString ( resource image, …
 bool imageDestroy ( resource image )
12:35:47
Programmation Web 2011-2012
13
Encapsulation objet
class GDImage {
// @var resource $_resource image identifier
private $_resource = null ;
private function __construct() {
}
public function __destruct() {
if (!is_null($this->_resource))
imageDestroy($this->_resource) ;
}
12:35:47
Programmation Web 2011-2012
14
Encapsulation objet : usine
public static function
createFromSize($x, $y, $truecolor=true) {
$x = (int) $x ; $y = (int) $y ;
$resource = false ;
if ($truecolor)
$resource = @imageCreateTrueColor($x, $y) ;
else $resource = @imageCreate($x, $y) ;
if ($resource !== false) {
$image = new self() ;
$image->_resource = $resource ;
return $image ; }
else
throw new LogicException("Failed to create
GD resource") ; }
12:35:47
Programmation Web 2011-2012
15
Encapsulation objet : usine
public static function
createFromFile($filename, $filetype) {
if (is_file($filename)) {
if (in_array($filetype, self::$_factory_types))
{
$functionName = 'imageCreateFrom' . $filetype ;
$image = new self() ;
if (($tmp = @$functionName($filename))
=== false) {
throw new Exception("unable to load file
'{$filename}'") ;
}
$image->_resource = $tmp ; return $image ;
} else throw new Exception("unknown filetype") ;
} else throw new Exception("{$filename} : no such file") ;
}
12:35:47
Programmation Web 2011-2012
16
Encapsulation objet : constantes
/**
* @var array
*/
private static $_factory_types = array(
self::GD,
self::GD2PART,
self::GD2,
self::GIF,
self::JPEG,
self::PNG,
self::WBMP,
self::XBM,
self::XPM,
) ;
12:35:47
Programmation Web 2011-2012
17
Encapsulation objet : usine
class GDImage {
const GD
= 'gd' ;
const GD2PART = 'gd2part' ;
const GD2
= 'gd2' ;
const GIF
= 'gif' ;
const JPEG
= 'jpeg' ;
const PNG
= 'png' ;
const WBMP
= 'wbmp' ;
const XBM
= 'xbm' ;
const XPM
= 'xpm' ;
12:35:47
Programmation Web 2011-2012
18
Création d'images : vision objet
<img src="img.php"
alt="Mon image"
height="152" width="210">
Peut dépendre de paramètres
GET :
•img.php?t=Coucou
•img.php?r=10&v=120&b=255
•img.php?i=im.jpg
•…
12:35:47
<?php
require_once('gdimage.class.php') ;
// Création
$im
= GDImage::createFromSize(100, 100) ;
// Manipulations
$red = $im->colorAllocate(255, 0, 0) ;
$im->filledRectangle(0, 0,
99, 99, $red) ;
// Envoi vers le navigateur
header('Content-type: image/png') ;
$im->PNG() ;
// Libération mémoire
// Automatique, à la destruction de $im
Programmation Web 2011-2012
19
Comparaison non objet / objet
<?php
// Création
$im = imageCreate(100, 100) ;
// Manipulations
$red = imageColorAllocate($im,
255, 0, 0) ;
imageFilledRectangle($im, 0, 0,
99, 99, $red) ;
// Envoi vers le navigateur
header('Content-Type: image/png') ;
imagePNG($im) ;
// Libération mémoire
imageDestroy($im) ;
12:35:47
<?php
require_once('gdimage.class.php') ;
// Création
$im
= GDImage::createFromSize(100, 100) ;
// Manipulations
$red = $im->colorAllocate(255, 0, 0) ;
$im->filledRectangle(0, 0,
99, 99, $red) ;
// Envoi vers le navigateur
header('Content-type: image/png') ;
$im->PNG() ;
// Libération mémoire
// Automatique, à la destruction de $im
Programmation Web 2011-2012
20