5/5 (4 avis)
Snippet vu 6 938 fois - Téléchargée 37 fois
<?php /***** arguments pour charger la page: image.php?file=imagefileURL&type=format&w=largeur&h=hauteur ******/ if(file_exists($f = $_GET['file'])) { /** retourne les nouvelles dimensions de l'image ajustée aux dimensions originales (pas de distortions)*/ function getSize($im) { if(isset($_GET['w'])) $w = $_GET['w']; if(isset($_GET['h'])) $h = $_GET['h']; if(!isset($w) || $w == 0) $w = imagesx($im)/imagesy($im) * $h; if(!isset($h) || $h == 0) $h = imagesy($im)/imagesx($im) * $w; if($w == 0 && $h == 0) { $w = imagesx($im); $h = imagesy($im); } return array($w,$h); } // les formats supportés par php 4 et 5 if (function_exists("imagegif") && $_GET['type'] == 'gif') { $im = imagecreatefromgif($f); $s = getSize($im); $im_r = imagecreatetruecolor($s[0], $s[1]); // ici la couleur transparente sera le noir de l'image vide true colors $trans = imagecolorallocate($im_r,0,0,0); imagecolortransparent($im_r,$trans); // une fois resamplée l'image recompose avec un fond transparent pour garder l'image départ intacte! imagecopyresampled($im_r, $im, 0, 0, 0, 0, $s[0], $s[1], imagesx($im), imagesy($im)); header("Content-type: image/gif"); imagegif($im_r); } elseif (function_exists("imagejpeg") && $_GET['type'] == 'jpeg') { $im = imagecreatefromjpeg($f); $s = getSize($im); $im_r = imagecreatetruecolor($s[0], $s[1]); imagecopyresampled($im_r, $im, 0, 0, 0, 0, $s[0], $s[1], imagesx($im), imagesy($im)); header("Content-type: image/jpeg"); imagejpeg($im_r, "", 80); } elseif (function_exists("imagepng") && $_GET['type'] == 'png') { $im = imagecreatefrompng($f); $s = getSize($im); $im_r = imagecreatetruecolor($s[0], $s[1]); $trans = imagecolorallocate($im_r,0,0,0); imagecolortransparent($im_r,$trans); // comme le format gif transparence imagecopyresampled($im_r, $im, 0, 0, 0, 0, $s[0], $s[1], imagesx($im), imagesy($im)); header("Content-type: image/png"); imagepng($im_r); } elseif (function_exists("imagewbmp") && $_GET['type'] == 'wbmp') { $im = imagecreatefrowbmp($f); $s = getSize($im); $im_r = imagecreatetruecolor($s[0], $s[1]); imagecopyresampled($im_r, $im, 0, 0, 0, 0, $s[0], $s[1], imagesx($im), imagesy($im)); header("Content-type: image/vnd.wap.wbmp"); imagewbmp($im_r); } // endif _get(type) } else { die("Pas de fichier ".$_GET['file']." sur ce serveur"); }// endif file_exists() ?>
8 nov. 2006 à 22:09
8 nov. 2006 à 20:57
comme sa :
############################################################
<?php
/***** arguments pour charger la page: image.php?file=imagefileURL&type=format&w=largeur&h=hauteur ******/
if( !isset ( $_GET[ 'file' ] ) or empty( $_GET[ 'file' ] ) )
{
die( "TON MESSAGE D'ERREUR" );
}
$f = $_GET['file'];
/** retourne les nouvelles dimensions de l'image ajustée aux dimensions originales (pas de distortions)*/
function getSize( $im )
{
$h = imagesy( $im ); // Si ....
$w = imagesx( $im ); // Variable inéxistante
if( !isset( $_GET[ 'w' ] ) or !is_numeric( $_GET[ 'w' ] ) or empty( $_GET[ 'w' ] ) )
{
$w = imagesx( $im ) / imagesy( $im ) * $h;
}
else
{
$w = $_GET[ 'w' ];
}
if( !isset( $_GET[ 'h' ] ) or !is_numeric( $_GET[ 'h' ] ) or empty( $_GET[ 'h' ] ) )
{
$h = imagesy( $im ) / imagesx( $im ) * $w;
}
else
{
$h = $_GET[ 'h' ];
}
return array( $w , $h );
}
// les formats supportés par php 4 et 5
if ( function_exists( "imagegif" ) && $_GET[ 'type' ] == 'gif' ) {
$im = imagecreatefromgif( $f );
$s = getSize( $im );
$im_r = imagecreate( $s[ 0 ], $s[ 1 ] );
$trans = imagecolorallocate( $im_r , 255 , 99 , 140 );
imagefilledrectangle( $im_r , 0 , 0 , $s[ 0 ], $s[ 1 ], $trans );
imagecolortransparent( $im_r , $trans );
imagecopyresampled( $im_r, $im, 0, 0, 0, 0, $s[ 0 ], $s[ 1 ], imagesx( $im ), imagesy( $im ) );
header( "Content-type: image/gif" );
imagegif( $im_r );
}
elseif ( function_exists( "imagejpeg" ) && $_GET[ 'type' ] == 'jpeg' )
{
$im = imagecreatefromjpeg( $f );
$s = getSize( $im );
$im_r = imagecreatetruecolor( $s[ 0 ], $s[ 1 ] );
imagecopyresampled( $im_r, $im, 0, 0, 0, 0, $s[ 0 ], $s[ 1 ], imagesx( $im ), imagesy( $im ) );
header( "Content-type: image/jpeg" );
imagejpeg( $im_r, "", 80 );
}
elseif ( function_exists( "imagepng" ) && $_GET['type'] == 'png' )
{
$im = imagecreatefrompng( $f );
$s = getSize( $im );
$im_r = imagecreate( $s[ 0 ], $s[ 1 ] );
$trans = imagecolorallocate( $im_r , 255 , 99 , 140 );
imagefilledrectangle( $im_r , 0 , 0 , $s[ 0 ], $s[ 1 ], $trans );
imagecolortransparent( $im_r, $trans );
imagecopyresampled( $im_r, $im, 0, 0, 0, 0, $s[ 0 ], $s[ 1 ], imagesx( $im ), imagesy( $im ) );
header( "Content-type: image/png" );
imagepng( $im_r );
} elseif ( function_exists( "imagewbmp" ) && $_GET['type'] == 'wbmp' )
{
$im = imagecreatefrowbmp( $f );
$s = getSize( $im );
$im_r = imagecreatetruecolor( $s[ 0 ], $s[ 1 ] );
imagecopyresampled( $im_r, $im, 0, 0, 0, 0, $s[ 0 ], $s[ 1 ], imagesx( $im ), imagesy( $im ) );
header( "Content-type: image/vnd.wap.wbmp" );
imagewbmp( $im_r );
} else {
die( "Pas de support graphique avec PHP sur ce serveur" );
}
?>
##############################################################
Bon, ok j'avous ya pas tros de différence, mais bon, .... ^^'
~~ DotWizard512 ~~
8 nov. 2006 à 20:39
nn..?! ^^'
Parceque sinon sa risque d'affiché des erreurs
si tu tombe sur un p'tit fouineur....
~~ DotWizard512 ~~
31 oct. 2006 à 09:11
(bin oui, y'a bien écrit tous formats)
à part ca, c'est pas mal, et ca reste classique, ça peut servir pour des débutants dans GD ;-)
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.