Resizing images using php

前端 未结 7 1872
慢半拍i
慢半拍i 2021-02-10 16:17

I\'m using a neat little php script to resize my images to fit in a 300x300 pixel square, while keeping the aspect ratio. I got the script from here.

Here is the whole s

7条回答
  •  太阳男子
    2021-02-10 16:47

            function imageResize($filename, $dest, $ndest,$filetempname,$filename) {
            $userfile_name = (isset($filename) ? $filename : "");
    
            $mb_byte_2 = (1024 * 2 * 1000);            
            $ftmp = $filetempname;
    
            $oname = $filename;
            $fname = $dest;
            $sizes = getimagesize($ftmp);
            $width = $sizes[0];
            $height = $sizes[1];
    
            $extenssion = strstr($oname, ".");
    
            $prod_img = $fname;
            $prod_img_thumb =$ndest;
            move_uploaded_file($ftmp, $prod_img);
            $original_filesize = (filesize($prod_img) / 1024);
            $sizes = getimagesize($prod_img);
    
            $expected_max_width = 125;
            $expected_max_height = 100;
            $originalw = $sizes[0];
            $originalh = $sizes[1];
    
            if ($originalh < $expected_max_height) {
                if ($originalw < $expected_max_width) {
                    $imgwidth = $originalw;
                    $imgheight = $originalh;
                } else {
                    $imgheight = ($expected_max_width * $originalh) / $originalw;
                    $imgwidth = $expected_max_width;
                }
            } else {
                $new_height = $expected_max_height;
                $new_width = ($expected_max_height * $originalw) / $originalh;
    
                if ($new_width > $expected_max_width) {
                    $new_height = ($expected_max_width * $expected_max_height) / $new_width;
                    $new_width = $expected_max_width;
                }
    
                $imgwidth = $new_width;
                $imgheight = $new_height;
            }
            $new_h = $imgheight;
            $new_w = $imgwidth;
            $new_w_im = '125';
            $new_h_im = '100';
            $offsetwidth = $new_w_im - $new_w;
            $offsetw = $offsetwidth / 2;
            $offsetheight = $new_h_im - $new_h;
            $offseth = $offsetheight / 2;
            //  echo $extenssion;
            $dest = imagecreatetruecolor($new_w_im, $new_h_im);
            $bg = imagecolorallocate($dest, 255, 255, 255);
            imagefill($dest, 0, 0, $bg);
    
            if ($extenssion == '.jpg') {
                $src = imagecreatefromjpeg($prod_img)
                        or die('Problem In opening Source JPG Image');
            } elseif ($extenssion == '.jpeg') {
                $src = imagecreatefromjpeg($prod_img)
                        or die('Problem In opening Source JPEG Image');
            } elseif ($extenssion == '.gif') {
                $src = imagecreatefromgif($prod_img)
                        or die('Problem In opening Source GIF Image');
            } elseif ($extenssion == '.png') {
                $src = imagecreatefrompng($prod_img)
                        or die('Problem In opening Source PNG Image');
            } elseif ($extenssion == '.bmp') {
                //print_r($prod_img);
              //                $src = imagecreatefrombmp($prod_img)
             //                or die('Problem In opening Source BMP Image');
            }
    
            if (function_exists('imagecopyresampled')) {
                imagecopyresampled($dest, $src, $offsetw, $offseth, 0, 0, 
                 $new_w, $new_h,   imagesx($src), imagesy($src))
                        or die('Problem In resizing');
            } else {
                Imagecopyresized($dest, $src, $offsetw, $offseth, 0, 0,  
                $new_w, $new_h, imagesx($src), imagesy($src))
                        or die('Problem In resizing');
            }
            imagejpeg($dest, $prod_img_thumb, 72)
                    or die('Problem In saving');
            imagedestroy($dest);
            @ob_flush();
            $new_filesize = (filesize($prod_img) / 1024);
        }
    

提交回复
热议问题