PHP generated thumbnails of a lot of code, but can be fully compatible with gd1.6 and gd2.x, and can ensure that the thumbnail clarity of the code is almost no, the following code can achieve good compatibility. Share it, for everyone to learn the reference.
- function Imageresize ($srcFile, $toW, $toH, $toFile = "")
- {
- if ($toFile = = "") {$toFile = $srcFile;}
- $info = "";
- $data = getimagesize ($srcFile, $info);
- Switch ($data [2])
- {
- Case 1:
- if (!function_exists ("Imagecreatefromgif")) {
- echo "Your GD library cannot use GIF format pictures, please use JPEG or PNG format!" Return ";
- Exit ();
- }
- $im = Imagecreatefromgif ($srcFile);
- Break
- Case 2:
- if (!function_exists ("Imagecreatefromjpeg")) {
- echo "Your GD library can not use JPEG format pictures, please use other format pictures!" Return ";
- Exit ();
- }
- $im = Imagecreatefromjpeg ($srcFile);
- Break
- Case 3:
- $im = Imagecreatefrompng ($srcFile);
- Break
- }
- $srcW =imagesx ($im);
- $srcH =imagesy ($im);
- $toWH = $toW/$toH;
- $srcWH = $srcW/$srcH;
- if ($toWH <= $srcWH) {
- $ftoW = $toW;
- $ftoH = $ftoW * ($srcH/$srcW);
- }
- else{
- $ftoH = $toH;
- $ftoW = $ftoH * ($srcW/$srcH);
- }
- if ($srcW > $toW | | $srcH > $toH)
- {
- if (function_exists ("Imagecreatetruecolor")) {
- @ $ni = Imagecreatetruecolor ($ftoW, $ftoH);
- if ($ni) imagecopyresampled ($ni, $im, 0,0,0,0, $ftoW, $ftoH, $srcW, $srcH);
- else{
- $ni =imagecreate ($ftoW, $ftoH);
- Imagecopyresized ($ni, $im, 0,0,0,0, $ftoW, $ftoH, $srcW, $srcH);
- }
- }else{
- $ni =imagecreate ($ftoW, $ftoH);
- Imagecopyresized ($ni, $im, 0,0,0,0, $ftoW, $ftoH, $srcW, $srcH);
- }
- if (function_exists (' imagejpeg ')) imagejpeg ($ni, $toFile);
- Else Imagepng ($ni, $toFile);
- Imagedestroy ($ni);
- }
- Imagedestroy ($im);
- }
Copy Code |