par
costadelo » 18 oct. 2007, 00:53
Bonjour @tous,
Je suis actuellement confronté à un pb de génération de miniatures d'images.
J'ai une image en entrée ($img = "mondossier/monimage.jpeg"), que je souhaite générer ensuite en différents formats selon certaines conditions dans un dossier particuliers.
private function open_image ($file) {
# JPEG:
$im = @imagecreatefromjpeg($file);
if ($im !== false) { return $im; }
# GIF:
re $im = @imagecreatefromgif($file);
if ($im !== false) { return $im; }
# PNG:
$im = @imagecreatefrompng($file);
if ($im !== false) { return $im; }
# GD File:
$im = @imagecreatefromgd($file);
if ($im !== false) { return $im; }
# GD2 File:
$im = @imagecreatefromgd2($file);
if ($im !== false) { return $im; }
# WBMP:
$im = @imagecreatefromwbmp($file);
if ($im !== false) { return $im; }
# XBM:
$im = @imagecreatefromxbm($file);
if ($im !== false) { return $im; }
# XPM:
$im = @imagecreatefromxpm($file);
if ($im !== false) { return $im; }
# Try and load from string:
$im = @imagecreatefromstring(file_get_contents($file));
if ($im !== false) { return $im; }
return false;
}
public function _resize_img( $img, $new_width, $new_height, $height_post, $width_post, $type_file, $dossier, $pseudo ){
// Load image
$image = $this->open_image($img);
if ($image == false) {
die ('<strong>You uploaded an invalid image. Please go back and try again.</strong>');
}
// Get original width and height
$width = imagesx($image);
$height = imagesy($image);
// New width? Calculate new height
if ( $new_width != -1 ) {
$new_width = floatval($new_width);
$new_height = $height * ($new_width/$width);
// New height? Calculate new width
} elseif ( $new_height != -1) {
$new_height = floatval($new_height);
$new_width = $width * ($new_height/$height);
// New height and new width
} elseif ( $height_post != -1 && $width_post != -1 ) {
$new_height = floatval($height);
$new_width = floatval($width);
} else {
die ('<strong>You didn\'t specify any resizing options.</strong>');
}
// Resample
$image_resized = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
}
Bref, un grand classique...Cependant, je souhaite donc donner un nom et déplacer cette image générée au final ($image_resized) dans un dossier particulier...Je me suis donc tourné vers la fonction rename()...mais que dois-je lui passer en paramètres, sachant que j'ai déjà essayer ce genre de chose :
rename ( imagejpeg($image_resized,"mondossier/monimage.jpeg") , "monnouveaudossier/monimageminiature.jpeg");
Ce qui me parait somme toute logique...mais bon...ca ne fonctionne pas, donc ma logique n'est pas la bonne....Help me pliz !
Merci d'avance
Bonjour @tous,
Je suis actuellement confronté à un pb de génération de miniatures d'images.
J'ai une image en entrée ($img = "mondossier/monimage.jpeg"), que je souhaite générer ensuite en différents formats selon certaines conditions dans un dossier particuliers.
[php]
private function open_image ($file) {
# JPEG:
$im = @imagecreatefromjpeg($file);
if ($im !== false) { return $im; }
# GIF:
re $im = @imagecreatefromgif($file);
if ($im !== false) { return $im; }
# PNG:
$im = @imagecreatefrompng($file);
if ($im !== false) { return $im; }
# GD File:
$im = @imagecreatefromgd($file);
if ($im !== false) { return $im; }
# GD2 File:
$im = @imagecreatefromgd2($file);
if ($im !== false) { return $im; }
# WBMP:
$im = @imagecreatefromwbmp($file);
if ($im !== false) { return $im; }
# XBM:
$im = @imagecreatefromxbm($file);
if ($im !== false) { return $im; }
# XPM:
$im = @imagecreatefromxpm($file);
if ($im !== false) { return $im; }
# Try and load from string:
$im = @imagecreatefromstring(file_get_contents($file));
if ($im !== false) { return $im; }
return false;
}
public function _resize_img( $img, $new_width, $new_height, $height_post, $width_post, $type_file, $dossier, $pseudo ){
// Load image
$image = $this->open_image($img);
if ($image == false) {
die ('<strong>You uploaded an invalid image. Please go back and try again.</strong>');
}
// Get original width and height
$width = imagesx($image);
$height = imagesy($image);
// New width? Calculate new height
if ( $new_width != -1 ) {
$new_width = floatval($new_width);
$new_height = $height * ($new_width/$width);
// New height? Calculate new width
} elseif ( $new_height != -1) {
$new_height = floatval($new_height);
$new_width = $width * ($new_height/$height);
// New height and new width
} elseif ( $height_post != -1 && $width_post != -1 ) {
$new_height = floatval($height);
$new_width = floatval($width);
} else {
die ('<strong>You didn\'t specify any resizing options.</strong>');
}
// Resample
$image_resized = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
}
[/php]
Bref, un grand classique...Cependant, je souhaite donc donner un nom et déplacer cette image générée au final ($image_resized) dans un dossier particulier...Je me suis donc tourné vers la fonction rename()...mais que dois-je lui passer en paramètres, sachant que j'ai déjà essayer ce genre de chose :
[php]
rename ( imagejpeg($image_resized,"mondossier/monimage.jpeg") , "monnouveaudossier/monimageminiature.jpeg");
[/php]
Ce qui me parait somme toute logique...mais bon...ca ne fonctionne pas, donc ma logique n'est pas la bonne....Help me pliz !
Merci d'avance