Logo classes.scriptsphp.org PHP

go to nav bar

image

Manipuler les images

La classe image fournit plusieurs méthodes simples permettant de redimensionner, compresser, convertir des images, de leur ajouter des textes et des logos.

Les Méthodes

Exemples

  1. Exemple Minimal

La Source

<?php

class image {

        var 
$bg;
        var 
$logo;
        var 
$logox;
        var 
$logoy;
        var 
$bgx;
        var 
$bgy;
        var 
$fontfile;
        var 
$color;

        function 
image($bgfile='') {
                
                
image::addbackground($bgfile);
                
        }

        function 
createimagefromtype($file,$handler) {
                
$size getimagesize($file);
                
$type=$size[2];
       
                switch (
$type) {
                        case 
$imhandler imagecreatefromjpeg($file);
                        break;
                        case 
$imhandler imagecreatefrompng($file);
                        break;
                        case 
$imhandler imagecreatefromgif($file);
                        break;
                }

                
$xtmpstr=$handler.'x';
                
$ytmpstr=$handler.'y';

 
                
$this->$xtmpstr=$size[0];
                
$this->$ytmpstr=$size[1];

                return 
$imhandler;
        }

        function 
resize $thumbw $thumbh $border) {

                
$size [0]=$this->bgx;
                
$size [1]=$this->bgy;

                if (
$border==1) {
                        
$scale min($thumbw/$size[0], $thumbh/$size[1]);
                        
$width = (int)($size[0]*$scale);
                        
$height = (int)($size[1]*$scale);
                        
$deltaw = (int)(($thumbw $width)/2);
                        
$deltah = (int)(($thumbh $height)/2);
                                
$dst_img ImageCreateTrueColor($thumbw$thumbh);
                                if ( !empty(
$this->color))
                                        
imagefill $dst_img00$this->color );
                                
$this->bgx=$thumbw;
                                
$this->bgy=$thumbh;
                }
                elseif (
$border==0) {
                        
$scale min($thumbw/$size[0], $thumbh/$size[1]);
                        
$width = (int)($size[0]*$scale);
                        
$height = (int)($size[1]*$scale);
                        
$deltaw 0;
                        
$deltah 0;
                                
$dst_img ImageCreateTrueColor($width$height);
                                
$this->bgx=$width;
                                
$this->bgy=$height;
                }

                
$src_img $this->bg;

                
ImageCopyResampled($dst_img$src_img$deltaw$deltah00$width$heightImageSX($src_img),ImageSY($src_img));


                
$this->bg=$dst_img;
                
imagedestroy($src_img);
        }

        function 
addbackground($bgfile) {
        
                if ( !empty(
$bgfile) && file_exists($bgfile) )  $this->bg image::createimagefromtype($bgfile,'bg');
                
imagealphablending$this->bg ,TRUE );
        
        }

        function 
addlogo($file) {
                
$this->logo image::createimagefromtype($file,'logo');
                
imagealphablending$this->logo ,TRUE );
                
$size getimagesize($file);
                
$this->logox=$size[0];
                
$this->logoy=$size[1];

        }

        function 
mergelogo($x,$y,$alpha=100) {
                 if (
$x<0$x=$this->bgx-$this->logox+$x;
                 if (
$y<0$y=$this->bgy-$this->logoy+$y;
                return 
imagecopymerge $this->bg$this->logo$x$y00$this->logox$this->logoy$alpha );
        }

        function 
makecolor($red$green$blue) {
                
$this->color imagecolorallocate$this->bg$red$green$blue );
        }

        function 
setfont($fontfile) {
                
$this->fontfile=$fontfile;
        }

        function 
addtext ($text,$x=0,$y=0,$size=12,$angle=0) {
                
$textpreg_replace('`(?<!\r)\n`',"\r\n",$text);
                
$box=imagettfbbox $size$angle$this->fontfile$text );
                if (
$x<0) {
                    
$x=$this->bgx max($box[2],$box[4]) + $x;
                } else {
                    
$x=max(-$box[0],-$box[6]) + $x;
                }
                if (
$y<0) {
                    
$y=$this->bgy max($box[1],$box[3]) + $y;
                } else {
                    
$y=max(-$box[7],-$box[5]) + $y;
                }
                
                
imagettftext($this->bg$size$angle$x$y$this->color$this->fontfile $text );
                
        }

        function 
send_image($type,$file='',$compress=-1) {
    
                switch (
$type) {
                        case 
'JPG' :
                                if (!
$fileheader("Content-type: image/jpeg");
                                if (
$compress==-1$compress=100;
                                return 
imagejpeg($this->bg,$file,$compress);
                        break;
                        case 
'PNG' :
                                if (!
$fileheader("Content-type: image/png");
                                if (
$compress!=-1)
                                        
imagetruecolortopalette $this->bgtrue$compress);
                                return 
imagepng($this->bg,$file,$compress);
                        break;
                        case 
'GIF' :
                                if (!
$fileheader("Content-type: image/gif");
                                if (
$compress!=-1)
                                        
imagetruecolortopalette $this->bgtrue$compress);
                                return 
imagegif($this->bg,$file,$compress);
                        break;
                        default: return 
0;
                }
                
                
imagedestroy($this->bg);
                
imagedestroy($this->logo);

        }

}

?>

Merci de ne pas suivre ce lien emails.

0.0810s | «»
PHP powered