Pour créer une miniature, déjà, il fait que votre Php dispose de GDlib (vous pouvez le vérifier grâce a ce code :
<?php phpinfo(); ?>
Ensuite, il faut utilisé GDlib, et la ça se complique. En effet, il existe plusieurs fonctions pour créer des miniatures. La différence est non seulement dans la qualité de la miniatures mais aussi dans la version de GDlib. Pour simplifier tous ça, ci-dessous, vous trouverez un ensemble de fonction qui permet de créer rapidement un miniature de la meilleure qualité possible.
<?php class thumbnails { function ResizeImage($image, $newimage, $newwidth=0, $newheight=0) { if (!function_exists('ImageTypes')) return false; list($width,$height,$type) = GetImageSize($image); if ($im = self::ReadImageFromFile($image, $type)) { if ($newwidth==0) $newwidth = ($newheight / $height) * $width; else if ($newheight==0) $newheight = ($newwidth / $width) * $height; elseif ($newheight && ($width < $height)) $newwidth = ($newheight / $height) * $width; else $newheight = ($newwidth / $width) * $height; if (function_exists('ImageCreateTrueColor')) $im2 = ImageCreateTrueColor($newwidth,$newheight); else $im2 = ImageCreate($newwidth,$newheight); if (function_exists('imagealphablending')) imagealphablending($im2, false); if (function_exists('imagesavealpha')) imagesavealpha ($im2 , true); if (function_exists('ImageCopyResampled')) ImageCopyResampled($im2,$im,0,0,0,0,$newwidth,$newheight,$width,$height); else ImageCopyResized($im2,$im,0,0,0,0,$newwidth,$newheight,$width,$height); if (self::WriteImageToFile($im2, $newimage, $type)) return true; } return false; } private function ReadImageFromFile($filename, $type) { $imagetypes = ImageTypes(); switch ($type) { case 1 : if ($imagetypes & IMG_GIF) return $im = ImageCreateFromGIF($filename); break; case 2 : if ($imagetypes & IMG_JPEG) return ImageCreateFromJPEG($filename); break; case 3 : if ($imagetypes & IMG_PNG) return ImageCreateFromPNG($filename); break; default: return 0; } } private function WriteImageToFile($im, $filename, $type) { switch ($type) { case 1 : return ImageGIF($im, $filename); case 2 : return ImageJpeg($im, $filename, 85); case 3 : return ImagePNG($im, $filename); default: return false; } } } ?>
C'est simple. Vous appelez la fonction thumbnails::ResizeImage avec ses paramètres :
thumbnails::ResizeImage($image, $newimage, $newwidth, $newheight)
Et voila, si thumbnails::ResizeImage retourne “true” tous c'est bien passé et $newimage à été créée.
22/05/2007 11:41 -
Discussion
très bon script, un seul regret c'est de ne pas pouvoir redimensionner dans une même largeur et hauteue avec le centrage de l'image sur fond blanc. Ca aurait été le pied
hum, je ne saisie pas quand est ce que l'image est sauvegardé… à aucun moment la miniature est créé, juste le nom est copié..
J'ai rajouter quelques instruction permettant de générer automatiquement le nom de la miniature
Remplacer la ligne: function ResizeImage($image, $newimage, $newwidth=0, $newheight=0) {
Par: function ResizeImage($image, $newwidth=0, $newheight=0) { $extension= strrchr($image, ”.”); $newimage= str_replace($extension, '_small'.$extension, $image);
L'appel de la fonction ne contient alors plus que trois paramétres: thumbnails::ResizeImage('monimage.jpg', 150, 150);
Ainsi la miniature crée se vera attribué automatiquement le nom: monimage_small.jpg
Chez moi les images en portrait sont retourner systématiquement.
Most help aritlces on the web are inaccurate or incoherent. Not this!
I will never call this people trrsoriet because they have not done anything against our country when ever a bomb blast happens inside a temple or train and scores of Hindus gets killed I used to burn in anger inside me and never had the courage to retaliate them, but I really appreciate this men for retaliating so that they also know what pain we Hindus go through when our brothers and sisters get killed Media in India is very biased because around one million Hindus and Sikhs have been butchers and thrown out of their home overnight and living as refuge in our own country but our media and politicians least bothered but on the other hand when few Muslims got affected in Gujarat then our entire pseudo seculars came to road dancing naked, where they went when a entire population of Kashmir Hindus and Sikhs thrown out of their home land, it's only the RSS who talks on behalf of Hindus One more information Muslims keep bombs in public places only during the Friday namaz time so that none of their faithful followers get killed, also can any one give me a single locality in India where Muslims are in majority and other faith people live peacefully, as soon as Muslims become majority in any area or country they start attacking the minority community this is fact and best example is Kashmir Thanks, Tamil from Nellai .
A bit supsrried it seems to simple and yet useful.