
Auteur : siddh
| Version : 17/01/2006 | | |
| |
C'est une classe php5 donc pour ceux qui voudraient l'utiliser en php4,
il faut juste changer les "private" des attributs en "var", et mettre un constructeur php4.
<?
class ImageTools
{
const X = 0;
const Y = 1;
const TOP = 2;
const BOTTOM = 3;
const LEFT = 4;
const RIGHT = 5;
const CENTER = 6;
const MIDDLE = 7;
private $img;
private $dstImg;
private $srcWidth;
private $srcHeight;
private $file;
private $ext;
private $srcPath;
private $dstPath;
private $font;
private $textColor;
private $bgColor;
private $old;
public function __construct()
{
$this->textColor = array("r" => 0, "g" => 0, "b" => 0);
$this->bgColor = array("r" => 255, "g" => 255, "b" => 255);
$this->font = $GLOBALS["SystemRoot"]."/include/font/VERDANA.TTF";
}
public function loadImage($img)
{
if (file_exists($img))
{
$tab = pathinfo($img);
$this->srcPath = $tab["dirname"];
$this->file = $tab["basename"];
$this->ext = $tab["extension"];
if ($this->ext == "jpg")
$this->img = imagecreatefromjpeg($img);
if ($this->ext == "png")
$this->img = imagecreatefrompng($img);
if ($this->ext == "gif")
$this->img = imagecreatefromgif($img);
$this->setBgColorWithFirstPixel();
$srcSize = getImageSize($img);
$this->srcWidth = $srcSize[0];
$this->srcHeight = $srcSize[1];
}
}
private function setBgColorWithFirstPixel()
{
$rgb = ImageColorAt($this->img, 0, 0);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$this->setBgColor($r,$g,$b);
}
public function resizeTo()
{
$ratio = -1;
$dest_width = 0;
$dest_height = 0;
$numArgs = func_num_args();
if($numArgs == 1)
{
$ratio = func_get_arg(0);
$perc = strrpos($ratio,"%");
if($perc)
$ratio = substr($ratio,0,$perc)/100;
$dest_width = $this->srcWidth*$ratio;
$dest_height = $this->srcHeight*$ratio;
}
elseif ($numArgs == 2)
{
$dest_width = func_get_arg(0);
$dest_height = func_get_arg(1);
}
else
return;
$this->resize($dest_width,$dest_height);
}
public function resizeByWidth($width)
{
$this->resizeTo($width/$this->srcWidth);
}
public function resizeByHeight($height)
{
$this->resizeTo($height/$this->srcHeight);
}
private function resize($maxWidth, $maxHeight)
{
$srcRatio = $this->srcWidth/$this->srcHeight;
$destRatio = $maxWidth/$maxHeight;
if ($destRatio > $srcRatio)
{
$destSize[1] = $maxHeight;
$destSize[0] = $maxHeight*$srcRatio;
}
else
{
$destSize[0] = $maxWidth;
$destSize[1] = $maxWidth/$srcRatio;
}
$this->dstImg = imagecreatetruecolor($destSize[0],$destSize[1]);
imageAntiAlias($this->dstImg,true);
$color = imagecolorallocate($this->dstImg, $this->bgColor["r"],
$this->bgColor["g"], $this->bgColor["b"]);
imagefill($this->dstImg,0,0,$color);
imageCopy($this->dstImg, $this->img, 0, 0, 0, 0,$destSize[0],
$destSize[1],$this->srcWidth,$this->srcHeight);
$this->img = $this->dstImg;
$this->srcWidth = $destSize[0];
$this->srcHeight = $destSize[1];
}
public function addTexte($texte, $fontSize=8, $vPos=BOTTOM, $hPos=CENTER)
{
$basefont = $fontSize;
$margin = 5;
$bottomMargin = round($fontSize/6) -1;
$description = $texte;
$box = imageTTFBBox ( $basefont, 0, $this->font, $description);
$boxWidth = $box[2] - $box[0] + $margin*2;
$boxHeight = $box[1] - $box[7] + $margin*2;
$width = 0;
$height = 0;
if($boxWidth <= $this->srcWidth)
$width = $this->srcWidth;
else
$width = $boxWidth;
if($boxHeight <= $this->srcHeight)
$height = $this->srcHeight;
else
$height = $boxHeight + $bottomMargin;
$tabX = array();
$tabY = array();
$tabX[LEFT] = $margin;
$tabX[CENTER] = ($width - $boxWidth) / 2 + $margin;
$tabX[RIGHT] = $width - $boxWidth + $margin;
$tabY[TOP] = $boxHeight - $margin;
$tabY[CENTER] = ($height + $boxHeight) / 2 +$margin;
$tabY[BOTTOM] = $height - $margin - $bottomMargin;
$this->dstImg = imagecreatetruecolor($width,$height);
imageAntiAlias($this->dstImg,true);
$color = imagecolorallocate($this->dstImg, $this->bgColor["r"],
$this->bgColor["g"], $this->bgColor["b"]);
imagefill($this->dstImg,0,0,$color);
$acolor = imagecolorallocate($this->img, $this->bgColor["r"],
$this->bgColor["g"], $this->bgColor["b"]);
imagefill($this->img,0,0,$acolor);
$imgX = ($width - $this->srcWidth) / 2;
$imgY = ($height - $this->srcHeight) / 2;
imagecopy($this->dstImg, $this->img, $imgX,$imgY, 0,0, $this->srcWidth, $this->srcHeight);
$textColor = imagecolorallocate($this->dstImg, $this->textColor["r"],
$this->textColor["g"], $this->textColor["b"]);
imagettftext($this->dstImg, $fontSize, 0, $tabX[$hPos], $tabY[$vPos],
$textColor, $this->font, $description);
$this->img = $this->dstImg;
$this->srcWidth = $boxWidth;
$this->srcHeight = $boxHeight;
}
public function saveAs($name)
{
imagepng($this->img,$name.".png");
}
public function getTextColor()
{
return $this->textColor;
}
public function setTextColor($r,$g,$b)
{
$this->textColor["r"] = $r;
$this->textColor["g"] = $g;
$this->textColor["b"] = $b;
}
public function setBgColor($r,$g,$b)
{
$this->old["r"] = $this->bgColor["r"];
$this->old["g"] = $this->bgColor["g"];
$this->old["b"] = $this->bgColor["b"];
$this->bgColor["r"] = $r;
$this->bgColor["g"] = $g;
$this->bgColor["b"] = $b;
}
}
?> |
|
| |
Voila donc si vous avez une liste d'images (seulement les extensions .jpg, .bmp, .gif, .png),
vous pouvez écrire un copyright en bas à gauche en rouge, noir ou blanc.
- Créez un dossier images dans le dossier ou se trouve votre fichier .php.
- Copier la police arial.tff dans le dossier ou se trouve votre fichier .php
- Placez les images que vous souhaitez dans le dossier images.
- Lancez votre page .php
<?
function head() {
echo '
<html>
<head>
<style type="text/css">
.folder, .file {
font-family:verdana;
font-size: 11px;
}
.folder {
color:orange;
}
.elements {
padding-left:20px;
}
.file, a {
color:black;
}
</style>
<script language="JavaScript">
function open_img(url, x, y) {
x = parseInt(x)+parseInt(20);
y = parseInt(y)+parseInt(20);
window.open(url, "aproposde", "toolbar=no, location=no, directories=no,
status=no, scrollbars=no, resizable=no, copyhistory=no, width="+x+", height="+y+", left=300, top=50");
}
</script>
</head>
<body><table width="100%"><tr><td width="30%"><form name="apply"
method="post" enctype="multipart/form-data">';
}
function foot() {
echo '</td><td width="70%"><span class="file">
Sélectionnez les images sur lesquelles écrire<br>puis écrivez le copyright à insérer: </span><br>'
.'<input type="text" name="copyright" maxlength="30" style="height:20px;width:200px;
vertical-align:middle"> '
.'<input type="submit" value="Ecrire" name="Ecrire" style="height:22px;
font-size: 11px;vertical-align:middle;"><br><br>'
.'<select name="couleur" style="height:18px;vertical-align:middle;font-size:11px">'
.'<option value="red">Rouge</option><option value="black">Noir</option>
<option value="white">Blanc</option></select> <span class="file">couleur du copyright</span><br>'
.'<input type="checkbox" name="suppr" style="vertical-align:middle">
<span class="file">Supprimer les fichiers sources</span>'
.'<br><br><br><span class="file">Pour supprimer les fichiers sélectionnés, cliquez ici:</span>
<input type="submit" value="Supprimer" name="Ecrire" style="height:22px;font-size: 11px;vertical-align:middle;">'
.'</form></td></tr></table>'
.'</body></html>';
}
function explore($homedir, $start='1') {
$extension = array('gif', 'jpeg', 'bmp', 'png');
$output = '';
if ($start == 1)
$output .= '<span class="file">Contenu du dossier</span> <span class="folder">'.$homedir.'/</span><br>';
$dir = openDir($homedir);
while ($file = readDir($dir)) {
if (($file!=".")&&($file!="..")) {
if (is_dir("$homedir/$file")) {
$output .= '<div class="folder">+ '.$file.'/</div>';
$subelements = explore("$homedir/$file", 0);
if ($subelements !== '') {
$output .= '<div class="elements">'.$subelements.'</div>';
}
} else {
list($width, $height, $type) = @getimagesize("images/".$file);
if ($type == 1) $type = 'gif';
elseif ($type == 2) $type = 'jpeg';
elseif ($type == 3) $type = 'png';
elseif ($type == 6) $type = 'bmp';
if (in_array($type, $extension)) {
$output .= "<div class=\"file\"><input type='checkbox' value='".$type."' name='delete[".$file."]'
style='vertical-align:middle'> "
."<a href=\"#\" title=\"Voir l'image\" OnClick=\"open_img('images/".$file."', '".$width."', '".$height."')\">".$file."</a></div>";
}
}
}
}
closeDir($dir);
return $output;
}
function submit() {
if ($_POST['Ecrire'] == 'Supprimer') {
if (isset($_POST['delete'])) {
foreach($_POST['delete'] As $image => $type) {
unlink('images/'.$image);
}
}
}
else {
if (isset($_POST['copyright']) && !empty($_POST['copyright'])) $copyright = trim($_POST['copyright']); else $copyright = '';
if (!empty($copyright)) {
$delete = (isset($_POST['suppr']) && $_POST['suppr'] == 'on') ? 'ok' : 'no';
if (isset($_POST['delete'])) {
foreach($_POST['delete'] As $image => $type) {
$fonction = 'imagecreatefrom'.$type;
$im = $fonction('images/'.$image);
switch($_POST['couleur']) {
case 'red':
$textcolor = imagecolorallocate($im, 255, 0, 0);
break;
case 'black':
$textcolor = imagecolorallocate($im, 0, 0, 0);
break;
case 'white':
$textcolor = imagecolorallocate($im, 255, 255, 255);
break;
default: $textcolor = imagecolorallocate($im, 0, 0, 0);
}
list($width_p, $height_p, $type_p) = getimagesize("images/".$image);
$pos_y = $height_p -2;
$font = 'arial.ttf';
$taille = 13;
while (($taille-2)*(strlen(stripslashes($copyright))) > $width_p) {
$taille--;
if ($taille == 5) break;
}
imagettftext($im, $taille, 0, 5, $pos_y, $textcolor, $font, stripslashes($copyright));
$renvoi = 'image'.$type;
if ($type == 'jpeg') $type_file = 'jpg'; else $type_file = $type;
if (isset($_POST['suppr']) && $_POST['suppr'] == 'on') unlink('images/'.$image);
$renvoi($im, 'images/'.substr($image, 0, -4).'-done.'.$type_file);
imagedestroy($im);
}
}
}
}
}
if (isset($_POST['Ecrire'])) submit();
head();
echo explore("images");
foot();
?> |
|
Consultez les autres pages sources
Les sources présentés sur cette pages sont libre de droits,
et vous pouvez les utiliser à votre convenance. Par contre cette page de présentation de ces sources constitue une oeuvre intellectuelle protégée par les droits d'auteurs.
Copyright ©2006
Developpez LLC. Tout droits réservés Developpez LLC.
Aucune reproduction, même partielle, ne peut être faite de ce site et de
l'ensemble de son contenu : textes, documents et images sans l'autorisation
expresse de Developpez LLC. Sinon vous encourez selon la loi jusqu'à 3 ans
de prison et jusqu'à 300 000 E de dommages et intérets.
Cette page est déposée à la SACD.
|